Skip to content
This repository was archived by the owner on Jun 26, 2021. It is now read-only.

Commit 46de2f3

Browse files
authored
Merge pull request #14 from adafruit/busio
Busio
2 parents 20a59bd + 4281703 commit 46de2f3

4 files changed

+99
-137
lines changed

Adafruit_MCP23008.cpp

+82-87
Original file line numberDiff line numberDiff line change
@@ -22,74 +22,59 @@
2222
* BSD license, all text above must be included in any redistribution
2323
*/
2424

25-
#if ARDUINO >= 100
26-
#include "Arduino.h"
27-
#else
28-
#include "WProgram.h"
29-
#endif
30-
#ifdef __AVR_ATtiny85__
31-
#include <TinyWireM.h>
32-
#define Wire TinyWireM
33-
#else
34-
#include <Wire.h>
35-
#endif
36-
37-
#ifdef __AVR
38-
#include <avr/pgmspace.h>
39-
#elif defined(ESP8266)
40-
#include <pgmspace.h>
41-
#endif
4225
#include "Adafruit_MCP23008.h"
4326

44-
////////////////////////////////////////////////////////////////////////////////
45-
// RTC_DS1307 implementation
27+
///////////////////////////////////////////////////////////////////////////////
4628

47-
void Adafruit_MCP23008::begin(uint8_t addr) {
48-
if (addr > 7) {
49-
addr = 7;
29+
/*!
30+
* @brief Begins the i2c connection using specified address
31+
* @param addr i2c address of the MCP23008, defaults to 0x20
32+
* @param wire TwoWire interface, defaults to &Wire
33+
* @returns False if failed to find device at address
34+
*/
35+
bool Adafruit_MCP23008::begin(uint8_t addr, TwoWire *wire) {
36+
if ((addr >= 0x20) && (addr <= 0x27)) {
37+
_i2caddr = addr;
38+
} else if (addr <= 0x07) {
39+
_i2caddr = 0x20 + addr;
40+
} else {
41+
_i2caddr = 0x27;
5042
}
51-
i2caddr = addr;
5243

53-
Wire.begin();
44+
if (i2c_dev) {
45+
delete i2c_dev; // remove old interface
46+
}
47+
48+
i2c_dev = new Adafruit_I2CDevice(_i2caddr, wire);
49+
50+
if (!i2c_dev->begin()) {
51+
return false;
52+
}
5453

5554
// set defaults!
56-
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
57-
#if ARDUINO >= 100
58-
Wire.write((byte)MCP23008_IODIR);
59-
Wire.write((byte)0xFF); // all inputs
60-
Wire.write((byte)0x00);
61-
Wire.write((byte)0x00);
62-
Wire.write((byte)0x00);
63-
Wire.write((byte)0x00);
64-
Wire.write((byte)0x00);
65-
Wire.write((byte)0x00);
66-
Wire.write((byte)0x00);
67-
Wire.write((byte)0x00);
68-
Wire.write((byte)0x00);
69-
#else
70-
Wire.send(MCP23008_IODIR);
71-
Wire.send(0xFF); // all inputs
72-
Wire.send(0x00);
73-
Wire.send(0x00);
74-
Wire.send(0x00);
75-
Wire.send(0x00);
76-
Wire.send(0x00);
77-
Wire.send(0x00);
78-
Wire.send(0x00);
79-
Wire.send(0x00);
80-
Wire.send(0x00);
81-
#endif
82-
Wire.endTransmission();
83-
}
55+
uint8_t buf[] = {MCP23008_IODIR,
56+
0xFF, // all inputs
57+
0x00, 0x00, 0x00, 0x00, 0x00,
58+
0x00, 0x00, 0x00, 0x00};
59+
if (!i2c_dev->write(buf, 10)) {
60+
return false;
61+
}
8462

85-
void Adafruit_MCP23008::begin(void) { begin(0); }
63+
return true;
64+
}
8665

87-
void Adafruit_MCP23008::pinMode(uint8_t p, uint8_t d) {
66+
/*!
67+
* @brief Sets the pin mode
68+
* @param p Mode to set
69+
* @param d Pin to set the mode to
70+
* @returns True on success, False on bad input or I2C failure
71+
*/
72+
bool Adafruit_MCP23008::pinMode(uint8_t p, uint8_t d) {
8873
uint8_t iodir;
8974

9075
// only 8 bits!
9176
if (p > 7)
92-
return;
77+
return false;
9378

9479
iodir = read8(MCP23008_IODIR);
9580

@@ -101,22 +86,39 @@ void Adafruit_MCP23008::pinMode(uint8_t p, uint8_t d) {
10186
}
10287

10388
// write the new IODIR
104-
write8(MCP23008_IODIR, iodir);
89+
return write8(MCP23008_IODIR, iodir);
10590
}
10691

92+
/*!
93+
* @brief Reads the current GPIO input
94+
* @return Returns the current GPIO input
95+
*/
10796
uint8_t Adafruit_MCP23008::readGPIO(void) {
10897
// read the current GPIO input
10998
return read8(MCP23008_GPIO);
11099
}
111100

112-
void Adafruit_MCP23008::writeGPIO(uint8_t gpio) { write8(MCP23008_GPIO, gpio); }
101+
/*!
102+
* @brief Writes to the GPIO
103+
* @param gpio what to write
104+
* @returns True on success, False on bad input or I2C failure
105+
*/
106+
bool Adafruit_MCP23008::writeGPIO(uint8_t gpio) {
107+
return write8(MCP23008_GPIO, gpio);
108+
}
113109

114-
void Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
110+
/*!
111+
* @brief Sets the pin and direction
112+
* @param p Pin to set
113+
* @param d Direction to set the pin
114+
* @returns True on success, False on bad input or I2C failure
115+
*/
116+
bool Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
115117
uint8_t gpio;
116118

117119
// only 8 bits!
118120
if (p > 7)
119-
return;
121+
return false;
120122

121123
// read the current GPIO output latches
122124
gpio = readGPIO();
@@ -129,15 +131,21 @@ void Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
129131
}
130132

131133
// write the new GPIO
132-
writeGPIO(gpio);
134+
return writeGPIO(gpio);
133135
}
134136

