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/AnalogOutput.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 "AnalogInternal.h" |
| 10 | #include "HALInitializer.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 11 | #include "HALInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | #include "PortsInternal.h" |
| 13 | #include "hal/Errors.h" |
| 14 | #include "hal/handles/HandlesInternal.h" |
| 15 | #include "hal/handles/IndexedHandleResource.h" |
| 16 | |
| 17 | using namespace hal; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | struct AnalogOutput { |
| 22 | uint8_t channel; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 23 | std::string previousAllocation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | static IndexedHandleResource<HAL_AnalogOutputHandle, AnalogOutput, |
| 29 | kNumAnalogOutputs, HAL_HandleEnum::AnalogOutput>* |
| 30 | analogOutputHandles; |
| 31 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | void InitializeAnalogOutput() { |
| 34 | static IndexedHandleResource<HAL_AnalogOutputHandle, AnalogOutput, |
| 35 | kNumAnalogOutputs, HAL_HandleEnum::AnalogOutput> |
| 36 | aoH; |
| 37 | analogOutputHandles = &aoH; |
| 38 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 39 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | |
| 41 | extern "C" { |
| 42 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 43 | HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort( |
| 44 | HAL_PortHandle portHandle, const char* allocationLocation, |
| 45 | int32_t* status) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | hal::init::CheckInit(); |
| 47 | initializeAnalog(status); |
| 48 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 49 | if (*status != 0) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | return HAL_kInvalidHandle; |
| 51 | } |
| 52 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 53 | int16_t channel = getPortHandleChannel(portHandle); |
| 54 | if (channel == InvalidHandleIndex || channel >= kNumAnalogOutputs) { |
| 55 | *status = RESOURCE_OUT_OF_RANGE; |
| 56 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Output", |
| 57 | 0, kNumAnalogOutputs, channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | return HAL_kInvalidHandle; |
| 59 | } |
| 60 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 61 | HAL_AnalogOutputHandle handle; |
| 62 | auto port = analogOutputHandles->Allocate(channel, &handle, status); |
| 63 | |
| 64 | if (*status != 0) { |
| 65 | if (port) { |
| 66 | hal::SetLastErrorPreviouslyAllocated(status, "Analog Output", channel, |
| 67 | port->previousAllocation); |
| 68 | } else { |
| 69 | hal::SetLastErrorIndexOutOfRange(status, |
| 70 | "Invalid Index for Analog Output", 0, |
| 71 | kNumAnalogOutputs, channel); |
| 72 | } |
| 73 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
| 74 | } |
| 75 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | port->channel = static_cast<uint8_t>(channel); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 77 | port->previousAllocation = allocationLocation ? allocationLocation : ""; |
| 78 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 79 | return handle; |
| 80 | } |
| 81 | |
| 82 | void HAL_FreeAnalogOutputPort(HAL_AnalogOutputHandle analogOutputHandle) { |
| 83 | // no status, so no need to check for a proper free. |
| 84 | analogOutputHandles->Free(analogOutputHandle); |
| 85 | } |
| 86 | |
| 87 | void HAL_SetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle, |
| 88 | double voltage, int32_t* status) { |
| 89 | auto port = analogOutputHandles->Get(analogOutputHandle); |
| 90 | if (port == nullptr) { |
| 91 | *status = HAL_HANDLE_ERROR; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | uint16_t rawValue = static_cast<uint16_t>(voltage / 5.0 * 0x1000); |
| 96 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 97 | if (voltage < 0.0) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 98 | rawValue = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 99 | } else if (voltage > 5.0) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 100 | rawValue = 0x1000; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 101 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 102 | |
| 103 | analogOutputSystem->writeMXP(port->channel, rawValue, status); |
| 104 | } |
| 105 | |
| 106 | double HAL_GetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle, |
| 107 | int32_t* status) { |
| 108 | auto port = analogOutputHandles->Get(analogOutputHandle); |
| 109 | if (port == nullptr) { |
| 110 | *status = HAL_HANDLE_ERROR; |
| 111 | return 0.0; |
| 112 | } |
| 113 | |
| 114 | uint16_t rawValue = analogOutputSystem->readMXP(port->channel, status); |
| 115 | |
| 116 | return rawValue * 5.0 / 0x1000; |
| 117 | } |
| 118 | |
| 119 | HAL_Bool HAL_CheckAnalogOutputChannel(int32_t channel) { |
| 120 | return channel < kNumAnalogOutputs && channel >= 0; |
| 121 | } |
| 122 | |
| 123 | } // extern "C" |