forked from adafruit/Adafruit-MCP23008-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Adafruit_MCP23008.cpp
186 lines (151 loc) · 3.73 KB
/
Adafruit_MCP23008.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
/***************************************************
This is a library for the MCP23008 i2c port expander
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <avr/pgmspace.h>
#include "Adafruit_MCP23008.h"
////////////////////////////////////////////////////////////////////////////////
// RTC_DS1307 implementation
void Adafruit_MCP23008::begin(uint8_t addr) {
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
Wire.begin();
// set defaults!
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
#if ARDUINO >= 100
Wire.write((byte)MCP23008_IODIR);
Wire.write((byte)0xFF); // all inputs
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
Wire.write((byte)0x00);
#else
Wire.send(MCP23008_IODIR);
Wire.send(0xFF); // all inputs
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
Wire.send(0x00);
#endif
Wire.endTransmission();
}
void Adafruit_MCP23008::send(uint8_t pin_states) {
// write the new GPIO
write8(MCP23008_GPIO, pin_states);
}
void Adafruit_MCP23008::pins(uint8_t pin_modes) {
write8(MCP23008_IODIR, pin_modes);
}
void Adafruit_MCP23008::begin(void) {
begin(0);
}
void Adafruit_MCP23008::pinMode(uint8_t p, uint8_t d) {
uint8_t iodir;
// only 8 bits!
if (p > 7)
return;
iodir = read8(MCP23008_IODIR);
// set the pin and direction
if (d == INPUT) {
iodir |= 1 << p;
} else {
iodir &= ~(1 << p);
}
// write the new IODIR
write8(MCP23008_IODIR, iodir);
}
uint8_t Adafruit_MCP23008::readGPIO(void) {
// read the current GPIO input
return read8(MCP23008_GPIO);
}
void Adafruit_MCP23008::writeGPIO(uint8_t gpio) {
write8(MCP23008_GPIO, gpio);
}
void Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
uint8_t gpio;
// only 8 bits!
if (p > 7)
return;
// read the current GPIO output latches
gpio = readGPIO();
// set the pin and direction
if (d == HIGH) {
gpio |= 1 << p;
} else {
gpio &= ~(1 << p);
}
// write the new GPIO
writeGPIO(gpio);
}
void Adafruit_MCP23008::pullUp(uint8_t p, uint8_t d) {
uint8_t gppu;
// only 8 bits!
if (p > 7)
return;
gppu = read8(MCP23008_GPPU);
// set the pin and direction
if (d == HIGH) {
gppu |= 1 << p;
} else {
gppu &= ~(1 << p);
}
// write the new GPIO
write8(MCP23008_GPPU, gppu);
}
uint8_t Adafruit_MCP23008::digitalRead(uint8_t p) {
// only 8 bits!
if (p > 7)
return 0;
// read the current GPIO
return (readGPIO() >> p) & 0x1;
}
uint8_t Adafruit_MCP23008::read8(uint8_t addr) {
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
#if ARDUINO >= 100
Wire.write((byte)addr);
#else
Wire.send(addr);
#endif
Wire.endTransmission();
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
void Adafruit_MCP23008::write8(uint8_t addr, uint8_t data) {
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
#if ARDUINO >= 100
Wire.write((byte)addr);
Wire.write((byte)data);
#else
Wire.send(addr);
Wire.send(data);
#endif
Wire.endTransmission();
}