Arduino Helpers pin-t-adl
Utility 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}
45
47 if (pin == NO_PIN)
48 return; // LCOV_EXCL_LINE
49 else if (isNativePin(pin)) {
51 } else {
52 auto el = getIOElementOfPin(pin);
53 el->digitalWrite(pin - el->getStart(), val);
54 }
55}
56
58 if (pin == NO_PIN)
59 return LOW; // LCOV_EXCL_LINE
60 else if (isNativePin(pin)) {
62 } else {
63 auto el = getIOElementOfPin(pin);
64 return el->digitalRead(pin - el->getStart());
65 }
66}
67
69 if (pin == NO_PIN)
70 return 0; // LCOV_EXCL_LINE
71 else if (isNativePin(pin)) {
73 } else {
74 auto el = getIOElementOfPin(pin);
75 return el->analogRead(pin - el->getStart());
76 }
77}
78
79void analogWrite(pin_t pin, analog_t val) {
80 if (pin == NO_PIN)
81 return; // LCOV_EXCL_LINE
82 else if (isNativePin(pin)) {
83#ifndef ESP32
85#endif
86 } else {
87 auto el = getIOElementOfPin(pin);
88 el->analogWrite(pin - el->getStart(), val);
89 }
90}
91void analogWrite(pin_t pin, int val) { analogWrite(pin, (analog_t)val); }
92
94 if (pin == NO_PIN)
95 return; // LCOV_EXCL_LINE
96 else if (isNativePin(pin)) {
97 ::pinMode(arduino_pin_cast(pin), mode);
98 } else {
99 auto el = getIOElementOfPin(pin);
100 el->pinModeBuffered(pin - el->getStart(), mode);
101 }
102}
103
105 if (pin == NO_PIN)
106 return; // LCOV_EXCL_LINE
107 else if (isNativePin(pin)) {
109 } else {
110 auto el = getIOElementOfPin(pin);
111 el->digitalWriteBuffered(pin - el->getStart(), val);
112 }
113}
114
116 if (pin == NO_PIN)
117 return LOW; // LCOV_EXCL_LINE
118 else if (isNativePin(pin)) {
120 } else {
121 auto el = getIOElementOfPin(pin);
122 return el->digitalReadBuffered(pin - el->getStart());
123 }
124}
125
127 if (pin == NO_PIN)
128 return 0; // LCOV_EXCL_LINE
129 else if (isNativePin(pin)) {
131 } else {
132 auto el = getIOElementOfPin(pin);
133 return el->analogReadBuffered(pin - el->getStart());
134 }
135 return 0;
136}
137
139 if (pin == NO_PIN)
140 return; // LCOV_EXCL_LINE
141 else if (isNativePin(pin)) {
142#ifndef ESP32
144#endif
145 } else {
146 auto el = getIOElementOfPin(pin);
147 el->analogWriteBuffered(pin - el->getStart(), val);
148 }
149}
150void analogWriteBuffered(pin_t pin, int val) {
151 analogWriteBuffered(pin, (analog_t)val);
152}
153
154void shiftOut(pin_t dataPin, pin_t clockPin, BitOrder_t bitOrder, uint8_t val) {
155 if (dataPin == NO_PIN || clockPin == NO_PIN)
156 return;
157 // Native version
158 if (isNativePin(dataPin) && isNativePin(clockPin)) {
159 ::shiftOut(arduino_pin_cast(dataPin), arduino_pin_cast(clockPin),
160 bitOrder, val);
161 }
162 // ExtIO version
163 else if (!isNativePin(dataPin) && !isNativePin(clockPin)) {
164 auto dataEl = getIOElementOfPin(dataPin);
165 auto dataPinN = dataPin - dataEl->getStart();
166 auto clockEl = getIOElementOfPin(clockPin);
167 auto clockPinN = clockPin - clockEl->getStart();
168 for (uint8_t i = 0; i < 8; i++) {
169 uint8_t mask = bitOrder == LSBFIRST ? (1 << i) : (1 << (7 - i));
170 dataEl->digitalWrite(dataPinN, (val & mask) ? HIGH : LOW);
171 clockEl->digitalWrite(clockPinN, HIGH);
172 clockEl->digitalWrite(clockPinN, LOW);
173 }
174 }
175 // Mixed version (slow)
176 else {
177 for (uint8_t i = 0; i < 8; i++) {
178 uint8_t mask = bitOrder == LSBFIRST ? (1 << i) : (1 << (7 - i));
179 digitalWrite(dataPin, (val & mask) ? HIGH : LOW);
180 digitalWrite(clockPin, HIGH);
181 digitalWrite(clockPin, LOW);
182 }
183 }
184}
185
186} // namespace ExtIO
187
189
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
ArduinoPin_t arduino_pin_cast(T t)
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.
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#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.
static DoublyLinkedList< ExtendedIOElement > & getAll()
Get the list of all Extended IO elements.
#define FATAL_ERROR(msg, errc)
Print the error message and error code, and stop the execution.
Definition: Error.hpp:60
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.
A namespace with alternatives to the standard Arduino IO functions that can be used with extended IO ...
static bool inRange(T target, T start, T end)
Type for storing pin numbers of Extended Input/Output elements.