bombatuino/source/libraries/bombatuino_INPUT_MCP23017/bombatuino_INPUT_MCP23017.cpp

119 lines
2.9 KiB
C++
Raw Normal View History

2013-02-14 02:59:15 +01:00
#include <Wire.h>
#include "Arduino.h"
2013-03-12 14:53:27 +01:00
#include "bombatuino_INPUT_MCP23017.h"
2013-02-14 02:59:15 +01:00
void INPUT_MCP23017::begin(uint8_t addr,CallbackFunction cbF) {
Wire.begin();
2013-02-14 12:23:57 +01:00
//check hardware address
2013-02-14 02:59:15 +01:00
if (addr > 7)
_addr = 7;
else _addr = addr;
_callbackFunction = cbF;
2013-02-14 03:12:31 +01:00
//set all ports as inputs
2013-02-14 02:59:15 +01:00
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write((byte)MCP23017_IODIR_A); //PORT A
2013-02-14 12:23:57 +01:00
Wire.write(0xFF);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_IODIR_B); //PORT B
2013-02-14 12:23:57 +01:00
Wire.write(0xFF);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
2013-02-14 03:12:31 +01:00
//activate pullup resistors
2013-02-14 02:59:15 +01:00
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPPU_A); //PORT A
2013-02-14 12:23:57 +01:00
Wire.write(0xFF);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPPU_B); //PORT B
2013-02-14 12:23:57 +01:00
Wire.write(0xFF);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
2013-02-14 03:12:31 +01:00
//inverse all inputs
2013-02-14 02:59:15 +01:00
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write((byte)MCP23017_IPOL_A); //PORT A
2013-02-14 12:23:57 +01:00
Wire.write(0xFF);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_IPOL_B); //PORT B
2013-02-14 12:23:57 +01:00
Wire.write(0xFF);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
//init start values
uint8_t pin,bank;
//read bank A
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPIO_A);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=0; pin<8; pin++)
_value[pin] = (bank >> pin) & 0x1;
//read bank B
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPIO_B);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=8; pin<16; pin++)
_value[pin] = (bank >> (pin-8)) & 0x1;
}
void INPUT_MCP23017::loop() {
uint8_t pin,bank;
int value;
//read bank A
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPIO_A);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=0; pin<8; pin++) {
value = (bank >> pin) & 0x1;
if (_value[pin] != value) {
_value[pin] = value;
(*_callbackFunction)(_addr,pin,value);
}
}
//read bank B
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPIO_B);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
bank = Wire.read();
for (pin=8; pin<16; pin++) {
value = (bank >> (pin-8)) & 0x1;
if (_value[pin] != value) {
_value[pin] = value;
(*_callbackFunction)(_addr,pin,value);
}
}
}
2013-02-14 03:12:31 +01:00
//maybe useful later
2013-02-14 02:59:15 +01:00
int INPUT_MCP23017::getSpecificValue(uint8_t pin) {
if (pin > 16)
return LOW;
Wire.beginTransmission(MCP23017_ADDRESS | _addr);
uint8_t p = pin;
if (pin > 8) {
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPIO_B);
2013-02-14 02:59:15 +01:00
p -= 8;
} else
2013-03-12 14:53:27 +01:00
Wire.write(MCP23017_GPIO_A);
2013-02-14 02:59:15 +01:00
Wire.endTransmission();
uint8_t bank = Wire.read();
int value = (bank >> p) & 0x1;
_value[pin] = value;
return value;
}