From 7cd0ae0bd79cdd251d755e8e10370cf253d86f2b Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Thu, 9 Jun 2022 10:57:53 +0200 Subject: [PATCH] IOExpander: add writeAll() and toggle() API --- .../CombinedIOExpander/CombinedIOExpander.ino | 63 ++++++++----------- .../GPIO_programmable/GPIO_programmable.ino | 61 +++++++++--------- src/utility/ioexpander/ArduinoIOExpander.cpp | 22 +++---- src/utility/ioexpander/ArduinoIOExpander.h | 21 ++++++- 4 files changed, 82 insertions(+), 85 deletions(-) diff --git a/examples/Digital_programmable/CombinedIOExpander/CombinedIOExpander.ino b/examples/Digital_programmable/CombinedIOExpander/CombinedIOExpander.ino index c76ad3d..9615050 100644 --- a/examples/Digital_programmable/CombinedIOExpander/CombinedIOExpander.ino +++ b/examples/Digital_programmable/CombinedIOExpander/CombinedIOExpander.ino @@ -53,14 +53,14 @@ void loop() { Serial.println(); // Write the status value to On to all the Output Pins - setAll(SWITCH_ON); + digital_programmables.writeAll(SWITCH_ON_ALL); // Reads from all Input Pins readAll(); delay(1000); // Write the status value to Off all to all the Output Pins - setAll(SWITCH_OFF); + digital_programmables.writeAll(SWITCH_OFF_ALL); // Reads from all Input Pins readAll(); @@ -69,43 +69,30 @@ void loop() { } -void setAll(PinStatus status) { - // Write the status value to each Pin - digital_programmables.set(IO_WRITE_CH_PIN_00, status); - digital_programmables.set(IO_WRITE_CH_PIN_01, status); - digital_programmables.set(IO_WRITE_CH_PIN_02, status); - digital_programmables.set(IO_WRITE_CH_PIN_03, status); - digital_programmables.set(IO_WRITE_CH_PIN_04, status); - digital_programmables.set(IO_WRITE_CH_PIN_05, status); - digital_programmables.set(IO_WRITE_CH_PIN_06, status); - digital_programmables.set(IO_WRITE_CH_PIN_07, status); - digital_programmables.set(IO_WRITE_CH_PIN_08, status); - digital_programmables.set(IO_WRITE_CH_PIN_09, status); - digital_programmables.set(IO_WRITE_CH_PIN_10, status); - digital_programmables.set(IO_WRITE_CH_PIN_11, status); -} void readAll() { - // Reads from input pins. This API returns -1 if you try to read from a write channel. - Serial.println("IO Pin 00: " + String(digital_programmables.read(IO_READ_CH_PIN_00))); - Serial.println("IO Pin 01: " + String(digital_programmables.read(IO_READ_CH_PIN_01))); - Serial.println("IO Pin 02: " + String(digital_programmables.read(IO_READ_CH_PIN_02))); - Serial.println("IO Pin 03: " + String(digital_programmables.read(IO_READ_CH_PIN_03))); - Serial.println("IO Pin 04: " + String(digital_programmables.read(IO_READ_CH_PIN_04))); - Serial.println("IO Pin 05: " + String(digital_programmables.read(IO_READ_CH_PIN_05))); - Serial.println("IO Pin 06: " + String(digital_programmables.read(IO_READ_CH_PIN_06))); - Serial.println("IO Pin 07: " + String(digital_programmables.read(IO_READ_CH_PIN_07))); - Serial.println("IO Pin 08: " + String(digital_programmables.read(IO_READ_CH_PIN_08))); - Serial.println("IO Pin 09: " + String(digital_programmables.read(IO_READ_CH_PIN_09))); - Serial.println("IO Pin 10: " + String(digital_programmables.read(IO_READ_CH_PIN_10))); - Serial.println("IO Pin 11: " + String(digital_programmables.read(IO_READ_CH_PIN_11))); + uint32_t inputs = digital_programmables.readAll(); + Serial.println("CH00: " + String((inputs & (1 << IO_READ_CH_PIN_00)) >> IO_READ_CH_PIN_00)); + Serial.println("CH01: " + String((inputs & (1 << IO_READ_CH_PIN_01)) >> IO_READ_CH_PIN_01)); + Serial.println("CH02: " + String((inputs & (1 << IO_READ_CH_PIN_02)) >> IO_READ_CH_PIN_02)); + Serial.println("CH03: " + String((inputs & (1 << IO_READ_CH_PIN_03)) >> IO_READ_CH_PIN_03)); + Serial.println("CH04: " + String((inputs & (1 << IO_READ_CH_PIN_04)) >> IO_READ_CH_PIN_04)); + Serial.println("CH05: " + String((inputs & (1 << IO_READ_CH_PIN_05)) >> IO_READ_CH_PIN_05)); + Serial.println("CH06: " + String((inputs & (1 << IO_READ_CH_PIN_06)) >> IO_READ_CH_PIN_06)); + Serial.println("CH07: " + String((inputs & (1 << IO_READ_CH_PIN_07)) >> IO_READ_CH_PIN_07)); + Serial.println("CH08: " + String((inputs & (1 << IO_READ_CH_PIN_08)) >> IO_READ_CH_PIN_08)); + Serial.println("CH09: " + String((inputs & (1 << IO_READ_CH_PIN_09)) >> IO_READ_CH_PIN_09)); + Serial.println("CH10: " + String((inputs & (1 << IO_READ_CH_PIN_10)) >> IO_READ_CH_PIN_10)); + Serial.println("CH11: " + String((inputs & (1 << IO_READ_CH_PIN_11)) >> IO_READ_CH_PIN_11)); + Serial.println(); + inputs = digital_inputs.readAll(); + Serial.println("CH00: " + String((inputs & (1 << DIN_READ_CH_PIN_00)) >> DIN_READ_CH_PIN_00)); + Serial.println("CH01: " + String((inputs & (1 << DIN_READ_CH_PIN_01)) >> DIN_READ_CH_PIN_01)); + Serial.println("CH02: " + String((inputs & (1 << DIN_READ_CH_PIN_02)) >> DIN_READ_CH_PIN_02)); + Serial.println("CH03: " + String((inputs & (1 << DIN_READ_CH_PIN_03)) >> DIN_READ_CH_PIN_03)); + Serial.println("CH04: " + String((inputs & (1 << DIN_READ_CH_PIN_04)) >> DIN_READ_CH_PIN_04)); + Serial.println("CH05: " + String((inputs & (1 << DIN_READ_CH_PIN_05)) >> DIN_READ_CH_PIN_05)); + Serial.println("CH06: " + String((inputs & (1 << DIN_READ_CH_PIN_06)) >> DIN_READ_CH_PIN_06)); + Serial.println("CH07: " + String((inputs & (1 << DIN_READ_CH_PIN_07)) >> DIN_READ_CH_PIN_07)); Serial.println(); - Serial.println("DIN Pin 00: " + String(digital_inputs.read(DIN_READ_CH_PIN_00))); - Serial.println("DIN Pin 01: " + String(digital_inputs.read(DIN_READ_CH_PIN_01))); - Serial.println("DIN Pin 02: " + String(digital_inputs.read(DIN_READ_CH_PIN_02))); - Serial.println("DIN Pin 03: " + String(digital_inputs.read(DIN_READ_CH_PIN_03))); - Serial.println("DIN Pin 04: " + String(digital_inputs.read(DIN_READ_CH_PIN_04))); - Serial.println("DIN Pin 05: " + String(digital_inputs.read(DIN_READ_CH_PIN_05))); - Serial.println("DIN Pin 06: " + String(digital_inputs.read(DIN_READ_CH_PIN_06))); - Serial.println("DIN Pin 07: " + String(digital_inputs.read(DIN_READ_CH_PIN_07))); } diff --git a/examples/Digital_programmable/GPIO_programmable/GPIO_programmable.ino b/examples/Digital_programmable/GPIO_programmable/GPIO_programmable.ino index ce9558a..cf80f0a 100644 --- a/examples/Digital_programmable/GPIO_programmable/GPIO_programmable.ino +++ b/examples/Digital_programmable/GPIO_programmable/GPIO_programmable.ino @@ -12,7 +12,7 @@ This example code is in the public domain. */ - + #include #include "Wire.h" using namespace machinecontrol; @@ -42,52 +42,47 @@ void loop() { digital_programmables.set(IO_WRITE_CH_PIN_03, SWITCH_OFF); delay(1000); + Serial.println(); + // Sets all the status Pins Values to On in one single operation + uint32_t status = ON_VALUE_PIN_10 | ON_VALUE_PIN_08 | ON_VALUE_PIN_06 | ON_VALUE_PIN_04 | ON_VALUE_PIN_02 | ON_VALUE_PIN_00; + digital_programmables.writeAll(status); + delay(1000); + + // Toggles the actual status values of all digital programmables Pins + digital_programmables.toggle(); + delay(1000); + Serial.println(); // Write the status value to On to all the Output Pins - setAll(SWITCH_ON); + digital_programmables.writeAll(SWITCH_ON_ALL); // Reads from all Input Pins readAll(); delay(1000); // Write the status value to Off all to all the Output Pins - setAll(SWITCH_OFF); + digital_programmables.writeAll(SWITCH_OFF_ALL); // Reads from all Input Pins readAll(); Serial.println(); delay(1000); - } -void setAll(PinStatus status) { - // Write the status value to each Pin - digital_programmables.set(IO_WRITE_CH_PIN_00, status); - digital_programmables.set(IO_WRITE_CH_PIN_01, status); - digital_programmables.set(IO_WRITE_CH_PIN_02, status); - digital_programmables.set(IO_WRITE_CH_PIN_03, status); - digital_programmables.set(IO_WRITE_CH_PIN_04, status); - digital_programmables.set(IO_WRITE_CH_PIN_05, status); - digital_programmables.set(IO_WRITE_CH_PIN_06, status); - digital_programmables.set(IO_WRITE_CH_PIN_07, status); - digital_programmables.set(IO_WRITE_CH_PIN_08, status); - digital_programmables.set(IO_WRITE_CH_PIN_09, status); - digital_programmables.set(IO_WRITE_CH_PIN_10, status); - digital_programmables.set(IO_WRITE_CH_PIN_11, status); +uint8_t readAll() { + uint32_t inputs = digital_programmables.readAll(); + Serial.println("CH00: " + String((inputs & (1 << IO_READ_CH_PIN_00)) >> IO_READ_CH_PIN_00)); + Serial.println("CH01: " + String((inputs & (1 << IO_READ_CH_PIN_01)) >> IO_READ_CH_PIN_01)); + Serial.println("CH02: " + String((inputs & (1 << IO_READ_CH_PIN_02)) >> IO_READ_CH_PIN_02)); + Serial.println("CH03: " + String((inputs & (1 << IO_READ_CH_PIN_03)) >> IO_READ_CH_PIN_03)); + Serial.println("CH04: " + String((inputs & (1 << IO_READ_CH_PIN_04)) >> IO_READ_CH_PIN_04)); + Serial.println("CH05: " + String((inputs & (1 << IO_READ_CH_PIN_05)) >> IO_READ_CH_PIN_05)); + Serial.println("CH06: " + String((inputs & (1 << IO_READ_CH_PIN_06)) >> IO_READ_CH_PIN_06)); + Serial.println("CH07: " + String((inputs & (1 << IO_READ_CH_PIN_07)) >> IO_READ_CH_PIN_07)); + Serial.println("CH08: " + String((inputs & (1 << IO_READ_CH_PIN_08)) >> IO_READ_CH_PIN_08)); + Serial.println("CH09: " + String((inputs & (1 << IO_READ_CH_PIN_09)) >> IO_READ_CH_PIN_09)); + Serial.println("CH10: " + String((inputs & (1 << IO_READ_CH_PIN_10)) >> IO_READ_CH_PIN_10)); + Serial.println("CH11: " + String((inputs & (1 << IO_READ_CH_PIN_11)) >> IO_READ_CH_PIN_11)); + Serial.println(); } -void readAll() { - // Reads from input pins. This API returns -1 if you try to read from a write channel. - Serial.println("Pin 00: " + String(digital_programmables.read(IO_READ_CH_PIN_00))); - Serial.println("Pin 01: " + String(digital_programmables.read(IO_READ_CH_PIN_01))); - Serial.println("Pin 02: " + String(digital_programmables.read(IO_READ_CH_PIN_02))); - Serial.println("Pin 03: " + String(digital_programmables.read(IO_READ_CH_PIN_03))); - Serial.println("Pin 04: " + String(digital_programmables.read(IO_READ_CH_PIN_04))); - Serial.println("Pin 05: " + String(digital_programmables.read(IO_READ_CH_PIN_05))); - Serial.println("Pin 06: " + String(digital_programmables.read(IO_READ_CH_PIN_06))); - Serial.println("Pin 07: " + String(digital_programmables.read(IO_READ_CH_PIN_07))); - Serial.println("Pin 08: " + String(digital_programmables.read(IO_READ_CH_PIN_08))); - Serial.println("Pin 09: " + String(digital_programmables.read(IO_READ_CH_PIN_09))); - Serial.println("Pin 10: " + String(digital_programmables.read(IO_READ_CH_PIN_10))); - Serial.println("Pin 11: " + String(digital_programmables.read(IO_READ_CH_PIN_11))); -} diff --git a/src/utility/ioexpander/ArduinoIOExpander.cpp b/src/utility/ioexpander/ArduinoIOExpander.cpp index 63880d3..1aacf11 100644 --- a/src/utility/ioexpander/ArduinoIOExpander.cpp +++ b/src/utility/ioexpander/ArduinoIOExpander.cpp @@ -83,6 +83,10 @@ int ArduinoIOExpanderClass::read(int pin) return -1; } +void ArduinoIOExpanderClass::writeAll(uint32_t banks) { + _tca.writeAll(banks & 0xFF, (banks >> 8) & 0xFF, 0x00); +} + uint32_t ArduinoIOExpanderClass::readAll() { uint8_t banks[3]; @@ -91,11 +95,14 @@ uint32_t ArduinoIOExpanderClass::readAll() } +void ArduinoIOExpanderClass::toggle(){ + writeAll(~(readAll())); +} + void ArduinoIOExpanderClass::initPins() { if (_tca.getAddress() == IO_ADD) { - PinStatus status = SWITCH_OFF; pinMode(IO_WRITE_CH_PIN_00, OUTPUT); pinMode(IO_WRITE_CH_PIN_01, OUTPUT); pinMode(IO_WRITE_CH_PIN_02, OUTPUT); @@ -121,18 +128,7 @@ void ArduinoIOExpanderClass::initPins() pinMode(IO_READ_CH_PIN_10, INPUT); pinMode(IO_READ_CH_PIN_11, INPUT); - set(IO_WRITE_CH_PIN_00, status); - set(IO_WRITE_CH_PIN_01, status); - set(IO_WRITE_CH_PIN_02, status); - set(IO_WRITE_CH_PIN_03, status); - set(IO_WRITE_CH_PIN_04, status); - set(IO_WRITE_CH_PIN_05, status); - set(IO_WRITE_CH_PIN_06, status); - set(IO_WRITE_CH_PIN_07, status); - set(IO_WRITE_CH_PIN_08, status); - set(IO_WRITE_CH_PIN_09, status); - set(IO_WRITE_CH_PIN_10, status); - set(IO_WRITE_CH_PIN_11, status); + writeAll(SWITCH_OFF_ALL); } else { pinMode(DIN_READ_CH_PIN_00, INPUT); pinMode(DIN_READ_CH_PIN_01, INPUT); diff --git a/src/utility/ioexpander/ArduinoIOExpander.h b/src/utility/ioexpander/ArduinoIOExpander.h index df02a89..e7cd402 100644 --- a/src/utility/ioexpander/ArduinoIOExpander.h +++ b/src/utility/ioexpander/ArduinoIOExpander.h @@ -25,6 +25,24 @@ #define SWITCH_ON HIGH #define SWITCH_OFF LOW +#define SWITCH_ON_ALL 0x0000FFFF +#define SWITCH_OFF_ALL 0x00000000 + +enum { + ON_VALUE_PIN_00 = 0x01, + ON_VALUE_PIN_01 = 0x02, + ON_VALUE_PIN_02 = 0x04, + ON_VALUE_PIN_03 = 0x08, + ON_VALUE_PIN_04 = 0x80, + ON_VALUE_PIN_05 = 0x40, + ON_VALUE_PIN_06 = 0x20, + ON_VALUE_PIN_07 = 0x10, + ON_VALUE_PIN_08 = 0x100, + ON_VALUE_PIN_09 = 0x200, + ON_VALUE_PIN_10 = 0x400, + ON_VALUE_PIN_11 = 0x800, +}; + enum { IO_WRITE_CH_PIN_00 = TCA6424A_P00, IO_WRITE_CH_PIN_01 = TCA6424A_P01, @@ -77,9 +95,10 @@ class ArduinoIOExpanderClass { void setAddress(uint8_t address); bool set(int pin, PinStatus status); bool set(int pin, int status) { return set( pin, (PinStatus)status); }; - + void writeAll(uint32_t banks); int read(int pin); uint32_t readAll(); + void toggle(); bool pinMode(int pin, PinMode direction); private: