Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 475746d

Browse files
authored
Merge pull request #89 from Rocketct/master
IOExpander: add writeAll() and toggle() API
2 parents b655bd2 + 7cd0ae0 commit 475746d

File tree

4 files changed

+82
-85
lines changed

4 files changed

+82
-85
lines changed

examples/Digital_programmable/CombinedIOExpander/CombinedIOExpander.ino

+25-38
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ void loop() {
5353

5454
Serial.println();
5555
// Write the status value to On to all the Output Pins
56-
setAll(SWITCH_ON);
56+
digital_programmables.writeAll(SWITCH_ON_ALL);
5757

5858
// Reads from all Input Pins
5959
readAll();
6060
delay(1000);
6161

6262
// Write the status value to Off all to all the Output Pins
63-
setAll(SWITCH_OFF);
63+
digital_programmables.writeAll(SWITCH_OFF_ALL);
6464

6565
// Reads from all Input Pins
6666
readAll();
@@ -69,43 +69,30 @@ void loop() {
6969

7070
}
7171

72-
void setAll(PinStatus status) {
73-
// Write the status value to each Pin
74-
digital_programmables.set(IO_WRITE_CH_PIN_00, status);
75-
digital_programmables.set(IO_WRITE_CH_PIN_01, status);
76-
digital_programmables.set(IO_WRITE_CH_PIN_02, status);
77-
digital_programmables.set(IO_WRITE_CH_PIN_03, status);
78-
digital_programmables.set(IO_WRITE_CH_PIN_04, status);
79-
digital_programmables.set(IO_WRITE_CH_PIN_05, status);
80-
digital_programmables.set(IO_WRITE_CH_PIN_06, status);
81-
digital_programmables.set(IO_WRITE_CH_PIN_07, status);
82-
digital_programmables.set(IO_WRITE_CH_PIN_08, status);
83-
digital_programmables.set(IO_WRITE_CH_PIN_09, status);
84-
digital_programmables.set(IO_WRITE_CH_PIN_10, status);
85-
digital_programmables.set(IO_WRITE_CH_PIN_11, status);
86-
}
8772

8873
void readAll() {
89-
// Reads from input pins. This API returns -1 if you try to read from a write channel.
90-
Serial.println("IO Pin 00: " + String(digital_programmables.read(IO_READ_CH_PIN_00)));
91-
Serial.println("IO Pin 01: " + String(digital_programmables.read(IO_READ_CH_PIN_01)));
92-
Serial.println("IO Pin 02: " + String(digital_programmables.read(IO_READ_CH_PIN_02)));
93-
Serial.println("IO Pin 03: " + String(digital_programmables.read(IO_READ_CH_PIN_03)));
94-
Serial.println("IO Pin 04: " + String(digital_programmables.read(IO_READ_CH_PIN_04)));
95-
Serial.println("IO Pin 05: " + String(digital_programmables.read(IO_READ_CH_PIN_05)));
96-
Serial.println("IO Pin 06: " + String(digital_programmables.read(IO_READ_CH_PIN_06)));
97-
Serial.println("IO Pin 07: " + String(digital_programmables.read(IO_READ_CH_PIN_07)));
98-
Serial.println("IO Pin 08: " + String(digital_programmables.read(IO_READ_CH_PIN_08)));
99-
Serial.println("IO Pin 09: " + String(digital_programmables.read(IO_READ_CH_PIN_09)));
100-
Serial.println("IO Pin 10: " + String(digital_programmables.read(IO_READ_CH_PIN_10)));
101-
Serial.println("IO Pin 11: " + String(digital_programmables.read(IO_READ_CH_PIN_11)));
74+
uint32_t inputs = digital_programmables.readAll();
75+
Serial.println("CH00: " + String((inputs & (1 << IO_READ_CH_PIN_00)) >> IO_READ_CH_PIN_00));
76+
Serial.println("CH01: " + String((inputs & (1 << IO_READ_CH_PIN_01)) >> IO_READ_CH_PIN_01));
77+
Serial.println("CH02: " + String((inputs & (1 << IO_READ_CH_PIN_02)) >> IO_READ_CH_PIN_02));
78+
Serial.println("CH03: " + String((inputs & (1 << IO_READ_CH_PIN_03)) >> IO_READ_CH_PIN_03));
79+
Serial.println("CH04: " + String((inputs & (1 << IO_READ_CH_PIN_04)) >> IO_READ_CH_PIN_04));
80+
Serial.println("CH05: " + String((inputs & (1 << IO_READ_CH_PIN_05)) >> IO_READ_CH_PIN_05));
81+
Serial.println("CH06: " + String((inputs & (1 << IO_READ_CH_PIN_06)) >> IO_READ_CH_PIN_06));
82+
Serial.println("CH07: " + String((inputs & (1 << IO_READ_CH_PIN_07)) >> IO_READ_CH_PIN_07));
83+
Serial.println("CH08: " + String((inputs & (1 << IO_READ_CH_PIN_08)) >> IO_READ_CH_PIN_08));
84+
Serial.println("CH09: " + String((inputs & (1 << IO_READ_CH_PIN_09)) >> IO_READ_CH_PIN_09));
85+
Serial.println("CH10: " + String((inputs & (1 << IO_READ_CH_PIN_10)) >> IO_READ_CH_PIN_10));
86+
Serial.println("CH11: " + String((inputs & (1 << IO_READ_CH_PIN_11)) >> IO_READ_CH_PIN_11));
87+
Serial.println();
88+
inputs = digital_inputs.readAll();
89+
Serial.println("CH00: " + String((inputs & (1 << DIN_READ_CH_PIN_00)) >> DIN_READ_CH_PIN_00));
90+
Serial.println("CH01: " + String((inputs & (1 << DIN_READ_CH_PIN_01)) >> DIN_READ_CH_PIN_01));
91+
Serial.println("CH02: " + String((inputs & (1 << DIN_READ_CH_PIN_02)) >> DIN_READ_CH_PIN_02));
92+
Serial.println("CH03: " + String((inputs & (1 << DIN_READ_CH_PIN_03)) >> DIN_READ_CH_PIN_03));
93+
Serial.println("CH04: " + String((inputs & (1 << DIN_READ_CH_PIN_04)) >> DIN_READ_CH_PIN_04));
94+
Serial.println("CH05: " + String((inputs & (1 << DIN_READ_CH_PIN_05)) >> DIN_READ_CH_PIN_05));
95+
Serial.println("CH06: " + String((inputs & (1 << DIN_READ_CH_PIN_06)) >> DIN_READ_CH_PIN_06));
96+
Serial.println("CH07: " + String((inputs & (1 << DIN_READ_CH_PIN_07)) >> DIN_READ_CH_PIN_07));
10297
Serial.println();
103-
Serial.println("DIN Pin 00: " + String(digital_inputs.read(DIN_READ_CH_PIN_00)));
104-
Serial.println("DIN Pin 01: " + String(digital_inputs.read(DIN_READ_CH_PIN_01)));
105-
Serial.println("DIN Pin 02: " + String(digital_inputs.read(DIN_READ_CH_PIN_02)));
106-
Serial.println("DIN Pin 03: " + String(digital_inputs.read(DIN_READ_CH_PIN_03)));
107-
Serial.println("DIN Pin 04: " + String(digital_inputs.read(DIN_READ_CH_PIN_04)));
108-
Serial.println("DIN Pin 05: " + String(digital_inputs.read(DIN_READ_CH_PIN_05)));
109-
Serial.println("DIN Pin 06: " + String(digital_inputs.read(DIN_READ_CH_PIN_06)));
110-
Serial.println("DIN Pin 07: " + String(digital_inputs.read(DIN_READ_CH_PIN_07)));
11198
}

examples/Digital_programmable/GPIO_programmable/GPIO_programmable.ino

+28-33
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This example code is in the public domain.
1414
*/
15-
15+
1616
#include <Arduino_MachineControl.h>
1717
#include "Wire.h"
1818
using namespace machinecontrol;
@@ -42,52 +42,47 @@ void loop() {
4242
digital_programmables.set(IO_WRITE_CH_PIN_03, SWITCH_OFF);
4343
delay(1000);
4444

45+
Serial.println();
46+
// Sets all the status Pins Values to On in one single operation
47+
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;
48+
digital_programmables.writeAll(status);
49+
delay(1000);
50+
51+
// Toggles the actual status values of all digital programmables Pins
52+
digital_programmables.toggle();
53+
delay(1000);
54+
4555
Serial.println();
4656
// Write the status value to On to all the Output Pins
47-
setAll(SWITCH_ON);
57+
digital_programmables.writeAll(SWITCH_ON_ALL);
4858

4959
// Reads from all Input Pins
5060
readAll();
5161
delay(1000);
5262

5363
// Write the status value to Off all to all the Output Pins
54-
setAll(SWITCH_OFF);
64+
digital_programmables.writeAll(SWITCH_OFF_ALL);
5565

5666
// Reads from all Input Pins
5767
readAll();
5868
Serial.println();
5969
delay(1000);
60-
6170
}
6271

63-
void setAll(PinStatus status) {
64-
// Write the status value to each Pin
65-
digital_programmables.set(IO_WRITE_CH_PIN_00, status);
66-
digital_programmables.set(IO_WRITE_CH_PIN_01, status);
67-
digital_programmables.set(IO_WRITE_CH_PIN_02, status);
68-
digital_programmables.set(IO_WRITE_CH_PIN_03, status);
69-
digital_programmables.set(IO_WRITE_CH_PIN_04, status);
70-
digital_programmables.set(IO_WRITE_CH_PIN_05, status);
71-
digital_programmables.set(IO_WRITE_CH_PIN_06, status);
72-
digital_programmables.set(IO_WRITE_CH_PIN_07, status);
73-
digital_programmables.set(IO_WRITE_CH_PIN_08, status);
74-
digital_programmables.set(IO_WRITE_CH_PIN_09, status);
75-
digital_programmables.set(IO_WRITE_CH_PIN_10, status);
76-
digital_programmables.set(IO_WRITE_CH_PIN_11, status);
72+
uint8_t readAll() {
73+
uint32_t inputs = digital_programmables.readAll();
74+
Serial.println("CH00: " + String((inputs & (1 << IO_READ_CH_PIN_00)) >> IO_READ_CH_PIN_00));
75+
Serial.println("CH01: " + String((inputs & (1 << IO_READ_CH_PIN_01)) >> IO_READ_CH_PIN_01));
76+
Serial.println("CH02: " + String((inputs & (1 << IO_READ_CH_PIN_02)) >> IO_READ_CH_PIN_02));
77+
Serial.println("CH03: " + String((inputs & (1 << IO_READ_CH_PIN_03)) >> IO_READ_CH_PIN_03));
78+
Serial.println("CH04: " + String((inputs & (1 << IO_READ_CH_PIN_04)) >> IO_READ_CH_PIN_04));
79+
Serial.println("CH05: " + String((inputs & (1 << IO_READ_CH_PIN_05)) >> IO_READ_CH_PIN_05));
80+
Serial.println("CH06: " + String((inputs & (1 << IO_READ_CH_PIN_06)) >> IO_READ_CH_PIN_06));
81+
Serial.println("CH07: " + String((inputs & (1 << IO_READ_CH_PIN_07)) >> IO_READ_CH_PIN_07));
82+
Serial.println("CH08: " + String((inputs & (1 << IO_READ_CH_PIN_08)) >> IO_READ_CH_PIN_08));
83+
Serial.println("CH09: " + String((inputs & (1 << IO_READ_CH_PIN_09)) >> IO_READ_CH_PIN_09));
84+
Serial.println("CH10: " + String((inputs & (1 << IO_READ_CH_PIN_10)) >> IO_READ_CH_PIN_10));
85+
Serial.println("CH11: " + String((inputs & (1 << IO_READ_CH_PIN_11)) >> IO_READ_CH_PIN_11));
86+
Serial.println();
7787
}
7888

79-
void readAll() {
80-
// Reads from input pins. This API returns -1 if you try to read from a write channel.
81-
Serial.println("Pin 00: " + String(digital_programmables.read(IO_READ_CH_PIN_00)));
82-
Serial.println("Pin 01: " + String(digital_programmables.read(IO_READ_CH_PIN_01)));
83-
Serial.println("Pin 02: " + String(digital_programmables.read(IO_READ_CH_PIN_02)));
84-
Serial.println("Pin 03: " + String(digital_programmables.read(IO_READ_CH_PIN_03)));
85-
Serial.println("Pin 04: " + String(digital_programmables.read(IO_READ_CH_PIN_04)));
86-
Serial.println("Pin 05: " + String(digital_programmables.read(IO_READ_CH_PIN_05)));
87-
Serial.println("Pin 06: " + String(digital_programmables.read(IO_READ_CH_PIN_06)));
88-
Serial.println("Pin 07: " + String(digital_programmables.read(IO_READ_CH_PIN_07)));
89-
Serial.println("Pin 08: " + String(digital_programmables.read(IO_READ_CH_PIN_08)));
90-
Serial.println("Pin 09: " + String(digital_programmables.read(IO_READ_CH_PIN_09)));
91-
Serial.println("Pin 10: " + String(digital_programmables.read(IO_READ_CH_PIN_10)));
92-
Serial.println("Pin 11: " + String(digital_programmables.read(IO_READ_CH_PIN_11)));
93-
}

src/utility/ioexpander/ArduinoIOExpander.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ int ArduinoIOExpanderClass::read(int pin)
8383
return -1;
8484
}
8585

86+
void ArduinoIOExpanderClass::writeAll(uint32_t banks) {
87+
_tca.writeAll(banks & 0xFF, (banks >> 8) & 0xFF, 0x00);
88+
}
89+
8690
uint32_t ArduinoIOExpanderClass::readAll()
8791
{
8892
uint8_t banks[3];
@@ -91,11 +95,14 @@ uint32_t ArduinoIOExpanderClass::readAll()
9195
}
9296

9397

98+
void ArduinoIOExpanderClass::toggle(){
99+
writeAll(~(readAll()));
100+
}
101+
94102
void ArduinoIOExpanderClass::initPins()
95103
{
96104

97105
if (_tca.getAddress() == IO_ADD) {
98-
PinStatus status = SWITCH_OFF;
99106
pinMode(IO_WRITE_CH_PIN_00, OUTPUT);
100107
pinMode(IO_WRITE_CH_PIN_01, OUTPUT);
101108
pinMode(IO_WRITE_CH_PIN_02, OUTPUT);
@@ -121,18 +128,7 @@ void ArduinoIOExpanderClass::initPins()
121128
pinMode(IO_READ_CH_PIN_10, INPUT);
122129
pinMode(IO_READ_CH_PIN_11, INPUT);
123130

124-
set(IO_WRITE_CH_PIN_00, status);
125-
set(IO_WRITE_CH_PIN_01, status);
126-
set(IO_WRITE_CH_PIN_02, status);
127-
set(IO_WRITE_CH_PIN_03, status);
128-
set(IO_WRITE_CH_PIN_04, status);
129-
set(IO_WRITE_CH_PIN_05, status);
130-
set(IO_WRITE_CH_PIN_06, status);
131-
set(IO_WRITE_CH_PIN_07, status);
132-
set(IO_WRITE_CH_PIN_08, status);
133-
set(IO_WRITE_CH_PIN_09, status);
134-
set(IO_WRITE_CH_PIN_10, status);
135-
set(IO_WRITE_CH_PIN_11, status);
131+
writeAll(SWITCH_OFF_ALL);
136132
} else {
137133
pinMode(DIN_READ_CH_PIN_00, INPUT);
138134
pinMode(DIN_READ_CH_PIN_01, INPUT);

src/utility/ioexpander/ArduinoIOExpander.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
#define SWITCH_ON HIGH
2626
#define SWITCH_OFF LOW
2727

28+
#define SWITCH_ON_ALL 0x0000FFFF
29+
#define SWITCH_OFF_ALL 0x00000000
30+
31+
enum {
32+
ON_VALUE_PIN_00 = 0x01,
33+
ON_VALUE_PIN_01 = 0x02,
34+
ON_VALUE_PIN_02 = 0x04,
35+
ON_VALUE_PIN_03 = 0x08,
36+
ON_VALUE_PIN_04 = 0x80,
37+
ON_VALUE_PIN_05 = 0x40,
38+
ON_VALUE_PIN_06 = 0x20,
39+
ON_VALUE_PIN_07 = 0x10,
40+
ON_VALUE_PIN_08 = 0x100,
41+
ON_VALUE_PIN_09 = 0x200,
42+
ON_VALUE_PIN_10 = 0x400,
43+
ON_VALUE_PIN_11 = 0x800,
44+
};
45+
2846
enum {
2947
IO_WRITE_CH_PIN_00 = TCA6424A_P00,
3048
IO_WRITE_CH_PIN_01 = TCA6424A_P01,
@@ -77,9 +95,10 @@ class ArduinoIOExpanderClass {
7795
void setAddress(uint8_t address);
7896
bool set(int pin, PinStatus status);
7997
bool set(int pin, int status) { return set( pin, (PinStatus)status); };
80-
98+
void writeAll(uint32_t banks);
8199
int read(int pin);
82100
uint32_t readAll();
101+
void toggle();
83102
bool pinMode(int pin, PinMode direction);
84103

85104
private:

0 commit comments

Comments
 (0)