blob: db6455ad47c2be121bb042adc06551d289a83bd4 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -08003/* 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
Brian Silverman8fce7482020-01-05 13:18:21 -080010#include "PortsInternal.h"
11#include "hal/AnalogTrigger.h"
Austin Schuh1e69f942020-11-14 15:06:14 -080012#include "hal/Errors.h"
13#include "hal/handles/DigitalHandleResource.h"
14#include "hal/handles/HandlesInternal.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080015
16namespace hal {
17
18DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
19 kNumDigitalChannels + kNumPWMHeaders>*
20 digitalChannelHandles;
21
22namespace init {
23void InitializeDigitalInternal() {
24 static DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
25 kNumDigitalChannels + kNumPWMHeaders>
26 dcH;
27 digitalChannelHandles = &dcH;
28}
29} // namespace init
30
31bool 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
58int32_t remapMXPChannel(int32_t channel) { return channel - 10; }
59
60int32_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
68int32_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