blob: 3efca200e7bb104ec01942df58f98a290ac1fdde [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "hal/AnalogGyro.h"
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <string>
8
Brian Silverman8fce7482020-01-05 13:18:21 -08009#include "HALInitializer.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070010#include "HALInternal.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;
Austin Schuh812d0d12021-11-04 20:16:48 -070021 std::string previousAllocation;
Brian Silverman8fce7482020-01-05 13:18:21 -080022};
23} // namespace
24
25using namespace hal;
26
27static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
28 HAL_HandleEnum::AnalogGyro>* analogGyroHandles;
29
Austin Schuh812d0d12021-11-04 20:16:48 -070030namespace hal::init {
Brian Silverman8fce7482020-01-05 13:18:21 -080031void InitializeAnalogGyro() {
32 static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
33 HAL_HandleEnum::AnalogGyro>
34 agH;
35 analogGyroHandles = &agH;
36}
Austin Schuh812d0d12021-11-04 20:16:48 -070037} // namespace hal::init
Brian Silverman8fce7482020-01-05 13:18:21 -080038
39extern "C" {
40HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
Austin Schuh812d0d12021-11-04 20:16:48 -070041 const char* allocationLocation,
Brian Silverman8fce7482020-01-05 13:18:21 -080042 int32_t* status) {
43 hal::init::CheckInit();
Austin Schuh812d0d12021-11-04 20:16:48 -070044 // Handle will be type checked by HAL_IsAccumulatorChannel
45 int16_t channel = getHandleIndex(analogHandle);
Brian Silverman8fce7482020-01-05 13:18:21 -080046 if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
47 if (*status == 0) {
48 *status = HAL_INVALID_ACCUMULATOR_CHANNEL;
Austin Schuh812d0d12021-11-04 20:16:48 -070049 hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
50 0, kNumAccumulators, channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080051 }
52 return HAL_kInvalidHandle;
53 }
54
Austin Schuh812d0d12021-11-04 20:16:48 -070055 HAL_GyroHandle handle;
56 auto gyro = analogGyroHandles->Allocate(channel, &handle, status);
Brian Silverman8fce7482020-01-05 13:18:21 -080057
Austin Schuh812d0d12021-11-04 20:16:48 -070058 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 Silverman8fce7482020-01-05 13:18:21 -080066 return HAL_kInvalidHandle; // failed to allocate. Pass error back.
Brian Silverman8fce7482020-01-05 13:18:21 -080067 }
68
69 gyro->handle = analogHandle;
70 gyro->index = channel;
71
72 SimAnalogGyroData[channel].initialized = true;
73
Austin Schuh812d0d12021-11-04 20:16:48 -070074 gyro->previousAllocation = allocationLocation ? allocationLocation : "";
75
Brian Silverman8fce7482020-01-05 13:18:21 -080076 return handle;
77}
78
79void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
80 // No op
81}
82
83void HAL_FreeAnalogGyro(HAL_GyroHandle handle) {
84 auto gyro = analogGyroHandles->Get(handle);
85 analogGyroHandles->Free(handle);
Austin Schuh812d0d12021-11-04 20:16:48 -070086 if (gyro == nullptr) {
87 return;
88 }
Brian Silverman8fce7482020-01-05 13:18:21 -080089 SimAnalogGyroData[gyro->index].initialized = false;
90}
91
92void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle,
93 double voltsPerDegreePerSecond, double offset,
94 int32_t center, int32_t* status) {
95 // No op
96}
97
98void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle,
99 double voltsPerDegreePerSecond,
100 int32_t* status) {
101 // No op
102}
103
104void 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
114void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
115 // Just do a reset
116 HAL_ResetAnalogGyro(handle, status);
117}
118
119void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts,
120 int32_t* status) {
121 // No op
122}
123
124double 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
134double 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
144double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) {
145 return 0.0;
146}
147
148int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) {
149 return 0;
150}
151} // extern "C"