blob: b7d84d9b5f5038afb07a7d50ab983ec0a840f522 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
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
20namespace {
21struct AnalogGyro {
22 HAL_AnalogInputHandle handle;
23 uint8_t index;
24};
25} // namespace
26
27using namespace hal;
28
29static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
30 HAL_HandleEnum::AnalogGyro>* analogGyroHandles;
31
32namespace hal {
33namespace init {
34void 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
43extern "C" {
44HAL_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
77void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
78 // No op
79}
80
81void 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
88void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle,
89 double voltsPerDegreePerSecond, double offset,
90 int32_t center, int32_t* status) {
91 // No op
92}
93
94void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle,
95 double voltsPerDegreePerSecond,
96 int32_t* status) {
97 // No op
98}
99
100void 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
110void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
111 // Just do a reset
112 HAL_ResetAnalogGyro(handle, status);
113}
114
115void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts,
116 int32_t* status) {
117 // No op
118}
119
120double 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
130double 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
140double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) {
141 return 0.0;
142}
143
144int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) {
145 return 0;
146}
147} // extern "C"