Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */ |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "hal/AnalogGyro.h" |
| 9 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include "HALInitializer.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 11 | #include "PortsInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | #include "hal/AnalogAccumulator.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 13 | #include "hal/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | #include "hal/handles/IndexedHandleResource.h" |
| 15 | #include "mockdata/AnalogGyroDataInternal.h" |
| 16 | |
| 17 | namespace { |
| 18 | struct AnalogGyro { |
| 19 | HAL_AnalogInputHandle handle; |
| 20 | uint8_t index; |
| 21 | }; |
| 22 | } // namespace |
| 23 | |
| 24 | using namespace hal; |
| 25 | |
| 26 | static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators, |
| 27 | HAL_HandleEnum::AnalogGyro>* analogGyroHandles; |
| 28 | |
| 29 | namespace hal { |
| 30 | namespace init { |
| 31 | void InitializeAnalogGyro() { |
| 32 | static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators, |
| 33 | HAL_HandleEnum::AnalogGyro> |
| 34 | agH; |
| 35 | analogGyroHandles = &agH; |
| 36 | } |
| 37 | } // namespace init |
| 38 | } // namespace hal |
| 39 | |
| 40 | extern "C" { |
| 41 | HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle, |
| 42 | int32_t* status) { |
| 43 | hal::init::CheckInit(); |
| 44 | if (!HAL_IsAccumulatorChannel(analogHandle, status)) { |
| 45 | if (*status == 0) { |
| 46 | *status = HAL_INVALID_ACCUMULATOR_CHANNEL; |
| 47 | } |
| 48 | return HAL_kInvalidHandle; |
| 49 | } |
| 50 | |
| 51 | // handle known to be correct, so no need to type check |
| 52 | int16_t channel = getHandleIndex(analogHandle); |
| 53 | |
| 54 | auto handle = analogGyroHandles->Allocate(channel, status); |
| 55 | |
| 56 | if (*status != 0) |
| 57 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
| 58 | |
| 59 | // Initialize port structure |
| 60 | auto gyro = analogGyroHandles->Get(handle); |
| 61 | if (gyro == nullptr) { // would only error on thread issue |
| 62 | *status = HAL_HANDLE_ERROR; |
| 63 | return HAL_kInvalidHandle; |
| 64 | } |
| 65 | |
| 66 | gyro->handle = analogHandle; |
| 67 | gyro->index = channel; |
| 68 | |
| 69 | SimAnalogGyroData[channel].initialized = true; |
| 70 | |
| 71 | return handle; |
| 72 | } |
| 73 | |
| 74 | void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 75 | // No op |
| 76 | } |
| 77 | |
| 78 | void HAL_FreeAnalogGyro(HAL_GyroHandle handle) { |
| 79 | auto gyro = analogGyroHandles->Get(handle); |
| 80 | analogGyroHandles->Free(handle); |
| 81 | if (gyro == nullptr) return; |
| 82 | SimAnalogGyroData[gyro->index].initialized = false; |
| 83 | } |
| 84 | |
| 85 | void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle, |
| 86 | double voltsPerDegreePerSecond, double offset, |
| 87 | int32_t center, int32_t* status) { |
| 88 | // No op |
| 89 | } |
| 90 | |
| 91 | void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle, |
| 92 | double voltsPerDegreePerSecond, |
| 93 | int32_t* status) { |
| 94 | // No op |
| 95 | } |
| 96 | |
| 97 | void HAL_ResetAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 98 | auto gyro = analogGyroHandles->Get(handle); |
| 99 | if (gyro == nullptr) { |
| 100 | *status = HAL_HANDLE_ERROR; |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | SimAnalogGyroData[gyro->index].angle = 0.0; |
| 105 | } |
| 106 | |
| 107 | void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 108 | // Just do a reset |
| 109 | HAL_ResetAnalogGyro(handle, status); |
| 110 | } |
| 111 | |
| 112 | void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts, |
| 113 | int32_t* status) { |
| 114 | // No op |
| 115 | } |
| 116 | |
| 117 | double HAL_GetAnalogGyroAngle(HAL_GyroHandle handle, int32_t* status) { |
| 118 | auto gyro = analogGyroHandles->Get(handle); |
| 119 | if (gyro == nullptr) { |
| 120 | *status = HAL_HANDLE_ERROR; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | return SimAnalogGyroData[gyro->index].angle; |
| 125 | } |
| 126 | |
| 127 | double HAL_GetAnalogGyroRate(HAL_GyroHandle handle, int32_t* status) { |
| 128 | auto gyro = analogGyroHandles->Get(handle); |
| 129 | if (gyro == nullptr) { |
| 130 | *status = HAL_HANDLE_ERROR; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | return SimAnalogGyroData[gyro->index].rate; |
| 135 | } |
| 136 | |
| 137 | double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) { |
| 138 | return 0.0; |
| 139 | } |
| 140 | |
| 141 | int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) { |
| 142 | return 0; |
| 143 | } |
| 144 | } // extern "C" |