forked from arduino-libraries/Arduino_MachineControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoIOExpander.cpp
158 lines (137 loc) · 4.5 KB
/
ArduinoIOExpander.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
This file is part of the MachineControl library.
Copyright (C) 2020 Arduino AG (http://www.arduino.cc/)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ArduinoIOExpander.h"
bool ArduinoIOExpanderClass::begin()
{
if(!_tca.testConnection()) {
return false;
}
//Initialize all pins to the default mode
initPins();
return true;
}
bool ArduinoIOExpanderClass::begin(uint8_t address)
{
setAddress(address);
if(!_tca.testConnection()) {
return false;
}
//Initialize all pins to the default mode
initPins();
return true;
}
ArduinoIOExpanderClass::operator bool()
{
return _tca.testConnection();
}
bool ArduinoIOExpanderClass::pinMode(int pin, PinMode direction)
{
if (direction > OUTPUT)
return false;
_tca.setPinDirection(pin, direction == INPUT ? TCA6424A_INPUT : TCA6424A_OUTPUT);
return true;
}
void ArduinoIOExpanderClass::setAddress(uint8_t address) {
_tca.setAddress(address);
}
bool ArduinoIOExpanderClass::set(int pin, PinStatus status)
{
if (pin < IO_READ_CH_PIN_00) {
if (status > HIGH)
return false;
_tca.writePin(pin, status == HIGH ? TCA6424A_HIGH : TCA6424A_LOW);
return true;
}
return false;
}
int ArduinoIOExpanderClass::read(int pin)
{
if(_tca.getAddress() == IO_ADD) {
if (pin > TCA6424A_P13 && pin <= TCA6424A_P27) {
return _tca.readPin(pin) == true ? 1 : 0;
}
} else if(_tca.getAddress() == DIN_ADD) {
if ((pin >= TCA6424A_P00) && (pin <= TCA6424A_P10) && (pin !=TCA6424A_P03)) {
return _tca.readPin(pin) == true ? 1 : 0;
}
}
return -1;
}
void ArduinoIOExpanderClass::writeAll(uint32_t banks) {
_tca.writeAll(banks & 0xFF, (banks >> 8) & 0xFF, 0x00);
}
uint32_t ArduinoIOExpanderClass::readAll()
{
uint8_t banks[3];
_tca.readAll(banks);
return *(uint32_t*)banks;
}
void ArduinoIOExpanderClass::toggle(){
writeAll(~(readAll()));
}
void ArduinoIOExpanderClass::initPins()
{
if (_tca.getAddress() == IO_ADD) {
PinStatus status = SWITCH_OFF;
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);
pinMode(IO_WRITE_CH_PIN_00, OUTPUT);
pinMode(IO_WRITE_CH_PIN_01, OUTPUT);
pinMode(IO_WRITE_CH_PIN_02, OUTPUT);
pinMode(IO_WRITE_CH_PIN_03, OUTPUT);
pinMode(IO_WRITE_CH_PIN_04, OUTPUT);
pinMode(IO_WRITE_CH_PIN_05, OUTPUT);
pinMode(IO_WRITE_CH_PIN_06, OUTPUT);
pinMode(IO_WRITE_CH_PIN_07, OUTPUT);
pinMode(IO_WRITE_CH_PIN_08, OUTPUT);
pinMode(IO_WRITE_CH_PIN_09, OUTPUT);
pinMode(IO_WRITE_CH_PIN_10, OUTPUT);
pinMode(IO_WRITE_CH_PIN_11, OUTPUT);
pinMode(IO_READ_CH_PIN_00, INPUT);
pinMode(IO_READ_CH_PIN_01, INPUT);
pinMode(IO_READ_CH_PIN_02, INPUT);
pinMode(IO_READ_CH_PIN_03, INPUT);
pinMode(IO_READ_CH_PIN_04, INPUT);
pinMode(IO_READ_CH_PIN_05, INPUT);
pinMode(IO_READ_CH_PIN_06, INPUT);
pinMode(IO_READ_CH_PIN_07, INPUT);
pinMode(IO_READ_CH_PIN_08, INPUT);
pinMode(IO_READ_CH_PIN_09, INPUT);
pinMode(IO_READ_CH_PIN_10, INPUT);
pinMode(IO_READ_CH_PIN_11, INPUT);
writeAll(SWITCH_OFF_ALL);
} else {
pinMode(DIN_READ_CH_PIN_00, INPUT);
pinMode(DIN_READ_CH_PIN_01, INPUT);
pinMode(DIN_READ_CH_PIN_02, INPUT);
pinMode(DIN_READ_CH_PIN_03, INPUT);
pinMode(DIN_READ_CH_PIN_04, INPUT);
pinMode(DIN_READ_CH_PIN_05, INPUT);
pinMode(DIN_READ_CH_PIN_06, INPUT);
pinMode(DIN_READ_CH_PIN_07, INPUT);
}
}
ArduinoIOExpanderClass Expander;