Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
ExtendedInputOutput.cpp
Go to the documentation of this file.
3#include <AH/Error/Error.hpp>
4
6
7namespace ExtIO {
8
9template <class T>
10static bool inRange(T target, T start, T end) {
11 return target >= start && target < end;
12}
13
15 for (auto &el : ExtendedIOElement::getAll())
16 if (pin < el.getStart())
17 break;
18 else if (inRange(pin, el.getStart(), el.getEnd()))
19 return &el;
20 return nullptr;
21}
22
24 auto *el = getIOElementOfPinOrNull(pin);
25 if (el == nullptr)
27 F("The given pin does not correspond to an Extended IO element."),
28 0x8888);
29 return el;
30}
31
32void pinMode(pin_t pin, PinMode_t mode) {
33 if (pin == NO_PIN)
34 return; // LCOV_EXCL_LINE
35 else if (isNativePin(pin)) {
36 ::pinMode(arduino_pin_cast(pin), mode);
37 } else {
38 auto el = getIOElementOfPin(pin);
39 el->pinMode(pin - el->getStart(), mode);
40 }
41}
42
44 if (pin == NO_PIN)
45 return; // LCOV_EXCL_LINE
46 else if (isNativePin(pin)) {
47 ::digitalWrite(arduino_pin_cast(pin), val);
48 } else {
49 auto el = getIOElementOfPin(pin);
50 el->digitalWrite(pin - el->getStart(), val);
51 }
52}
53
55 if (pin == NO_PIN)
56 return LOW; // LCOV_EXCL_LINE
57 else if (isNativePin(pin)) {
58 return ::digitalRead(arduino_pin_cast(pin));
59 } else {
60 auto el = getIOElementOfPin(pin);
61 return el->digitalRead(pin - el->getStart());
62 }
63}
64
66 if (pin == NO_PIN)
67 return 0; // LCOV_EXCL_LINE
68 else if (isNativePin(pin)) {
69 return ::analogRead(arduino_pin_cast(pin));
70 } else {
71 auto el = getIOElementOfPin(pin);
72 return el->analogRead(pin - el->getStart());
73 }
74}
75
77 if (pin == NO_PIN)
78 return; // LCOV_EXCL_LINE
79 else if (isNativePin(pin)) {
80#ifndef ESP32
81 ::analogWrite(arduino_pin_cast(pin), val);
82#endif
83 } else {
84 auto el = getIOElementOfPin(pin);
85 el->analogWrite(pin - el->getStart(), val);
86 }
87}
88void analogWrite(pin_t pin, int val) { analogWrite(pin, (analog_t)val); }
89
91 if (pin == NO_PIN)
92 return; // LCOV_EXCL_LINE
93 else if (isNativePin(pin)) {
94 ::pinMode(arduino_pin_cast(pin), mode);
95 } else {
96 auto el = getIOElementOfPin(pin);
97 el->pinModeBuffered(pin - el->getStart(), mode);
98 }
99}
100
102 if (pin == NO_PIN)
103 return; // LCOV_EXCL_LINE
104 else if (isNativePin(pin)) {
105 ::digitalWrite(arduino_pin_cast(pin), val);
106 } else {
107 auto el = getIOElementOfPin(pin);
109 }
110}
111
113 if (pin == NO_PIN)
114 return LOW; // LCOV_EXCL_LINE
115 else if (isNativePin(pin)) {
116 return ::digitalRead(arduino_pin_cast(pin));
117 } else {
118 auto el = getIOElementOfPin(pin);
119 return el->digitalReadBuffered(pin - el->getStart());
120 }
121}
122
124 if (pin == NO_PIN)
125 return 0; // LCOV_EXCL_LINE
126 else if (isNativePin(pin)) {
127 return ::analogRead(arduino_pin_cast(pin));
128 } else {
129 auto el = getIOElementOfPin(pin);
130 return el->analogReadBuffered(pin - el->getStart());
131 }
132 return 0;
133}
134
136 if (pin == NO_PIN)
137 return; // LCOV_EXCL_LINE
138 else if (isNativePin(pin)) {
139#ifndef ESP32
140 ::analogWrite(arduino_pin_cast(pin), val);
141#endif
142 } else {
143 auto el = getIOElementOfPin(pin);
145 }
146}
150
151void shiftOut(pin_t dataPin, pin_t clockPin, BitOrder_t bitOrder, uint8_t val) {
152 if (dataPin == NO_PIN || clockPin == NO_PIN)
153 return;
154 // Native version
155 if (isNativePin(dataPin) && isNativePin(clockPin)) {
156 ::shiftOut(arduino_pin_cast(dataPin), arduino_pin_cast(clockPin),
157 bitOrder, val);
158 }
159 // ExtIO version
160 else if (!isNativePin(dataPin) && !isNativePin(clockPin)) {
161 auto dataEl = getIOElementOfPin(dataPin);
162 auto dataPinN = dataPin - dataEl->getStart();
163 auto clockEl = getIOElementOfPin(clockPin);
164 auto clockPinN = clockPin - clockEl->getStart();
165 for (uint8_t i = 0; i < 8; i++) {
166 uint8_t mask = bitOrder == LSBFIRST ? (1 << i) : (1 << (7 - i));
170 }
171 }
172 // Mixed version (slow)
173 else {
174 for (uint8_t i = 0; i < 8; i++) {
175 uint8_t mask = bitOrder == LSBFIRST ? (1 << i) : (1 << (7 - i));
176 digitalWrite(dataPin, (val & mask) ? HIGH : LOW);
177 digitalWrite(clockPin, HIGH);
178 digitalWrite(clockPin, LOW);
179 }
180 }
181}
182
183} // namespace ExtIO
184
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr PinStatus_t LOW
AH::function_traits< decltype(::digitalWrite)>::argument_t< 1 > PinStatus_t
uint8_t BitOrder_t
constexpr PinStatus_t HIGH
AH::function_traits< decltype(::pinMode)>::argument_t< 1 > PinMode_t
An abstract base class for Extended Input/Output elements.
pin_t getStart() const
Get the smallest global extended IO pin number that belongs to this extended IO element.
pin_t getEnd() const
Get the largest global extended IO pin number that belongs to this extended IO element.
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
void pinMode(pin_int_t pin, PinMode_t mode) override __attribute__((deprecated))
The pinMode function is not implemented because the mode is OUTPUT by definition.
PinStatus_t digitalRead(pin_int_t pin) override
Get the current state of a given output pin.
void digitalWrite(pin_int_t pin, PinStatus_t val) override
Set the state of a given output pin.
void analogWriteBuffered(pin_int_t pin, analog_t val) override __attribute__((deprecated))
The analogWrite function is not deprecated because a shift is always digital.
PinStatus_t digitalReadBuffered(pin_int_t pin) override
Get the current state of a given output pin.
void pinModeBuffered(pin_int_t pin, PinMode_t mode) override __attribute__((deprecated))
The pinMode function is not implemented because the mode is OUTPUT by definition.
analog_t analogReadBuffered(pin_int_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a shift is always digital.
void digitalWriteBuffered(pin_int_t pin, PinStatus_t val) override
Set the output of a given pin in the software buffer.
void analogWrite(pin_int_t pin, analog_t val) override __attribute__((deprecated))
The analogWrite function is not deprecated because a shift is always digital.
analog_t analogRead(pin_int_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a shift is always digital.
#define FATAL_ERROR(msg, errc)
Print the error message and error code, and stop the execution.
Definition Error.hpp:57
void analogWriteBuffered(pin_t pin, analog_t val)
A buffered ExtIO version of the Arduino function.
bool isNativePin(pin_t pin)
Check if the given pin number is a real Arduino pin number, and not an ExtIO pin number.
void pinModeBuffered(pin_t pin, PinMode_t mode)
A buffered ExtIO version of the Arduino function.
ExtendedIOElement * getIOElementOfPin(pin_t pin)
Find the IO element of a given extended IO pin number.
void digitalWriteBuffered(pin_t pin, PinStatus_t val)
A buffered ExtIO version of the Arduino function.
void analogWrite(pin_t pin, analog_t val)
An ExtIO version of the Arduino function.
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
ExtendedIOElement * getIOElementOfPinOrNull(pin_t pin)
Find the IO element of a given extended IO pin number.
PinStatus_t digitalReadBuffered(pin_t pin)
A buffered ExtIO version of the Arduino function.
analog_t analogRead(pin_t pin)
An ExtIO version of the Arduino function.
analog_t analogReadBuffered(pin_t pin)
A buffered ExtIO version of the Arduino function.
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
void shiftOut(pin_t dataPin, pin_t clockPin, BitOrder_t bitOrder, uint8_t val)
An ExtIO version of the Arduino function.
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.
static bool inRange(T target, T start, T end)
uint16_t analog_t
The type returned from analogRead and similar functions.
constexpr ArduinoPin_t arduino_pin_cast(T t)
Type for storing pin numbers of Extended Input/Output elements.