Control Surface pin-t-adl
MIDI Control Surface library for Arduino
ExtendedInputOutput.cpp
Go to the documentation of this file.
2AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
3
4#include "ExtendedIOElement.hpp"
6#include <AH/Error/Error.hpp>
7
9
10namespace ExtIO {
11
12template <class T>
13static bool inRange(T target, T start, T end) {
14 return target >= start && target < end;
15}
16
18 for (auto &el : ExtendedIOElement::getAll())
19 if (pin < el.getStart())
20 break;
21 else if (inRange(pin, el.getStart(), el.getEnd()))
22 return &el;
23 return nullptr;
24}
25
27 auto *el = getIOElementOfPinOrNull(pin);
28 if (el == nullptr)
30 F("The given pin does not correspond to an Extended IO element."),
31 0x8888);
32 return el;
33}
34
35void pinMode(pin_t pin, PinMode_t mode) {
36 if (pin == NO_PIN)
37 return; // LCOV_EXCL_LINE
38 else if (isNativePin(pin)) {
39 ::pinMode(arduino_pin_cast(pin), mode);
40 } else {
41 auto el = getIOElementOfPin(pin);
42 el->pinMode(pin - el->getStart(), mode);
43 }
44}
45void pinMode(int pin, PinMode_t mode) {
46 ::pinMode(arduino_pin_cast(pin), mode);
47}
48
50 if (pin == NO_PIN)
51 return; // LCOV_EXCL_LINE
52 else if (isNativePin(pin)) {
54 } else {
55 auto el = getIOElementOfPin(pin);
56 el->digitalWrite(pin - el->getStart(), val);
57 }
58}
59void digitalWrite(int pin, PinStatus_t val) {
61}
62
64 if (pin == NO_PIN)
65 return LOW; // LCOV_EXCL_LINE
66 else if (isNativePin(pin)) {
68 } else {
69 auto el = getIOElementOfPin(pin);
70 return el->digitalRead(pin - el->getStart());
71 }
72}
75}
76
78 if (pin == NO_PIN)
79 return 0; // LCOV_EXCL_LINE
80 else if (isNativePin(pin)) {
82 } else {
83 auto el = getIOElementOfPin(pin);
84 return el->analogRead(pin - el->getStart());
85 }
86}
88
89void analogWrite(pin_t pin, analog_t val) {
90 if (pin == NO_PIN)
91 return; // LCOV_EXCL_LINE
92 else if (isNativePin(pin)) {
93#ifndef ESP32
95#endif
96 } else {
97 auto el = getIOElementOfPin(pin);
98 el->analogWrite(pin - el->getStart(), val);
99 }
100}
101void analogWrite(pin_t pin, int val) { analogWrite(pin, (analog_t)val); }
102#ifndef ESP32
103void analogWrite(int pin, analog_t val) {
105}
106void analogWrite(int pin, int val) {
108}
109#endif
110
112 if (pin == NO_PIN)
113 return; // LCOV_EXCL_LINE
114 else if (isNativePin(pin)) {
115 ::pinMode(arduino_pin_cast(pin), mode);
116 } else {
117 auto el = getIOElementOfPin(pin);
118 el->pinModeBuffered(pin - el->getStart(), mode);
119 }
120}
121
123 if (pin == NO_PIN)
124 return; // LCOV_EXCL_LINE
125 else if (isNativePin(pin)) {
127 } else {
128 auto el = getIOElementOfPin(pin);
129 el->digitalWriteBuffered(pin - el->getStart(), val);
130 }
131}
132
134 if (pin == NO_PIN)
135 return LOW; // LCOV_EXCL_LINE
136 else if (isNativePin(pin)) {
138 } else {
139 auto el = getIOElementOfPin(pin);
140 return el->digitalReadBuffered(pin - el->getStart());
141 }
142}
143
145 if (pin == NO_PIN)
146 return 0; // LCOV_EXCL_LINE
147 else if (isNativePin(pin)) {
149 } else {
150 auto el = getIOElementOfPin(pin);
151 return el->analogReadBuffered(pin - el->getStart());
152 }
153 return 0;
154}
155
157 if (pin == NO_PIN)
158 return; // LCOV_EXCL_LINE
159 else if (isNativePin(pin)) {
160#ifndef ESP32
162#endif
163 } else {
164 auto el = getIOElementOfPin(pin);
165 el->analogWriteBuffered(pin - el->getStart(), val);
166 }
167}
168void analogWriteBuffered(pin_t pin, int val) {
169 analogWriteBuffered(pin, (analog_t)val);
170}
171
172void shiftOut(pin_t dataPin, pin_t clockPin, BitOrder_t bitOrder, uint8_t val) {
173 if (dataPin == NO_PIN || clockPin == NO_PIN)
174 return;
175 // Native version
176 if (isNativePin(dataPin) && isNativePin(clockPin)) {
177 ::shiftOut(arduino_pin_cast(dataPin), arduino_pin_cast(clockPin),
178 bitOrder, val);
179 }
180 // ExtIO version
181 else if (!isNativePin(dataPin) && !isNativePin(clockPin)) {
182 auto dataEl = getIOElementOfPin(dataPin);
183 auto dataPinN = dataPin - dataEl->getStart();
184 auto clockEl = getIOElementOfPin(clockPin);
185 auto clockPinN = clockPin - clockEl->getStart();
186 for (uint8_t i = 0; i < 8; i++) {
187 uint8_t mask = bitOrder == LSBFIRST ? (1 << i) : (1 << (7 - i));
188 dataEl->digitalWrite(dataPinN, (val & mask) ? HIGH : LOW);
189 clockEl->digitalWrite(clockPinN, HIGH);
190 clockEl->digitalWrite(clockPinN, LOW);
191 }
192 }
193 // Mixed version (slow)
194 else {
195 for (uint8_t i = 0; i < 8; i++) {
196 uint8_t mask = bitOrder == LSBFIRST ? (1 << i) : (1 << (7 - i));
197 digitalWrite(dataPin, (val & mask) ? HIGH : LOW);
198 digitalWrite(clockPin, HIGH);
199 digitalWrite(clockPin, LOW);
200 }
201 }
202}
203void shiftOut(int dataPin, int clockPin, BitOrder_t bitOrder, uint8_t val) {
204 ::shiftOut(arduino_pin_cast(dataPin), arduino_pin_cast(clockPin), bitOrder,
205 val);
206}
207
208#if UINT16_MAX != UINT_MAX
209void pinMode(unsigned int pin, PinMode_t mode) {
210 ::pinMode(arduino_pin_cast(pin), mode);
211}
212void digitalWrite(unsigned int pin, PinStatus_t val) {
214}
215PinStatus_t digitalRead(unsigned int pin) {
217}
218analog_t analogRead(unsigned int pin) {
220}
221#ifndef ESP32
222void analogWrite(unsigned int pin, analog_t val) {
224}
225void analogWrite(unsigned int pin, int val) {
227}
228#endif
229void shiftOut(unsigned int dataPin, unsigned int clockPin, BitOrder_t bitOrder,
230 uint8_t val) {
231 ::shiftOut(arduino_pin_cast(dataPin), arduino_pin_cast(clockPin), bitOrder,
232 val);
233}
234#endif
235
236} // namespace ExtIO
237
239
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr PinStatus_t LOW
uint8_t BitOrder_t
constexpr PinStatus_t HIGH
AH::function_traits< decltype(::pinMode)>::argument_t< 1 > PinMode_t
AH::function_traits< decltype(::digitalWrite)>::argument_t< 1 > PinStatus_t
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
An abstract base class for Extended Input/Output elements.
#define FATAL_ERROR(msg, errc)
Print the error message and error code, and stop the execution.
Definition: Error.hpp:60
PinStatus_t digitalRead(int pin)
Overload to Arduino digitalRead 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.
ExtendedIOElement * getIOElementOfPinOrNull(pin_t pin)
Find the IO element of a given extended IO pin number.
void shiftOut(int dataPin, int clockPin, BitOrder_t bitOrder, uint8_t val)
Overload to Arduino shiftOut function.
void analogWriteBuffered(pin_t pin, int val)
A buffered ExtIO version of the Arduino function.
PinStatus_t digitalReadBuffered(pin_t pin)
A buffered ExtIO version of the Arduino function.
void digitalWrite(int pin, PinStatus_t val)
Overload to Arduino digitalWrite function.
void analogWrite(int pin, int val)
Overload to Arduino analogWrite function.
void pinMode(int pin, PinMode_t mode)
Overload to Arduino pinMode function.
analog_t analogReadBuffered(pin_t pin)
A buffered ExtIO version of the Arduino function.
analog_t analogRead(int pin)
Overload to Arduino analogRead function.
static bool inRange(T target, T start, T end)
uint16_t analog_t
The type returned from analogRead and similar functions.
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
ArduinoPin_t arduino_pin_cast(T t)
Type for storing pin numbers of Extended Input/Output elements.