Skip to content

Commit

Permalink
Replace pin_t by class for ADL pin functions
Browse files Browse the repository at this point in the history
and better type safety
  • Loading branch information
tttapa committed Nov 3, 2024
1 parent 53f3e4c commit 7bd5268
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include <Arduino_Helpers.h> // Include the Arduino Helpers library.
#include <AH/Hardware/ExtendedInputOutput/SPIShiftRegisterOut.hpp>

using namespace ExtIO; // Bring the ExtIO pin functions into your sketch

// Instantiate a shift register with the SPI slave select pin as latch pin, most
// significant bit first, and a total of 8 outputs.
SPIShiftRegisterOut<8> sreg {SPI, SS, MSBFIRST};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <Arduino_Helpers.h> // Include the Arduino Helpers library.
#include <AH/Hardware/ExtendedInputOutput/ShiftRegisterOut.hpp>

using namespace ExtIO; // Bring the ExtIO pin functions into your sketch

const pin_t latchPin = 10; // Pin connected to ST_CP of 74HC595
const pin_t dataPin = 11; // Pin connected to DS of 74HC595
const pin_t clockPin = 13; // Pin connected to SH_CP of 74HC595
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
#include <Arduino_Helpers.h> // Include the Arduino Helpers library.
#include <AH/Hardware/ExtendedInputOutput/SPIShiftRegisterOut.hpp>

using namespace ExtIO; // Bring the ExtIO pin functions into your sketch

// If you wired your LEDs as RGB
const uint8_t ShiftRegisterOutRGB::redBit = 0;
const uint8_t ShiftRegisterOutRGB::greenBit = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <Arduino_Helpers.h> // Include the Arduino Helpers library.
#include <AH/Hardware/ExtendedInputOutput/MAX7219.hpp>

using namespace ExtIO; // Bring the ExtIO pin functions into your sketch

// Instantiate a MAX7219 with the SPI slave select pin as latch pin
// There's just 1 MAX7219 in the chain, if you have more of them daisy-chained
// together, you can increase the template argument (between angled brackets)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

#include <AH/Hardware/ExtendedInputOutput/MCP23017.hpp>

using namespace ExtIO; // Use the extended input/output functions

