-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPCF8574.cpp
214 lines (191 loc) · 5.27 KB
/
PCF8574.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
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
//
// FILE: PCF8574.cpp
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.3.1
// PURPOSE: Arduino library for PCF8574 - 8 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8574
// http://forum.arduino.cc/index.php?topic=184800
//
// PA0PHH Adjusted for Raspberry PI
// HISTORY:
// 0.3.1 2021-04-23 Fix for platformIO compatibility
// 0.3.0 2021-01-03 multiWire support - inspirated by mattbue - issue #14
// 0.2.4 2020-12-17 fix #6 tag problem 0.2.3
// 0.2.3 2020-12-14 fix #6 readButton8 ambiguity
// 0.2.2 2020-12-07 add Arduino-ci + start unit test + _wire->h in PCF8574.h
// 0.2.1 2020-06-19 fix library.json
// 0.2.0 2020-05-22 #pragma once; refactor;
// removed pre 1.0 support
// added begin(dsa, scl) for ESP32
// added reverse()
//
// 0.1.9 2017-02-27 fix warning about return in readButton8()
// 0.1.08 2016-05-20 Merged work of Septillion
// Fix/refactor ButtonRead8() - see https://github.com/RobTillaart/Arduino/issues/38
// missing begin() => mask parameter
// 0.1.07 2016-05-02 (manually merged) Septillion
// added dataOut so a write() doesn't read first,
// possibly corrupting a input pin;
// fixed shift comment, should read 1..7;
// added begin() to be sure it's in a known state,
// states could be different if uC is reset and the PCF8574 isn't;
// added buttonRead() and buttonRead8()
// which only effect the output while reading
// 0.1.06 (intermediate) added defined errors + refactor rotate
// 0.1.05 2016-04-30 refactor, +toggleMask, +rotLeft, +rotRight
// 0.1.04 2015-05-09 removed ambiguity in read8()
// 0.1.03 2015-03-02 address int -> uint8_t
// 0.1.02 replaced ints with uint8_t to reduce footprint;
// added default value for shiftLeft() and shiftRight()
// renamed status() to lastError();
// 0.1.01 added value(); returns last read 8 bit value (cached);
// value() does not always reflect the latest state of the pins!
// 0.1.00 initial version
//
#include "PCF8574.h"
PCF8574::PCF8574(const uint8_t deviceAddress)
{
_address = deviceAddress;
_dataIn = 0;
_dataOut = 0xFF;
_buttonMask = 0xFF;
_error = PCF8574_OK;
_connected = false;
fd = -1;
}
bool PCF8574::begin(uint8_t val)
{
if ((fd = wiringPiI2CSetup(_address)) < 0)
return false ;
if (! isConnected()) return false;
PCF8574::write8(val);
_connected = true;
return true;
}
bool PCF8574::isConnected()
{
int ret = wiringPiI2CRead(fd);
return ( ret != -1);
}
uint8_t PCF8574::read8()
{
int ret = wiringPiI2CReadReg8(_address, (uint8_t)1);
if (ret < 0)
{
_error = PCF8574_I2C_ERROR;
return _dataIn; // last value
}
_dataIn = ret & 0xFF;
return _dataIn;
}
void PCF8574::write8(const uint8_t value)
{
int ret = wiringPiI2CWrite(fd, value);
_dataOut = value;
_error = ret;
}
uint8_t PCF8574::read(const uint8_t pin)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return 0;
}
PCF8574::read8();
return (_dataIn & (1 << pin)) > 0;
}
void PCF8574::write(const uint8_t pin, const uint8_t value)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return;
}
if (value == LOW)
{
_dataOut &= ~(1 << pin);
}
else
{
_dataOut |= (1 << pin);
}
write8(_dataOut);
}
void PCF8574::toggle(const uint8_t pin)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return;
}
toggleMask(1 << pin);
}
void PCF8574::toggleMask(const uint8_t mask)
{
_dataOut ^= mask;
PCF8574::write8(_dataOut);
}
void PCF8574::shiftRight(const uint8_t n)
{
if ((n == 0) || (_dataOut == 0)) return;
if (n > 7) _dataOut = 0; // shift 8++ clears all, valid...
if (_dataOut != 0) _dataOut >>= n; // only shift if there are bits set
PCF8574::write8(_dataOut);
}
void PCF8574::shiftLeft(const uint8_t n)
{
if ((n == 0) || (_dataOut == 0)) return;
if (n > 7) _dataOut = 0; // shift 8++ clears all, valid...
if (_dataOut != 0) _dataOut <<= n; // only shift if there are bits set
PCF8574::write8(_dataOut);
}
int PCF8574::lastError()
{
int e = _error;
_error = PCF8574_OK; // reset error after read, is this wise?
return e;
}
void PCF8574::rotateRight(const uint8_t n)
{
uint8_t r = n & 7;
if (r == 0) return;
_dataOut = (_dataOut >> r) | (_dataOut << (8 - r));
PCF8574::write8(_dataOut);
}
void PCF8574::rotateLeft(const uint8_t n)
{
rotateRight(8 - (n & 7));
}
void PCF8574::reverse() // quite fast: 14 shifts, 3 or, 3 assignment.
{
uint8_t x = _dataOut;
x = (((x & 0xAA) >> 1) | ((x & 0x55) << 1));
x = (((x & 0xCC) >> 2) | ((x & 0x33) << 2));
x = ((x >> 4) | (x << 4));
PCF8574::write8(x);
}
//added 0.1.07/08 Septillion
uint8_t PCF8574::readButton8(const uint8_t mask)
{
uint8_t temp = _dataOut;
PCF8574::write8(mask | _dataOut); // read only selected lines
PCF8574::read8();
PCF8574::write8(temp); // restore
return _dataIn;
}
//added 0.1.07 Septillion
uint8_t PCF8574::readButton(const uint8_t pin)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return 0;
}
uint8_t temp = _dataOut;
PCF8574::write(pin, HIGH);
uint8_t rtn = PCF8574::read(pin);
PCF8574::write8(temp);
return rtn;
}
// -- END OF FILE --