Skip to content

Commit

Permalink
IOExpander: add writeAll() and toggle() API
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocketct committed Jun 9, 2022
1 parent a8f1623 commit 7cd0ae0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
This example code is in the public domain.
*/

#include <Arduino_MachineControl.h>
#include "Wire.h"
using namespace machinecontrol;
Expand Down Expand Up @@ -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)));
}
22 changes: 9 additions & 13 deletions src/utility/ioexpander/ArduinoIOExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand All @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion src/utility/ioexpander/ArduinoIOExpander.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7cd0ae0

Please sign in to comment.