using WireType = decltype(Wire); // The type of the I²C driver to use
MCP23017<WireType> mcp {
Wire, // The I²C driver to use
Expand Down
8 changes: 5 additions & 3 deletions examples/test/ExtIO-pin-test/ExtIO-pin-test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

#include <AH/Hardware/ExtendedInputOutput/ExtendedIOElement.hpp>

using namespace ExtIO;

void setup() {
using namespace ExtIO;
pinMode(13, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {}
void loop() {
pinMode(13, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
3 changes: 3 additions & 0 deletions src/AH/Hardware/Arduino-Hardware-Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ template <class T>
inline ArduinoPin_t arduino_pin_cast(T t) {
return static_cast<ArduinoPin_t>(t);
}
inline ArduinoPin_t arduino_pin_cast(pin_t t) {
return t.pin;
}
END_AH_NAMESPACE
2 changes: 2 additions & 0 deletions src/AH/Hardware/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Button {
* will be enabled when `begin` is called.
*/
Button(pin_t pin);
/// @copydoc Button(pin_t)
Button(ArduinoPin_t pin) : Button(pin_t(pin)) {}

/// @brief Initialize (enable the internal pull-up resistor).
void begin();
Expand Down
4 changes: 2 additions & 2 deletions src/AH/Hardware/ExtendedInputOutput/AnalogMultiplex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void AnalogMultiplex<N>::pinModeBuffered(pin_t, PinMode_t mode) {

template <uint8_t N>
PinStatus_t AnalogMultiplex<N>::digitalRead(pin_t pin) {
prepareReading(pin);
prepareReading(pin.pin);
PinStatus_t result = ExtIO::digitalRead(analogPin);
afterReading();
return result;
Expand All @@ -219,7 +219,7 @@ PinStatus_t AnalogMultiplex<N>::digitalReadBuffered(pin_t pin) {

template <uint8_t N>
analog_t AnalogMultiplex<N>::analogRead(pin_t pin) {
prepareReading(pin);
prepareReading(pin.pin);
if (discardFirstReading_)
(void)ExtIO::analogRead(analogPin); // Discard first reading
analog_t result = ExtIO::analogRead(analogPin);
Expand Down
5 changes: 3 additions & 2 deletions src/AH/Hardware/ExtendedInputOutput/ExtendedIOElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ BEGIN_AH_NAMESPACE

ExtendedIOElement::ExtendedIOElement(pin_t length)
: length(length), start(offset), end(offset + length) {
if (end < start)
if (end > NO_PIN)
FATAL_ERROR(F("ExtIO ran out of pin numbers. "
"Dynamically creating new ExtendedIOElements is not "
"recommended."),
Expand All @@ -28,7 +28,8 @@ void ExtendedIOElement::updateAllBufferedInputs() {

pin_t ExtendedIOElement::pin(pin_t p) const {
if (p >= length) {
static_assert(std::is_unsigned<pin_t>::value,
static_assert(!std::is_integral<pin_t>::value ||
std::is_unsigned<pin_t>::value,
"Error: pin_t should be an unsigned integer type");
ERROR(F("Error: the pin number (")
<< p
Expand Down
28 changes: 12 additions & 16 deletions src/AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@ ExtendedIOElement *getIOElementOfPin(pin_t pin) {
return el;
}

template <class T>
ArduinoPin_t arduino_pin_cast(T t) {
return static_cast<ArduinoPin_t>(t);
}

void pinMode(pin_t pin, PinMode_t mode) {
if (pin == NO_PIN)
return; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
::pinMode(pin, mode);
::pinMode(arduino_pin_cast(pin), mode);
} else {
auto el = getIOElementOfPin(pin);
el->pinMode(pin - el->getStart(), mode);
Expand All @@ -52,7 +47,7 @@ void digitalWrite(pin_t pin, PinStatus_t val) {
if (pin == NO_PIN)
return; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
::digitalWrite(pin, val);
::digitalWrite(arduino_pin_cast(pin), val);
} else {
auto el = getIOElementOfPin(pin);
el->digitalWrite(pin - el->getStart(), val);
Expand All @@ -66,7 +61,7 @@ PinStatus_t digitalRead(pin_t pin) {
if (pin == NO_PIN)
return LOW; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
return ::digitalRead(pin);
return ::digitalRead(arduino_pin_cast(pin));
} else {
auto el = getIOElementOfPin(pin);
return el->digitalRead(pin - el->getStart());
Expand All @@ -80,7 +75,7 @@ analog_t analogRead(pin_t pin) {
if (pin == NO_PIN)
return 0; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
return ::analogRead(pin);
return ::analogRead(arduino_pin_cast(pin));
} else {
auto el = getIOElementOfPin(pin);
return el->analogRead(pin - el->getStart());
Expand All @@ -93,7 +88,7 @@ void analogWrite(pin_t pin, analog_t val) {
return; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
#ifndef ESP32
::analogWrite(pin, val);
::analogWrite(arduino_pin_cast(pin), val);
#endif
} else {
auto el = getIOElementOfPin(pin);
Expand All @@ -114,7 +109,7 @@ void pinModeBuffered(pin_t pin, PinMode_t mode) {
if (pin == NO_PIN)
return; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
::pinMode(pin, mode);
::pinMode(arduino_pin_cast(pin), mode);
} else {
auto el = getIOElementOfPin(pin);
el->pinModeBuffered(pin - el->getStart(), mode);
Expand All @@ -125,7 +120,7 @@ void digitalWriteBuffered(pin_t pin, PinStatus_t val) {
if (pin == NO_PIN)
return; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
::digitalWrite(pin, val);
::digitalWrite(arduino_pin_cast(pin), val);
} else {
auto el = getIOElementOfPin(pin);
el->digitalWriteBuffered(pin - el->getStart(), val);
Expand All @@ -136,7 +131,7 @@ PinStatus_t digitalReadBuffered(pin_t pin) {
if (pin == NO_PIN)
return LOW; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
return ::digitalRead(pin);
return ::digitalRead(arduino_pin_cast(pin));
} else {
auto el = getIOElementOfPin(pin);
return el->digitalReadBuffered(pin - el->getStart());
Expand All @@ -147,7 +142,7 @@ analog_t analogReadBuffered(pin_t pin) {
if (pin == NO_PIN)
return 0; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
return ::analogRead(pin);
return ::analogRead(arduino_pin_cast(pin));
} else {
auto el = getIOElementOfPin(pin);
return el->analogReadBuffered(pin - el->getStart());
Expand All @@ -160,7 +155,7 @@ void analogWriteBuffered(pin_t pin, analog_t val) {
return; // LCOV_EXCL_LINE
else if (isNativePin(pin)) {
#ifndef ESP32
::analogWrite(pin, val);
::analogWrite(arduino_pin_cast(pin), val);
#endif
} else {
auto el = getIOElementOfPin(pin);
Expand All @@ -176,7 +171,8 @@ void shiftOut(pin_t dataPin, pin_t clockPin, BitOrder_t bitOrder, uint8_t val) {
return;
// Native version
if (isNativePin(dataPin) && isNativePin(clockPin)) {
::shiftOut((int)dataPin, (int)clockPin, bitOrder, val);
::shiftOut(arduino_pin_cast(dataPin), arduino_pin_cast(clockPin),
bitOrder, val);
}
// ExtIO version
else if (!isNativePin(dataPin) && !isNativePin(clockPin)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ExtIO {
/// Check if the given pin number is a real Arduino pin number, and not an ExtIO
/// pin number.
inline bool isNativePin(pin_t pin) {
return pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS;
return pin.pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/AH/Hardware/ExtendedInputOutput/MAX7219.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class MAX7219 : public MAX7219_Base<SPIDriver>,
};

static IndexMask pin2index(pin_t pin) {
uint8_t row = pin / 8;
uint8_t col = pin % 8;
uint8_t row = pin.pin / 8;
uint8_t col = pin.pin % 8;
uint8_t rowgrp = row % 8;
uint8_t rowmask = 1 << rowgrp;
uint8_t colmask = 1 << col;
Expand Down
28 changes: 14 additions & 14 deletions src/AH/Hardware/ExtendedInputOutput/MCP23017.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,36 @@ MCP23017<WireType>::MCP23017(WireType &wire, uint8_t addressOffset,
template <class WireType>
void MCP23017<WireType>::pinModeBuffered(pin_t pin, PinMode_t mode) {
if (mode == INPUT) {
pinModesDirty |= bufferedPinModes.get(pin) == 0;
pullupsDirty |= bufferedPullups.get(pin) == 1;
bufferedPinModes.set(pin);
bufferedPullups.clear(pin);
pinModesDirty |= bufferedPinModes.get(pin.pin) == 0;
pullupsDirty |= bufferedPullups.get(pin.pin) == 1;
bufferedPinModes.set(pin.pin);
bufferedPullups.clear(pin.pin);
} else if (mode == OUTPUT) {
pinModesDirty |= bufferedPinModes.get(pin) == 1;
bufferedPinModes.clear(pin);
pinModesDirty |= bufferedPinModes.get(pin.pin) == 1;
bufferedPinModes.clear(pin.pin);
} else if (mode == INPUT_PULLUP) {
pinModesDirty |= bufferedPinModes.get(pin) == 0;
pullupsDirty |= bufferedPullups.get(pin) == 0;
bufferedPinModes.set(pin);
bufferedPullups.set(pin);
pinModesDirty |= bufferedPinModes.get(pin.pin) == 0;
pullupsDirty |= bufferedPullups.get(pin.pin) == 0;
bufferedPinModes.set(pin.pin);
bufferedPullups.set(pin.pin);
}
}

template <class WireType>
void MCP23017<WireType>::digitalWriteBuffered(pin_t pin, PinStatus_t status) {
bool boolstate = status == HIGH;
outputsDirty |= bufferedOutputs.get(pin) != boolstate;
bufferedOutputs.set(pin, boolstate);
outputsDirty |= bufferedOutputs.get(pin.pin) != boolstate;
bufferedOutputs.set(pin.pin, boolstate);
}

template <class WireType>
PinStatus_t MCP23017<WireType>::digitalReadBuffered(pin_t pin) {
return bufferedInputs.get(pin) ? HIGH : LOW;
return bufferedInputs.get(pin.pin) ? HIGH : LOW;
}

template <class WireType>
analog_t MCP23017<WireType>::analogReadBuffered(pin_t pin) {
return bufferedInputs.get(pin) ? 1023 : 0;
return bufferedInputs.get(pin.pin) ? 1023 : 0;
}

template <class WireType>
Expand Down
6 changes: 3 additions & 3 deletions src/AH/Hardware/ExtendedInputOutput/ShiftRegisterOutBase.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ ShiftRegisterOutBase<N>::ShiftRegisterOutBase(pin_t latchPin,

template <uint16_t N>
void ShiftRegisterOutBase<N>::digitalWrite(pin_t pin, PinStatus_t val) {
buffer.set(pin, val);
buffer.set(pin.pin, val);
dirty = true;
this->updateBufferedOutputs(); // TODO: should I always update?
}

template <uint16_t N>
void ShiftRegisterOutBase<N>::digitalWriteBuffered(pin_t pin, PinStatus_t val) {
buffer.set(pin, val);
buffer.set(pin.pin, val);
dirty = true;
}

template <uint16_t N>
PinStatus_t ShiftRegisterOutBase<N>::digitalRead(pin_t pin) {
return buffer.get(pin) ? HIGH : LOW;
return buffer.get(pin.pin) ? HIGH : LOW;
}

template <uint16_t N>
Expand Down
11 changes: 11 additions & 0 deletions src/AH/Hardware/FilteredAnalog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class GenericFilteredAnalog {
filter(increaseBitDepth<ADC_BITS + IncRes, Precision, AnalogType,
AnalogType>(initial)) {}

/// @copydoc GenericFilteredAnalog(pin_t,MappingFunction,AnalogType)
GenericFilteredAnalog(ArduinoPin_t analogPin, MappingFunction mapFn,
AnalogType initial = 0)
: GenericFilteredAnalog(pin_t(analogPin),
std::forward<MappingFunction>(mapFn), initial) {
}

/**
* @brief Reset the filter to the given value.
*
Expand Down Expand Up @@ -280,6 +287,10 @@ class FilteredAnalog
FilterShiftFactor, FilterType, AnalogType,
IncRes>(analogPin, nullptr, initial) {}

/// @copydoc FilteredAnalog(pin_t,AnalogType)
FilteredAnalog(ArduinoPin_t analogPin, AnalogType initial = 0)
: FilteredAnalog(pin_t(analogPin), initial) {}

/**
* @brief Construct a new FilteredAnalog object.
*
Expand Down
Loading

0 comments on commit 7bd5268

Please sign in to comment.