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