-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCF8574.cpp
108 lines (95 loc) · 1.79 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
//
// FILE: PCF8574.cpp
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.1.02
// PURPOSE: I2C PCF8574 library for Arduino
// URL:
//
// HISTORY:
// 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"
#include <Wire.h>
PCF8574::PCF8574(int address)
{
_address = address;
}
void PCF8574::begin(){
Wire.begin();
}
uint8_t PCF8574::read8()
{
Wire.beginTransmission(_address);
Wire.requestFrom(_address, 1);
#if (ARDUINO < 100)
_data = Wire.receive();
#else
_data = Wire.read();
#endif
_error = Wire.endTransmission();
return _data;
}
uint8_t PCF8574::value()
{
return _data;
}
void PCF8574::write8(uint8_t value)
{
Wire.beginTransmission(_address);
_data = value;
Wire.write(_data);
_error = Wire.endTransmission();
}
uint8_t PCF8574::read(uint8_t pin)
{
PCF8574::read8();
return (_data & (1<<pin)) > 0;
}
void PCF8574::write(uint8_t pin, uint8_t value)
{
PCF8574::read8();
if (value == LOW)
{
_data &= ~(1<<pin);
}
else
{
_data |= (1<<pin);
}
PCF8574::write8(_data);
}
void PCF8574::toggle(uint8_t pin)
{
PCF8574::read8();
_data ^= (1 << pin);
PCF8574::write8(_data);
}
void PCF8574::shiftRight(uint8_t n)
{
if (n == 0 || n > 7 ) return;
PCF8574::read8();
_data >>= n;
PCF8574::write8(_data);
}
void PCF8574::shiftLeft(uint8_t n)
{
if (n == 0 || n > 7) return;
PCF8574::read8();
_data <<= n;
PCF8574::write8(_data);
}
int PCF8574::lastError()
{
int e = _error;
_error = 0;
return e;
}
//
// END OF FILE
//