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/Relay.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 "DigitalInternal.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/handles/IndexedHandleResource.h" |
| 14 | |
| 15 | using namespace hal; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | struct Relay { |
| 20 | uint8_t channel; |
| 21 | bool fwd; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 22 | std::string previousAllocation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | static IndexedHandleResource<HAL_RelayHandle, Relay, kNumRelayChannels, |
| 28 | HAL_HandleEnum::Relay>* relayHandles; |
| 29 | |
| 30 | // Create a mutex to protect changes to the relay values |
| 31 | static wpi::mutex digitalRelayMutex; |
| 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 33 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | void InitializeRelay() { |
| 35 | static IndexedHandleResource<HAL_RelayHandle, Relay, kNumRelayChannels, |
| 36 | HAL_HandleEnum::Relay> |
| 37 | rH; |
| 38 | relayHandles = &rH; |
| 39 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 40 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 41 | |
| 42 | extern "C" { |
| 43 | |
| 44 | HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 45 | const char* allocationLocation, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | int32_t* status) { |
| 47 | hal::init::CheckInit(); |
| 48 | initializeDigital(status); |
| 49 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 50 | if (*status != 0) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | return HAL_kInvalidHandle; |
| 52 | } |
| 53 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 54 | int16_t channel = getPortHandleChannel(portHandle); |
| 55 | if (channel == InvalidHandleIndex || channel >= kNumRelayChannels) { |
| 56 | *status = RESOURCE_OUT_OF_RANGE; |
| 57 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0, |
| 58 | kNumRelayChannels, channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | return HAL_kInvalidHandle; |
| 60 | } |
| 61 | |
| 62 | if (!fwd) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 63 | channel += kNumRelayHeaders; // add 4 to reverse channels |
| 64 | } |
| 65 | |
| 66 | HAL_RelayHandle handle; |
| 67 | auto port = relayHandles->Allocate(channel, &handle, status); |
| 68 | |
| 69 | if (*status != 0) { |
| 70 | if (port) { |
| 71 | hal::SetLastErrorPreviouslyAllocated(status, "Relay", channel, |
| 72 | port->previousAllocation); |
| 73 | } else { |
| 74 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0, |
| 75 | kNumRelayChannels, channel); |
| 76 | } |
| 77 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
| 78 | } |
| 79 | |
| 80 | if (!fwd) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 81 | // Subtract number of headers to put channel in range |
| 82 | channel -= kNumRelayHeaders; |
| 83 | |
| 84 | port->fwd = false; // set to reverse |
| 85 | } else { |
| 86 | port->fwd = true; // set to forward |
| 87 | } |
| 88 | |
| 89 | port->channel = static_cast<uint8_t>(channel); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 90 | port->previousAllocation = allocationLocation ? allocationLocation : ""; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 91 | return handle; |
| 92 | } |
| 93 | |
| 94 | void HAL_FreeRelayPort(HAL_RelayHandle relayPortHandle) { |
| 95 | // no status, so no need to check for a proper free. |
| 96 | relayHandles->Free(relayPortHandle); |
| 97 | } |
| 98 | |
| 99 | HAL_Bool HAL_CheckRelayChannel(int32_t channel) { |
| 100 | // roboRIO only has 4 headers, and the FPGA has |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 101 | // separate functions for forward and reverse, |
| 102 | // instead of separate channel IDs |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 103 | return channel < kNumRelayHeaders && channel >= 0; |
| 104 | } |
| 105 | |
| 106 | void HAL_SetRelay(HAL_RelayHandle relayPortHandle, HAL_Bool on, |
| 107 | int32_t* status) { |
| 108 | auto port = relayHandles->Get(relayPortHandle); |
| 109 | if (port == nullptr) { |
| 110 | *status = HAL_HANDLE_ERROR; |
| 111 | return; |
| 112 | } |
| 113 | std::scoped_lock lock(digitalRelayMutex); |
| 114 | uint8_t relays = 0; |
| 115 | if (port->fwd) { |
| 116 | relays = relaySystem->readValue_Forward(status); |
| 117 | } else { |
| 118 | relays = relaySystem->readValue_Reverse(status); |
| 119 | } |
| 120 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 121 | if (*status != 0) { |
| 122 | return; // bad status read |
| 123 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | |
| 125 | if (on) { |
| 126 | relays |= 1 << port->channel; |
| 127 | } else { |
| 128 | relays &= ~(1 << port->channel); |
| 129 | } |
| 130 | |
| 131 | if (port->fwd) { |
| 132 | relaySystem->writeValue_Forward(relays, status); |
| 133 | } else { |
| 134 | relaySystem->writeValue_Reverse(relays, status); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | HAL_Bool HAL_GetRelay(HAL_RelayHandle relayPortHandle, int32_t* status) { |
| 139 | auto port = relayHandles->Get(relayPortHandle); |
| 140 | if (port == nullptr) { |
| 141 | *status = HAL_HANDLE_ERROR; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | uint8_t relays = 0; |
| 146 | if (port->fwd) { |
| 147 | relays = relaySystem->readValue_Forward(status); |
| 148 | } else { |
| 149 | relays = relaySystem->readValue_Reverse(status); |
| 150 | } |
| 151 | |
| 152 | return (relays & (1 << port->channel)) != 0; |
| 153 | } |
| 154 | |
| 155 | } // extern "C" |