135-
void Adafruit_MCP23008::pullUp(uint8_t p, uint8_t d) {
137+
/*!
138+
* @brief Sets pull-up resistor on specified pin
139+
* @param p Pin to set
140+
* @param d Direction to set the pin
141+
* @returns True on success, False on bad input or I2C failure
142+
*/
143+
bool Adafruit_MCP23008::pullUp(uint8_t p, uint8_t d) {
136144
uint8_t gppu;
137145

138146
// only 8 bits!
139147
if (p > 7)
140-
return;
148+
return false;
141149

142150
gppu = read8(MCP23008_GPPU);
143151
// set the pin and direction
@@ -147,9 +155,14 @@ void Adafruit_MCP23008::pullUp(uint8_t p, uint8_t d) {
147155
gppu &= ~(1 << p);
148156
}
149157
// write the new GPIO
150-
write8(MCP23008_GPPU, gppu);
158+
return write8(MCP23008_GPPU, gppu);
151159
}
152160

161+
/*!
162+
* @brief Reads the status of a gpio pin
163+
* @param p Pin to read
164+
* @return Returns the current gpio
165+
*/
153166
uint8_t Adafruit_MCP23008::digitalRead(uint8_t p) {
154167
// only 8 bits!
155168
if (p > 7)
@@ -160,30 +173,12 @@ uint8_t Adafruit_MCP23008::digitalRead(uint8_t p) {
160173
}
161174

162175
uint8_t Adafruit_MCP23008::read8(uint8_t addr) {
163-
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
164-
#if ARDUINO >= 100
165-
Wire.write((byte)addr);
166-
#else
167-
Wire.send(addr);
168-
#endif
169-
Wire.endTransmission();
170-
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
171-
172-
#if ARDUINO >= 100
173-
return Wire.read();
174-
#else
175-
return Wire.receive();
176-
#endif
176+
Adafruit_BusIO_Register reg = Adafruit_BusIO_Register(i2c_dev, addr, 1);
177+
return reg.read();
177178
}
178179

179-
void Adafruit_MCP23008::write8(uint8_t addr, uint8_t data) {
180-
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
181-
#if ARDUINO >= 100
182-
Wire.write((byte)addr);
183-
Wire.write((byte)data);
184-
#else
185-
Wire.send(addr);
186-
Wire.send(data);
187-
#endif
188-
Wire.endTransmission();
180+
bool Adafruit_MCP23008::write8(uint8_t addr, uint8_t data) {
181+
Adafruit_BusIO_Register reg = Adafruit_BusIO_Register(i2c_dev, addr, 1);
182+
183+
return reg.write(data);
189184
}

Adafruit_MCP23008.h

+13-49
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,32 @@
44

55
#ifndef _ADAFRUIT_MCP23008_H
66
#define _ADAFRUIT_MCP23008_H
7-
// Don't forget the Wire library
8-
#ifdef __AVR_ATtiny85__
9-
#include <TinyWireM.h>
10-
#else
11-
#include <Wire.h>
12-
#endif
7+
8+
#include <Adafruit_BusIO_Register.h>
9+
#include <Adafruit_I2CDevice.h>
10+
#include <Arduino.h>
1311

1412
/*!
1513
* @brief Class that stores state and functions for interacting with MCP23008
1614
* chip
1715
*/
1816
class Adafruit_MCP23008 {
1917
public:
20-
/*!
21-
* @brief Begins the i2c connection using specified address
22-
* @param addr i2c address of the MCP23008
23-
*/
24-
void begin(uint8_t addr);
25-
/*!
26-
* @brief Begins the i2c connection using default address
27-
*/
28-
void begin(void);
18+
bool begin(uint8_t addr = 0x20, TwoWire *wire = &Wire);
2919

30-
/*!
31-
* @brief Sets the pin mode
32-
* @param p Mode to set
33-
* @param d Pin to set the mode to
34-
*/
35-
void pinMode(uint8_t p, uint8_t d);
36-
/*!
37-
* @brief Sets the pin and direction
38-
* @param p Pin to set
39-
* @param d Direction to set the pin
40-
*/
41-
void digitalWrite(uint8_t p, uint8_t d);
42-
/*!
43-
* @brief Sets pull-up resistor on specified pin
44-
* @param p Pin to set
45-
* @param d Direction to set the pin
46-
*/
47-
void pullUp(uint8_t p, uint8_t d);
48-
/*!
49-
* @brief Reads the status of a gpio pin
50-
* @param p Pin to read
51-
* @return Returns the current gpio
52-
*/
20+
bool pinMode(uint8_t p, uint8_t d);
21+
bool digitalWrite(uint8_t p, uint8_t d);
22+
bool pullUp(uint8_t p, uint8_t d);
5323
uint8_t digitalRead(uint8_t p);
54-
/*!
55-
* @brief Reads the current GPIO input
56-
* @return Returns the current GPIO input
57-
*/
5824
uint8_t readGPIO(void);
59-
/*!
60-
* @brief Writes to the GPIO
61-
* @param gpio what to write
62-
*/
63-
void writeGPIO(uint8_t);
25+
bool writeGPIO(uint8_t);
6426

6527
private:
66-
uint8_t i2caddr;
28+
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
29+
30+
uint8_t _i2caddr;
6731
uint8_t read8(uint8_t addr);
68-
void write8(uint8_t addr, uint8_t data);
32+
bool write8(uint8_t addr, uint8_t data);
6933
};
7034

7135
#define MCP23008_ADDRESS 0x20 //!< MCP23008 serial address

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ To download, click "Download Source" in the top right corner. Then install as in
44
--> http://www.ladyada.net/library/arduino/libraries.html
55
in a folder called Adafruit_MCP23008.
66

7+
You will also need the Adafruit BusIO library: https://github.com/adafruit/Adafruit_BusIO
8+
79
This is very much beta, it seems to work fine but its not optimized and doesn't currently suport the interrupt capability of the chip
810

911
Currently it can do: setting pin directions, inputs and outputs and turning on/off the pull-up resistors

library.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name=Adafruit MCP23008 library
2-
version=1.1.0
2+
version=2.0.0
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Arduino Library for the MCP23008 (and '9) I2C I/O expander
66
paragraph=Arduino Library for the MCP23008 (and '9) I2C I/O expander
77
category=Signal Input/Output
88
url=https://github.com/adafruit/Adafruit-MCP23008-library
99
architectures=*
10+
depends=Adafruit BusIO

0 commit comments

Comments
 (0)