Trying to integrate in my program a PCF8575 i2c GPIO expander to read inputs from 3 buttons.
I followed and slightly modified an example I found online: http://www.instructables.com/id/Using-T ... d-Inputs-/
I created a C++ class named Buttons with a pressed method to indicate, depending on the byte value read from the expander, which pin of the expander the signal comes from. As a signal, get an int = 2^n (where n is the pin number as indicated on the GPIO expander).
Here is a snippet from my code (Buttons.cpp):
Code: Select all
#include "Buttons.h"
#include <Arduino.h>
#include <Wire.h>
#include "../defines.h"
void Buttons::init() {
pcf8575_write(word(B00000000, B00000000));
DBG("Buttons initialized");
}
bool Buttons::pressed(int button) {
uint16_t dataReceive;
if (pcf8575_read(dataReceive))
return (dataReceive == button);
else
return false;
}
void Buttons::pcf8575_write(uint16_t dt) {
uint8_t error;
Wire.beginTransmission(PF_ADDR);
Wire.write(lowByte(dt));
Wire.write(highByte(dt));
error = Wire.endTransmission();
if (error == ku8TWISuccess)
DBG("success initializing PF8575");
else {
DBG("error writing to PF8575, check connections");
}
}
bool Buttons::pcf8575_read(uint16_t &result) {
uint8_t hi, lo, error;
Wire.beginTransmission(PF_ADDR);
error = Wire.endTransmission();
if (error == ku8TWISuccess) {
Wire.requestFrom(PF_ADDR, 2);
if (Wire.available()) {
lo = Wire.read();
hi = Wire.read();
result = (word(hi, lo));
return true;
} else {
DBG("error: Wire not available");
return false;
}
} else {
DBG("error reading PF8575, check connections");
return false;
}
}
Code: Select all
if (buttons.pressed(MINUS) && buttons.pressed(OK)) {
sm.trigger("ADMIN");
}
Any ideas how to fix this? I was looking at interrupts too, unable to make it work yet with this expander.
Thanks!