-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPCF857X.cpp
More file actions
301 lines (233 loc) · 5.96 KB
/
PCF857X.cpp
File metadata and controls
301 lines (233 loc) · 5.96 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* See header file for details
*
* This program is free software: you can redistribute it and/or modify\n
* it under the terms of the GNU General Public License as published by\n
* the Free Software Foundation, either version 3 of the License, or\n
* (at your option) any later version.\n
*
* This program is distributed in the hope that it will be useful,\n
* but WITHOUT ANY WARRANTY; without even the implied warranty of\n
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n
* GNU General Public License for more details.\n
*
* You should have received a copy of the GNU General Public License\n
* along with this program. If not, see <http://www.gnu.org/licenses/>.\n
*/
/* Dependencies */
#include <Wire.h>
#include "PCF857X.h"
#ifdef PCF857X_INTERRUPT_SUPPORT
#include "PCint.h"
#endif
// max pins of chips: 8 pins for PCF8574, 16 pins for PCF8575
uint8_t PCF857X_MAX_PINS[2] = { 8, 16 };
PCF857X::PCF857X() :
_PORT(0), _PIN(0), _DDR(0), _address(0)
#ifdef PCF857X_INTERRUPT_SUPPORT
, _oldPIN(0), _isrIgnore(0), _pcintPin(0), _intMode(), _intCallback()
#endif
{
}
void PCF857X::begin(uint8_t address, uint8_t chip) {
/* Store the I2C address */
_address = address;
/* Store the chip type */
_chip = chip;
/* Init the Wire library */
Wire.begin();
readGPIO();
}
void PCF857X::pinMode(uint8_t pin, uint8_t mode) {
/* Switch according mode */
switch (mode) {
case INPUT:
_DDR &= ~(1 << pin);
_PORT &= ~(1 << pin);
break;
case INPUT_PULLUP:
_DDR &= ~(1 << pin);
_PORT |= (1 << pin);
break;
case OUTPUT:
_DDR |= (1 << pin);
_PORT &= ~(1 << pin);
break;
default:
break;
}
/* Update GPIO values */
updateGPIO();
}
void PCF857X::digitalWrite(uint8_t pin, uint8_t value) {
/* Set PORT bit value */
if (value)
_PORT |= (1 << pin);
else
_PORT &= ~(1 << pin);
/* Update GPIO values */
updateGPIO();
}
uint8_t PCF857X::digitalRead(uint8_t pin) {
/* Read GPIO */
readGPIO();
#ifdef PCF857X_INTERRUPT_SUPPORT
/* Check for interrupt (manual detection) */
//checkForInterrupt();
#endif
/* Read and return the pin state */
return (_PIN & (1 << pin)) ? HIGH : LOW;
}
void PCF857X::write(uint16_t value) {
/* Store pins values and apply */
_PORT = value;
/* Update GPIO values */
updateGPIO();
}
uint16_t PCF857X::read() {
/* Read GPIO */
readGPIO();
#ifdef PCF857X_INTERRUPT_SUPPORT
/* Check for interrupt (manual detection) */
//checkForInterrupt();
#endif
/* Return current pins values */
return _PIN;
}
void PCF857X::pullUp(uint8_t pin) {
/* Same as pinMode(INPUT_PULLUP) */
pinMode(pin, INPUT_PULLUP); // /!\ pinMode form THE LIBRARY
}
void PCF857X::pullDown(uint8_t pin) {
/* Same as pinMode(INPUT) */
pinMode(pin, INPUT); // /!\ pinMode form THE LIBRARY
}
void PCF857X::clear() {
/* User friendly wrapper for write() */
if (_chip == CHIP_PCF8575) {
write(0x0000);
} else {
write(0x00);
}
}
void PCF857X::set() {
/* User friendly wrapper for write() */
if (_chip == CHIP_PCF8575) {
write(0xFFFF);
} else {
write(0xFF);
}
}
void PCF857X::toggle(uint8_t pin) {
/* Toggle pin state */
_PORT ^= (1 << pin);
/* Update GPIO values */
updateGPIO();
}
void PCF857X::blink(uint8_t pin, uint16_t count, uint32_t duration) {
/* Compute steps duration */
duration /= count * 2;
/* Loop n times */
while (count--) {
/* Toggle pin 2 times */
toggle(pin);
delay(duration);
toggle(pin);
delay(duration);
}
}
#ifdef PCF857X_INTERRUPT_SUPPORT
void PCF857X::enableInterrupt(uint8_t pin, void (*selfCheckFunction)(void)) {
/* Store interrupt pin number */
_pcintPin = pin;
/* Setup interrupt pin */
#if ARDUINO >= 100
::pinMode(pin, INPUT_PULLUP); // /!\ pinMode form THE ARDUINO CORE
#else
::pinMode(pin, INPUT); // /!\ pinMode form THE ARDUINO CORE
::digitalWrite(pin, HIGH); // /!\ digitalWrite form THE ARDUINO CORE
#endif
/* Attach interrupt handler */
PCattachInterrupt(pin, selfCheckFunction, FALLING);
}
void PCF857X::disableInterrupt() {
/* Detach interrupt handler */
PCdetachInterrupt(_pcintPin);
}
void PCF857X::checkForInterrupt() {
/* Avoid nested interrupt triggered by I2C read/write */
if(_isrIgnore)
return;
else
_isrIgnore = 1;
/* Re-enable interrupts to allow Wire library to work */
sei();
/* Read current pins values */
readGPIO();
/* Check all pins */
for (uint8_t i = 0; i < PCF857X_MAX_PINS[_chip]; ++i) {
/* Check for interrupt handler */
if (!_intCallback[i])
continue;
/* Check for interrupt event */
switch (_intMode[i]) {
case CHANGE:
if ((1 << i) & (_PIN ^ _oldPIN))
_intCallback[i]();
break;
case LOW:
if (!(_PIN & (1 << i)))
_intCallback[i]();
break;
case FALLING:
if ((_oldPIN & (1 << i)) && !(_PIN & (1 << i)))
_intCallback[i]();
break;
case RISING:
if (!(_oldPIN & (1 << i)) && (_PIN & (1 << i)))
_intCallback[i]();
break;
}
}
/* Turn off ISR ignore flag */
_isrIgnore = 0;
}
void PCF857X::attachInterrupt(uint8_t pin, void (*userFunc)(void),
uint8_t mode) {
/* Store interrupt mode and callback */
_intMode[pin] = mode;
_intCallback[pin] = userFunc;
}
void PCF857X::detachInterrupt(uint8_t pin) {
/* Void interrupt handler */
_intCallback[pin] = 0;
}
#endif
void PCF857X::readGPIO() {
#ifdef PCF857X_INTERRUPT_SUPPORT
/* Store old _PIN value */
_oldPIN = _PIN;
#endif
/* Start request, wait for data and receive GPIO values as byte */
Wire.requestFrom(_address, (uint8_t) 0x02);
if (Wire.available() >= 2) {
if (_chip == CHIP_PCF8575) {
_PIN = I2CREAD(); /* LSB first */
_PIN |= I2CREAD() << 8;
} else {
_PIN |= I2CREAD() << 8;
}
}
}
void PCF857X::updateGPIO() {
/* Read current GPIO states */
//readGPIO(); // Experimental
/* Compute new GPIO states */
//uint8_t value = ((_PIN & ~_DDR) & ~(~_DDR & _PORT)) | _PORT; // Experimental
uint16_t value = (_PIN & ~_DDR) | _PORT;
/* Start communication and send GPIO values as byte */
Wire.beginTransmission(_address);
I2CWRITE(value & 0x00FF);
I2CWRITE((value & 0xFF00) >> 8);
Wire.endTransmission();
}