-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPCF8574.h
246 lines (224 loc) · 5.6 KB
/
PCF8574.h
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
/**
* @file PCF8574.h
* @version 1.0
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* 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.
*/
#ifndef PCF8574_H
#define PCF8574_H
#include "TWI.h"
/**
* Driver for the PCF8574/PCF8574A Remote 8-bit I/O expander for
* I2C-bus with interrupt.
*
* @section Circuit
* @code
* PCF8574A
* +------------+
* (GND)---[ ]---------1-|A0 VCC|-16--------------(VCC)
* (GND)---[ ]---------2-|A1 SDA|-15-----------(SDA/A4)
* (GND)---[ ]---------3-|A2 SCL|-14-----------(SCL/A5)
* (P0)----------------4-|P0 INT|-13--------------(EXT)
* (P1)----------------5-|P1 P7|-12---------------(P7)
* (P2)----------------6-|P2 P6|-11---------------(P6)
* (P3)----------------7-|P3 P5|-10---------------(P5)
* (GND)---------------8-|GND P4|-9----------------(P4)
* +------------+
* @endcode
*
* @section References
* 1. NXP Semiconductors Product data sheet, Rev. 5, 27 May 2013.
*/
class PCF8574 : protected TWI::Device {
public:
/**
* Construct connection to PCF8574 Remote 8-bit I/O expander with
* given sub-address.
* @param[in] twi bus manager.
* @param[in] subaddr sub-address (0..7, default 7).
*/
PCF8574(TWI& twi, uint8_t subaddr = 7) :
TWI::Device(twi, 0x20 | (subaddr & 0x7)),
m_ddr(0xff),
m_port(0)
{}
/**
* Get data direction for port; 0 for output, 1 for input.
* @return data direction.
*/
uint8_t ddr()
{
return (m_ddr);
}
/**
* Set data direction for port pin P0..P7; 0 for output, 1 for input.
* @param[in] ddr data direction mask.
*/
void ddr(uint8_t ddr)
{
m_ddr = ddr;
m_port |= m_ddr;
acquire();
Device::write(&m_port, sizeof(m_port));
release();
}
/**
* Read pins and return current values.
* @return input pin values.
*/
uint8_t read()
{
uint8_t res;
acquire();
Device::read(&res, sizeof(res));
release();
return ((res & m_ddr) | m_port);
}
/**
* Get data port values.
* @return port value.
*/
uint8_t port()
{
return (m_port);
}
/**
* Write given value to the output pins.
* @param[in] value to write to port.
*/
void write(uint8_t value)
{
acquire();
m_port = value | m_ddr;
Device::write(&m_port, sizeof(m_port));
release();
}
/**
* Write given values to the output pins.
* @param[in] buf pointer to data to write to device.
* @param[in] size of buffer.
*/
void write(void* buf, size_t size)
{
acquire();
uint8_t* bp = (uint8_t*) buf;
size_t n = size;
while (n--) *bp++ |= m_ddr;
Device::write(buf, size);
release();
}
/**
* PCF8574 based General Purpose Digital I/O pin template class.
* @param[in] PIN index on device (0..7).
*/
template<uint8_t PIN>
class GPIO {
public:
/** Pin bit position mask. */
static const uint8_t MASK = (1 << (PIN & 0x7));
/**
* Construct pin handler with given PCF8574 device driver.
*/
GPIO(PCF8574& dev) : m_dev(dev) {}
/**
* Set input mode.
*/
void input()
__attribute__((always_inline))
{
m_dev.ddr(m_dev.ddr() | MASK);
}
/**
* Set output mode.
*/
void output()
__attribute__((always_inline))
{
m_dev.ddr(m_dev.ddr() & ~MASK);
}
/**
* Read pin state. Return true is set otherwise false.
* @return bool.
*/
bool read()
{
if (m_dev.ddr() & MASK)
return ((m_dev.read() & MASK) != 0);
return ((m_dev.port() & MASK) != 0);
}
/**
* Read pin state. Return true is set otherwise false.
* Shorthand for read().
* @return bool.
*/
operator bool()
__attribute__((always_inline))
{
return (read());
}
/**
* Write pin state one(1) if given value is non-zero,
* otherwise zero(0).
* @param[in] value to write.
*/
void write(int value)
{
uint8_t data = m_dev.port();
if (value)
data |= MASK;
else
data &= ~MASK;
m_dev.write(data);
}
/**
* Assign pin state one(1) if given value is non-zero,
* otherwise zero(0). Shorthand for write().
*/
void operator=(int value)
__attribute__((always_inline))
{
write(value);
}
protected:
PCF8574& m_dev;
};
protected:
/** Data Direction Register, 0 = output, 1 = input. */
uint8_t m_ddr;
/** Port Register to mask and maintain output pin values. */
uint8_t m_port;
/**
* Construct connection to PCF8574 Remote 8-bit I/O expander with
* given address.
* @param[in] twi bus manager.
* @param[in] addr bus address.
* @param[in] subaddr device sub address.
*/
PCF8574(TWI& twi, uint8_t addr, uint8_t subaddr) :
TWI::Device(twi, addr | (subaddr & 0x7)),
m_ddr(0xff),
m_port(0)
{}
};
class PCF8574A : public PCF8574 {
public:
/**
* Construct connection to PCF8574A Remote 8-bit I/O expander with
* given sub-address.
* @param[in] twi bus manager.
* @param[in] subaddr sub-address (0..7, default 7).
*/
PCF8574A(TWI& twi, uint8_t subaddr = 7) : PCF8574(twi, 0x38, subaddr) {}
};
#endif