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. |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <hal/DMA.h> |
| 8 | #include <hal/DutyCycle.h> |
| 9 | #include <hal/HAL.h> |
| 10 | |
| 11 | namespace hlt { |
| 12 | |
| 13 | struct InterruptHandle { |
| 14 | public: |
| 15 | explicit InterruptHandle(int32_t* status) { |
| 16 | handle = HAL_InitializeInterrupts(status); |
| 17 | } |
| 18 | InterruptHandle(const InterruptHandle&) = delete; |
| 19 | InterruptHandle operator=(const InterruptHandle&) = delete; |
| 20 | |
| 21 | InterruptHandle(InterruptHandle&&) = default; |
| 22 | InterruptHandle& operator=(InterruptHandle&&) = default; |
| 23 | |
| 24 | ~InterruptHandle() { HAL_CleanInterrupts(handle); } |
| 25 | |
| 26 | operator HAL_InterruptHandle() const { return handle; } |
| 27 | |
| 28 | private: |
| 29 | HAL_InterruptHandle handle = 0; |
| 30 | }; |
| 31 | |
| 32 | struct DMAHandle { |
| 33 | public: |
| 34 | explicit DMAHandle(int32_t* status) { handle = HAL_InitializeDMA(status); } |
| 35 | DMAHandle(const DMAHandle&) = delete; |
| 36 | DMAHandle operator=(const DMAHandle&) = delete; |
| 37 | |
| 38 | DMAHandle(DMAHandle&&) = default; |
| 39 | DMAHandle& operator=(DMAHandle&&) = default; |
| 40 | |
| 41 | ~DMAHandle() { HAL_FreeDMA(handle); } |
| 42 | |
| 43 | operator HAL_DMAHandle() const { return handle; } |
| 44 | |
| 45 | private: |
| 46 | HAL_DMAHandle handle = 0; |
| 47 | }; |
| 48 | |
| 49 | struct AnalogInputHandle { |
| 50 | public: |
| 51 | AnalogInputHandle(int32_t port, int32_t* status) { |
| 52 | handle = HAL_InitializeAnalogInputPort(HAL_GetPort(port), nullptr, status); |
| 53 | } |
| 54 | AnalogInputHandle(const AnalogInputHandle&) = delete; |
| 55 | AnalogInputHandle operator=(const AnalogInputHandle&) = delete; |
| 56 | |
| 57 | AnalogInputHandle(AnalogInputHandle&&) = default; |
| 58 | AnalogInputHandle& operator=(AnalogInputHandle&&) = default; |
| 59 | |
| 60 | ~AnalogInputHandle() { HAL_FreeAnalogInputPort(handle); } |
| 61 | |
| 62 | operator HAL_AnalogInputHandle() const { return handle; } |
| 63 | |
| 64 | private: |
| 65 | HAL_AnalogInputHandle handle = 0; |
| 66 | }; |
| 67 | |
| 68 | struct AnalogOutputHandle { |
| 69 | public: |
| 70 | AnalogOutputHandle(int32_t port, int32_t* status) { |
| 71 | handle = HAL_InitializeAnalogOutputPort(HAL_GetPort(port), nullptr, status); |
| 72 | } |
| 73 | AnalogOutputHandle(const AnalogOutputHandle&) = delete; |
| 74 | AnalogOutputHandle operator=(const AnalogOutputHandle&) = delete; |
| 75 | |
| 76 | AnalogOutputHandle(AnalogOutputHandle&&) = default; |
| 77 | AnalogOutputHandle& operator=(AnalogOutputHandle&&) = default; |
| 78 | |
| 79 | ~AnalogOutputHandle() { HAL_FreeAnalogOutputPort(handle); } |
| 80 | |
| 81 | operator HAL_AnalogOutputHandle() const { return handle; } |
| 82 | |
| 83 | private: |
| 84 | HAL_AnalogOutputHandle handle = 0; |
| 85 | }; |
| 86 | |
| 87 | struct AnalogTriggerHandle { |
| 88 | public: |
| 89 | explicit AnalogTriggerHandle(HAL_AnalogInputHandle port, int32_t* status) { |
| 90 | handle = HAL_InitializeAnalogTrigger(port, status); |
| 91 | } |
| 92 | AnalogTriggerHandle(const AnalogTriggerHandle&) = delete; |
| 93 | AnalogTriggerHandle operator=(const AnalogTriggerHandle&) = delete; |
| 94 | |
| 95 | AnalogTriggerHandle(AnalogTriggerHandle&&) = default; |
| 96 | AnalogTriggerHandle& operator=(AnalogTriggerHandle&&) = default; |
| 97 | |
| 98 | ~AnalogTriggerHandle() { |
| 99 | int32_t status = 0; |
| 100 | HAL_CleanAnalogTrigger(handle, &status); |
| 101 | } |
| 102 | |
| 103 | operator HAL_AnalogTriggerHandle() const { return handle; } |
| 104 | |
| 105 | private: |
| 106 | HAL_AnalogTriggerHandle handle = 0; |
| 107 | }; |
| 108 | |
| 109 | struct DutyCycleHandle { |
| 110 | public: |
| 111 | DutyCycleHandle(HAL_DigitalHandle port, int32_t* status) { |
| 112 | handle = HAL_InitializeDutyCycle( |
| 113 | port, HAL_AnalogTriggerType::HAL_Trigger_kInWindow, status); |
| 114 | } |
| 115 | DutyCycleHandle(const DutyCycleHandle&) = delete; |
| 116 | DutyCycleHandle operator=(const DutyCycleHandle&) = delete; |
| 117 | |
| 118 | DutyCycleHandle(DutyCycleHandle&&) = default; |
| 119 | DutyCycleHandle& operator=(DutyCycleHandle&&) = default; |
| 120 | |
| 121 | ~DutyCycleHandle() { HAL_FreeDutyCycle(handle); } |
| 122 | |
| 123 | operator HAL_DutyCycleHandle() const { return handle; } |
| 124 | |
| 125 | private: |
| 126 | HAL_DutyCycleHandle handle = 0; |
| 127 | }; |
| 128 | |
| 129 | struct DIOHandle { |
| 130 | public: |
| 131 | DIOHandle() {} |
| 132 | DIOHandle(const DIOHandle&) = delete; |
| 133 | DIOHandle operator=(const DIOHandle&) = delete; |
| 134 | |
| 135 | DIOHandle(DIOHandle&&) = default; |
| 136 | DIOHandle& operator=(DIOHandle&&) = default; |
| 137 | |
| 138 | DIOHandle(int32_t port, HAL_Bool input, int32_t* status) { |
| 139 | handle = HAL_InitializeDIOPort(HAL_GetPort(port), input, nullptr, status); |
| 140 | } |
| 141 | |
| 142 | ~DIOHandle() { HAL_FreeDIOPort(handle); } |
| 143 | |
| 144 | operator HAL_DigitalHandle() const { return handle; } |
| 145 | |
| 146 | private: |
| 147 | HAL_DigitalHandle handle = 0; |
| 148 | }; |
| 149 | |
| 150 | struct PWMHandle { |
| 151 | public: |
| 152 | PWMHandle() {} |
| 153 | PWMHandle(const PWMHandle&) = delete; |
| 154 | PWMHandle operator=(const PWMHandle&) = delete; |
| 155 | |
| 156 | PWMHandle(PWMHandle&&) = default; |
| 157 | PWMHandle& operator=(PWMHandle&&) = default; |
| 158 | |
| 159 | PWMHandle(int32_t port, int32_t* status) { |
| 160 | handle = HAL_InitializePWMPort(HAL_GetPort(port), nullptr, status); |
| 161 | } |
| 162 | |
| 163 | ~PWMHandle() { |
| 164 | int32_t status = 0; |
| 165 | HAL_FreePWMPort(handle, &status); |
| 166 | } |
| 167 | |
| 168 | operator HAL_DigitalHandle() const { return handle; } |
| 169 | |
| 170 | private: |
| 171 | HAL_DigitalHandle handle = 0; |
| 172 | }; |
| 173 | |
| 174 | struct RelayHandle { |
| 175 | public: |
| 176 | RelayHandle(int32_t port, HAL_Bool fwd, int32_t* status) { |
| 177 | handle = HAL_InitializeRelayPort(HAL_GetPort(port), fwd, nullptr, status); |
| 178 | } |
| 179 | RelayHandle(const RelayHandle&) = delete; |
| 180 | RelayHandle operator=(const RelayHandle&) = delete; |
| 181 | |
| 182 | RelayHandle(RelayHandle&&) = default; |
| 183 | RelayHandle& operator=(RelayHandle&&) = default; |
| 184 | |
| 185 | ~RelayHandle() { HAL_FreeRelayPort(handle); } |
| 186 | |
| 187 | operator HAL_RelayHandle() const { return handle; } |
| 188 | |
| 189 | private: |
| 190 | HAL_RelayHandle handle = 0; |
| 191 | }; |
| 192 | |
| 193 | #define ASSERT_LAST_ERROR_STATUS(status, x) \ |
| 194 | do { \ |
| 195 | ASSERT_EQ(status, HAL_USE_LAST_ERROR); \ |
| 196 | const char* lastErrorMessageInMacro = HAL_GetLastError(&status); \ |
| 197 | ASSERT_EQ(status, x); \ |
| 198 | } while (0) |
| 199 | |
| 200 | } // namespace hlt |