Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 "DigitalInternal.h" |
| 9 | |
| 10 | #include "ConstantsInternal.h" |
| 11 | #include "PortsInternal.h" |
| 12 | #include "hal/AnalogTrigger.h" |
| 13 | #include "hal/HAL.h" |
| 14 | #include "hal/Ports.h" |
| 15 | |
| 16 | namespace hal { |
| 17 | |
| 18 | DigitalHandleResource<HAL_DigitalHandle, DigitalPort, |
| 19 | kNumDigitalChannels + kNumPWMHeaders>* |
| 20 | digitalChannelHandles; |
| 21 | |
| 22 | namespace init { |
| 23 | void InitializeDigitalInternal() { |
| 24 | static DigitalHandleResource<HAL_DigitalHandle, DigitalPort, |
| 25 | kNumDigitalChannels + kNumPWMHeaders> |
| 26 | dcH; |
| 27 | digitalChannelHandles = &dcH; |
| 28 | } |
| 29 | } // namespace init |
| 30 | |
| 31 | bool remapDigitalSource(HAL_Handle digitalSourceHandle, |
| 32 | HAL_AnalogTriggerType analogTriggerType, |
| 33 | uint8_t& channel, uint8_t& module, |
| 34 | bool& analogTrigger) { |
| 35 | if (isHandleType(digitalSourceHandle, HAL_HandleEnum::AnalogTrigger)) { |
| 36 | // If handle passed, index is not negative |
| 37 | int32_t index = getHandleIndex(digitalSourceHandle); |
| 38 | channel = (index << 2) + analogTriggerType; |
| 39 | module = channel >> 4; |
| 40 | analogTrigger = true; |
| 41 | return true; |
| 42 | } else if (isHandleType(digitalSourceHandle, HAL_HandleEnum::DIO)) { |
| 43 | int32_t index = getHandleIndex(digitalSourceHandle); |
| 44 | if (index >= kNumDigitalHeaders) { |
| 45 | channel = remapMXPChannel(index); |
| 46 | module = 1; |
| 47 | } else { |
| 48 | channel = index; |
| 49 | module = 0; |
| 50 | } |
| 51 | analogTrigger = false; |
| 52 | return true; |
| 53 | } else { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | int32_t remapMXPChannel(int32_t channel) { return channel - 10; } |
| 59 | |
| 60 | int32_t remapMXPPWMChannel(int32_t channel) { |
| 61 | if (channel < 14) { |
| 62 | return channel - 10; // first block of 4 pwms (MXP 0-3) |
| 63 | } else { |
| 64 | return channel - 6; // block of PWMs after SPI |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | int32_t GetDigitalInputChannel(HAL_DigitalHandle handle, int32_t* status) { |
| 69 | auto digital = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO); |
| 70 | if (digital == nullptr) { |
| 71 | *status = HAL_HANDLE_ERROR; |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | return digital->channel; |
| 76 | } |
| 77 | } // namespace hal |