blob: 9666f4a9e3cea1c0326572ade0127bdd7a8fa333 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -08003/* 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 Silverman8fce7482020-01-05 13:18:21 -080010#include "HALInitializer.h"
Austin Schuh1e69f942020-11-14 15:06:14 -080011#include "PortsInternal.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080012#include "hal/AnalogAccumulator.h"
Austin Schuh1e69f942020-11-14 15:06:14 -080013#include "hal/Errors.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014#include "hal/handles/IndexedHandleResource.h"
15#include "mockdata/AnalogGyroDataInternal.h"
16
17namespace {
18struct AnalogGyro {
19 HAL_AnalogInputHandle handle;
20 uint8_t index;
21};
22} // namespace
23
24using namespace hal;
25
26static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
27 HAL_HandleEnum::AnalogGyro>* analogGyroHandles;
28
29namespace hal {
30namespace init {
31void 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
40extern "C" {
41HAL_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
74void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
75 // No op
76}
77
78void 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
85void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle,
86 double voltsPerDegreePerSecond, double offset,
87 int32_t center, int32_t* status) {
88 // No op
89}
90
91void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle,
92 double voltsPerDegreePerSecond,
93 int32_t* status) {
94 // No op
95}
96
97void 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
107void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
108 // Just do a reset
109 HAL_ResetAnalogGyro(handle, status);
110}
111
112void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts,
113 int32_t* status) {
114 // No op
115}
116
117double 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
127double 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
137double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) {
138 return 0.0;
139}
140
141int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) {
142 return 0;
143}
144} // extern "C"