Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding support to read a single bank at a time. #3

Closed
wants to merge 1 commit into from

2 participants

@hydra

The diff also includes whitespace cleanups performed by my editor.

@tdicola tdicola closed this in a45e9a6
@tdicola
Owner

Thanks for submitting the fix, and again sorry it wasn't reviewed sooner. I couldn't do a direct merge because of some other fixes integrated in, but I pulled out the new function and added it in a separate commit. Thanks again for sending the pull!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2012
  1. @hydra
This page is out of date. Refresh to see the latest.
Showing with 49 additions and 30 deletions.
  1. +42 −24 Adafruit_MCP23017.cpp
  2. +7 −6 Adafruit_MCP23017.h
View
66 Adafruit_MCP23017.cpp
@@ -1,13 +1,13 @@
-/***************************************************
+/***************************************************
This is a library for the MCP23017 i2c port expander
- These displays use I2C to communicate, 2 pins are required to
+ 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
+ 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.
+ Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
@@ -48,7 +48,7 @@ void Adafruit_MCP23017::begin(uint8_t addr) {
Wire.begin();
-
+
// set defaults!
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_IODIRA);
@@ -83,15 +83,15 @@ void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) {
// read the current IODIR
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
- wiresend(iodiraddr);
+ wiresend(iodiraddr);
Wire.endTransmission();
-
+
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
iodir = wirerecv();
// set the pin and direction
if (d == INPUT) {
- iodir |= 1 << p;
+ iodir |= 1 << p;
} else {
iodir &= ~(1 << p);
}
@@ -99,7 +99,7 @@ void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) {
// write the new IODIR
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(iodiraddr);
- wiresend(iodir);
+ wiresend(iodir);
Wire.endTransmission();
}
@@ -109,9 +109,9 @@ uint16_t Adafruit_MCP23017::readGPIOAB() {
// read the current GPIO output latches
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
- wiresend(MCP23017_GPIOA);
+ wiresend(MCP23017_GPIOA);
Wire.endTransmission();
-
+
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 2);
a = wirerecv();
ba = wirerecv();
@@ -121,9 +121,27 @@ uint16_t Adafruit_MCP23017::readGPIOAB() {
return ba;
}
+uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) {
+ uint8_t gpioaddr;
+
+ if (b == 0)
+ gpioaddr = MCP23017_GPIOA;
+ else {
+ gpioaddr = MCP23017_GPIOB;
+ }
+
+ // read the current GPIO output latches
+ Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
+ wiresend(gpioaddr);
+ Wire.endTransmission();
+
+ Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
+ return wirerecv();
+}
+
void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) {
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
- wiresend(MCP23017_GPIOA);
+ wiresend(MCP23017_GPIOA);
wiresend(ba & 0xFF);
wiresend(ba >> 8);
Wire.endTransmission();
@@ -148,15 +166,15 @@ void Adafruit_MCP23017::digitalWrite(uint8_t p, uint8_t d) {
// read the current GPIO output latches
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
- wiresend(olataddr);
+ wiresend(olataddr);
Wire.endTransmission();
-
+
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
gpio = wirerecv();
// set the pin and direction
if (d == HIGH) {
- gpio |= 1 << p;
+ gpio |= 1 << p;
} else {
gpio &= ~(1 << p);
}
@@ -164,7 +182,7 @@ void Adafruit_MCP23017::digitalWrite(uint8_t p, uint8_t d) {
// write the new GPIO
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(gpioaddr);
- wiresend(gpio);
+ wiresend(gpio);
Wire.endTransmission();
}
@@ -186,15 +204,15 @@ void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) {
// read the current pullup resistor set
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
- wiresend(gppuaddr);
+ wiresend(gppuaddr);
Wire.endTransmission();
-
+
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
gppu = wirerecv();
// set the pin and direction
if (d == HIGH) {
- gppu |= 1 << p;
+ gppu |= 1 << p;
} else {
gppu &= ~(1 << p);
}
@@ -202,7 +220,7 @@ void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) {
// write the new GPIO
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(gppuaddr);
- wiresend(gppu);
+ wiresend(gppu);
Wire.endTransmission();
}
@@ -222,9 +240,9 @@ uint8_t Adafruit_MCP23017::digitalRead(uint8_t p) {
// read the current GPIO
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
- wiresend(gpioaddr);
+ wiresend(gpioaddr);
Wire.endTransmission();
-
+
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
return (wirerecv() >> p) & 0x1;
-}
+}
View
13 Adafruit_MCP23017.h
@@ -1,13 +1,13 @@
-/***************************************************
+/***************************************************
This is a library for the MCP23017 i2c port expander
- These displays use I2C to communicate, 2 pins are required to
+ 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
+ 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.
+ Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
@@ -27,6 +27,7 @@ class Adafruit_MCP23017 {
void writeGPIOAB(uint16_t);
uint16_t readGPIOAB();
+ uint8_t readGPIO(uint8_t b);
private:
uint8_t i2caddr;
@@ -60,4 +61,4 @@ class Adafruit_MCP23017 {
#define MCP23017_GPIOB 0x13
#define MCP23017_OLATB 0x15
-#endif
+#endif
Something went wrong with that request. Please try again.