-
Notifications
You must be signed in to change notification settings - Fork 29
/
pcf8574a.cpp
157 lines (131 loc) · 2.92 KB
/
pcf8574a.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
#include <stdio.h>
#include <inttypes.h>
#include <Arduino.h>
#include "pcf8574a.h"
#include <../Wire/Wire.h>//this chip uses wire
pcf8574a::pcf8574a(){
}
pcf8574a::pcf8574a(const uint8_t adrs){
postSetup(adrs);
_gpioDirection = 0;
_gpioInputs = 0;
_gpioState = 0;
}
void pcf8574a::postSetup(const uint8_t adrs){
if (adrs >= 0x38 && adrs <= 0x3F){// 0x38...0x3F for A variant!
_adrs = adrs;
_error = false;
} else {
_error = true;
}
}
void pcf8574a::begin(bool protocolInitOverride) {
if (!protocolInitOverride && !_error) {
Wire.begin();
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif
}
delay(100);
readGpio();
}
void pcf8574a::gpioPinMode(uint8_t mode){
if (mode == INPUT){
_gpioDirection = 0xFF;
} else if (mode == INPUT_PULLUP){
_gpioDirection = 0x00;
_gpioState = 0xFF;
} else if (mode == OUTPUT){
_gpioDirection = 0x00;
_gpioState = 0x00;
} else {
_gpioDirection = mode;
}
updateGpio();
}
void pcf8574a::gpioPinMode(uint8_t pin, uint8_t mode){
if (pin < 8){//0...7
switch (mode) {
case INPUT:
_gpioDirection &= ~(1 << pin);
_gpioState &= ~(1 << pin);
break;
case INPUT_PULLUP:
_gpioDirection &= ~(1 << pin);
_gpioState |= (1 << pin);
break;
case OUTPUT:
_gpioDirection |= (1 << pin);
_gpioState &= ~(1 << pin);
break;
default:
break;
}
updateGpio();
}
}
void pcf8574a::gpioPort(uint8_t value){
if (value == HIGH){
_gpioState = 0xFF;
} else if (value == LOW){
_gpioState = 0x00;
} else {
_gpioState = value;
}
updateGpio();
}
uint8_t pcf8574a::readGpioPort(){
readGpio();
return _gpioState;
}
uint8_t pcf8574a::readGpioPortFast(){
return _gpioState;
}
uint8_t pcf8574a::gpioDigitalRead(uint8_t pin){
readGpio();
return (_gpioState & (1 << pin)) ? HIGH : LOW;
}
uint8_t pcf8574a::gpioDigitalReadFast(uint8_t pin){
int temp = 0;
if (pin < 8) temp = bitRead(_gpioState,pin);//0...7
return temp;
}
void pcf8574a::gpioPinToggle(uint8_t pin) {
_gpioState ^= (1 << pin);
updateGpio();
}
void pcf8574a::gpioDigitalWrite(uint8_t pin, bool value){
if (pin < 8){//0...7
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
updateGpio();
}
}
void pcf8574a::gpioDigitalWriteFast(uint8_t pin, bool value){
if (pin < 8){//0...7
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
}
}
void pcf8574a::gpioPortUpdate(){
if (!_error){
//uint8_t data = (_gpioInputs & ~_gpioDirection) | _gpioState;
Wire.beginTransmission(_adrs);
Wire.write(_gpioState);
Wire.endTransmission();
}
}
void pcf8574a::updateGpio(){
if (!_error){
uint8_t data = (_gpioInputs & ~_gpioDirection) | _gpioState;
Wire.beginTransmission(_adrs);
Wire.write(data);
Wire.endTransmission();
}
}
void pcf8574a::readGpio(){
if (!_error){
Wire.requestFrom((uint8_t)_adrs,(uint8_t)1);
_gpioState = Wire.read();
}
}