1818
1919bool ArduinoIOExpanderClass::begin ()
2020{
21- Wire.begin ();
22- _tca.initialize ();
21+ if (!_tca.testConnection ()) {
22+ return false ;
23+ }
24+ // Initialize all pins to the default mode
25+ initPins ();
26+
27+ return true ;
28+ }
29+
30+ bool ArduinoIOExpanderClass::begin (uint8_t address)
31+ {
32+ setAddress (address);
2333 if (!_tca.testConnection ()) {
2434 return false ;
2535 }
@@ -43,6 +53,10 @@ bool ArduinoIOExpanderClass::pinMode(int pin, PinMode direction)
4353 return true ;
4454}
4555
56+ void ArduinoIOExpanderClass::setAddress (uint8_t address) {
57+ _tca.setAddress (address);
58+ }
59+
4660bool ArduinoIOExpanderClass::set (int pin, PinStatus status)
4761{
4862 if (pin < IO_READ_CH_PIN_00) {
@@ -57,52 +71,70 @@ bool ArduinoIOExpanderClass::set(int pin, PinStatus status)
5771
5872int ArduinoIOExpanderClass::read (int pin)
5973{
60- if (pin > IO_READ_CH_PIN_11) {
61- return _tca.readPin (pin) == true ? 1 : 0 ;
74+ if (_tca.getAddress () == IO_ADD) {
75+ if (pin > TCA6424A_P13 && pin <= TCA6424A_P27) {
76+ return _tca.readPin (pin) == true ? 1 : 0 ;
77+ }
78+ } else if (_tca.getAddress () == DIN_ADD) {
79+ if ((pin >= TCA6424A_P00) && (pin <= TCA6424A_P10) && (pin !=TCA6424A_P03)) {
80+ return _tca.readPin (pin) == true ? 1 : 0 ;
81+ }
6282 }
6383 return -1 ;
6484}
6585
6686void ArduinoIOExpanderClass::initPins ()
6787{
68- PinStatus status = SWITCH_OFF;
69- pinMode (IO_WRITE_CH_PIN_00, OUTPUT);
70- pinMode (IO_WRITE_CH_PIN_01, OUTPUT);
71- pinMode (IO_WRITE_CH_PIN_02, OUTPUT);
72- pinMode (IO_WRITE_CH_PIN_03, OUTPUT);
73- pinMode (IO_WRITE_CH_PIN_04, OUTPUT);
74- pinMode (IO_WRITE_CH_PIN_05, OUTPUT);
75- pinMode (IO_WRITE_CH_PIN_06, OUTPUT);
76- pinMode (IO_WRITE_CH_PIN_07, OUTPUT);
77- pinMode (IO_WRITE_CH_PIN_08, OUTPUT);
78- pinMode (IO_WRITE_CH_PIN_09, OUTPUT);
79- pinMode (IO_WRITE_CH_PIN_10, OUTPUT);
80- pinMode (IO_WRITE_CH_PIN_11, OUTPUT);
81- pinMode (IO_READ_CH_PIN_00, INPUT);
82- pinMode (IO_READ_CH_PIN_01, INPUT);
83- pinMode (IO_READ_CH_PIN_02, INPUT);
84- pinMode (IO_READ_CH_PIN_03, INPUT);
85- pinMode (IO_READ_CH_PIN_04, INPUT);
86- pinMode (IO_READ_CH_PIN_05, INPUT);
87- pinMode (IO_READ_CH_PIN_06, INPUT);
88- pinMode (IO_READ_CH_PIN_07, INPUT);
89- pinMode (IO_READ_CH_PIN_08, INPUT);
90- pinMode (IO_READ_CH_PIN_09, INPUT);
91- pinMode (IO_READ_CH_PIN_10, INPUT);
92- pinMode (IO_READ_CH_PIN_11, INPUT);
93-
94- set (IO_WRITE_CH_PIN_00, status);
95- set (IO_WRITE_CH_PIN_01, status);
96- set (IO_WRITE_CH_PIN_02, status);
97- set (IO_WRITE_CH_PIN_03, status);
98- set (IO_WRITE_CH_PIN_04, status);
99- set (IO_WRITE_CH_PIN_05, status);
100- set (IO_WRITE_CH_PIN_06, status);
101- set (IO_WRITE_CH_PIN_07, status);
102- set (IO_WRITE_CH_PIN_08, status);
103- set (IO_WRITE_CH_PIN_09, status);
104- set (IO_WRITE_CH_PIN_10, status);
105- set (IO_WRITE_CH_PIN_11, status);
88+
89+ if (_tca.getAddress () == IO_ADD) {
90+ PinStatus status = SWITCH_OFF;
91+ pinMode (IO_WRITE_CH_PIN_00, OUTPUT);
92+ pinMode (IO_WRITE_CH_PIN_01, OUTPUT);
93+ pinMode (IO_WRITE_CH_PIN_02, OUTPUT);
94+ pinMode (IO_WRITE_CH_PIN_03, OUTPUT);
95+ pinMode (IO_WRITE_CH_PIN_04, OUTPUT);
96+ pinMode (IO_WRITE_CH_PIN_05, OUTPUT);
97+ pinMode (IO_WRITE_CH_PIN_06, OUTPUT);
98+ pinMode (IO_WRITE_CH_PIN_07, OUTPUT);
99+ pinMode (IO_WRITE_CH_PIN_08, OUTPUT);
100+ pinMode (IO_WRITE_CH_PIN_09, OUTPUT);
101+ pinMode (IO_WRITE_CH_PIN_10, OUTPUT);
102+ pinMode (IO_WRITE_CH_PIN_11, OUTPUT);
103+ pinMode (IO_READ_CH_PIN_00, INPUT);
104+ pinMode (IO_READ_CH_PIN_01, INPUT);
105+ pinMode (IO_READ_CH_PIN_02, INPUT);
106+ pinMode (IO_READ_CH_PIN_03, INPUT);
107+ pinMode (IO_READ_CH_PIN_04, INPUT);
108+ pinMode (IO_READ_CH_PIN_05, INPUT);
109+ pinMode (IO_READ_CH_PIN_06, INPUT);
110+ pinMode (IO_READ_CH_PIN_07, INPUT);
111+ pinMode (IO_READ_CH_PIN_08, INPUT);
112+ pinMode (IO_READ_CH_PIN_09, INPUT);
113+ pinMode (IO_READ_CH_PIN_10, INPUT);
114+ pinMode (IO_READ_CH_PIN_11, INPUT);
115+
116+ set (IO_WRITE_CH_PIN_00, status);
117+ set (IO_WRITE_CH_PIN_01, status);
118+ set (IO_WRITE_CH_PIN_02, status);
119+ set (IO_WRITE_CH_PIN_03, status);
120+ set (IO_WRITE_CH_PIN_04, status);
121+ set (IO_WRITE_CH_PIN_05, status);
122+ set (IO_WRITE_CH_PIN_06, status);
123+ set (IO_WRITE_CH_PIN_07, status);
124+ set (IO_WRITE_CH_PIN_08, status);
125+ set (IO_WRITE_CH_PIN_09, status);
126+ set (IO_WRITE_CH_PIN_10, status);
127+ set (IO_WRITE_CH_PIN_11, status);
128+ } else {
129+ pinMode (DIN_READ_CH_PIN_00, INPUT);
130+ pinMode (DIN_READ_CH_PIN_01, INPUT);
131+ pinMode (DIN_READ_CH_PIN_02, INPUT);
132+ pinMode (DIN_READ_CH_PIN_03, INPUT);
133+ pinMode (DIN_READ_CH_PIN_04, INPUT);
134+ pinMode (DIN_READ_CH_PIN_05, INPUT);
135+ pinMode (DIN_READ_CH_PIN_06, INPUT);
136+ pinMode (DIN_READ_CH_PIN_07, INPUT);
137+ }
106138}
107139
108140ArduinoIOExpanderClass Expander;
0 commit comments