Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "hal/AnalogGyro.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <string> |
| 8 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | #include "HALInitializer.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 10 | #include "HALInternal.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; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 21 | std::string previousAllocation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 22 | }; |
| 23 | } // namespace |
| 24 | |
| 25 | using namespace hal; |
| 26 | |
| 27 | static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators, |
| 28 | HAL_HandleEnum::AnalogGyro>* analogGyroHandles; |
| 29 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 30 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | void InitializeAnalogGyro() { |
| 32 | static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators, |
| 33 | HAL_HandleEnum::AnalogGyro> |
| 34 | agH; |
| 35 | analogGyroHandles = &agH; |
| 36 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 37 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 38 | |
| 39 | extern "C" { |
| 40 | HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 41 | const char* allocationLocation, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 42 | int32_t* status) { |
| 43 | hal::init::CheckInit(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 44 | // Handle will be type checked by HAL_IsAccumulatorChannel |
| 45 | int16_t channel = getHandleIndex(analogHandle); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | if (!HAL_IsAccumulatorChannel(analogHandle, status)) { |
| 47 | if (*status == 0) { |
| 48 | *status = HAL_INVALID_ACCUMULATOR_CHANNEL; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 49 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro", |
| 50 | 0, kNumAccumulators, channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | } |
| 52 | return HAL_kInvalidHandle; |
| 53 | } |
| 54 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 55 | HAL_GyroHandle handle; |
| 56 | auto gyro = analogGyroHandles->Allocate(channel, &handle, status); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 58 | if (*status != 0) { |
| 59 | if (gyro) { |
| 60 | hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel, |
| 61 | gyro->previousAllocation); |
| 62 | } else { |
| 63 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro", |
| 64 | 0, kNumAccumulators, channel); |
| 65 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | gyro->handle = analogHandle; |
| 70 | gyro->index = channel; |
| 71 | |
| 72 | SimAnalogGyroData[channel].initialized = true; |
| 73 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 74 | gyro->previousAllocation = allocationLocation ? allocationLocation : ""; |
| 75 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | return handle; |
| 77 | } |
| 78 | |
| 79 | void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 80 | // No op |
| 81 | } |
| 82 | |
| 83 | void HAL_FreeAnalogGyro(HAL_GyroHandle handle) { |
| 84 | auto gyro = analogGyroHandles->Get(handle); |
| 85 | analogGyroHandles->Free(handle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 86 | if (gyro == nullptr) { |
| 87 | return; |
| 88 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | SimAnalogGyroData[gyro->index].initialized = false; |
| 90 | } |
| 91 | |
| 92 | void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle, |
| 93 | double voltsPerDegreePerSecond, double offset, |
| 94 | int32_t center, int32_t* status) { |
| 95 | // No op |
| 96 | } |
| 97 | |
| 98 | void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle, |
| 99 | double voltsPerDegreePerSecond, |
| 100 | int32_t* status) { |
| 101 | // No op |
| 102 | } |
| 103 | |
| 104 | void HAL_ResetAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 105 | auto gyro = analogGyroHandles->Get(handle); |
| 106 | if (gyro == nullptr) { |
| 107 | *status = HAL_HANDLE_ERROR; |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | SimAnalogGyroData[gyro->index].angle = 0.0; |
| 112 | } |
| 113 | |
| 114 | void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 115 | // Just do a reset |
| 116 | HAL_ResetAnalogGyro(handle, status); |
| 117 | } |
| 118 | |
| 119 | void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts, |
| 120 | int32_t* status) { |
| 121 | // No op |
| 122 | } |
| 123 | |
| 124 | double HAL_GetAnalogGyroAngle(HAL_GyroHandle handle, int32_t* status) { |
| 125 | auto gyro = analogGyroHandles->Get(handle); |
| 126 | if (gyro == nullptr) { |
| 127 | *status = HAL_HANDLE_ERROR; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | return SimAnalogGyroData[gyro->index].angle; |
| 132 | } |
| 133 | |
| 134 | double HAL_GetAnalogGyroRate(HAL_GyroHandle handle, int32_t* status) { |
| 135 | auto gyro = analogGyroHandles->Get(handle); |
| 136 | if (gyro == nullptr) { |
| 137 | *status = HAL_HANDLE_ERROR; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | return SimAnalogGyroData[gyro->index].rate; |
| 142 | } |
| 143 | |
| 144 | double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) { |
| 145 | return 0.0; |
| 146 | } |
| 147 | |
| 148 | int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) { |
| 149 | return 0; |
| 150 | } |
| 151 | } // extern "C" |