From 5e45b29c2f2cd960e5d7cd923e9c4289cfd26bde Mon Sep 17 00:00:00 2001 From: biver Date: Mon, 3 Oct 2022 12:06:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D1=83=20Ada?= =?UTF-8?q?fruit=5FPCF8574=20=D0=BF=D1=80=D0=B8=20=D0=B8=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/exec/Pcf8574/Pcf8574.cpp | 66 ++++++++++++++++++++++++--- src/modules/exec/Pcf8574/modinfo.json | 4 +- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/modules/exec/Pcf8574/Pcf8574.cpp b/src/modules/exec/Pcf8574/Pcf8574.cpp index 8d661e1f..3eabb9ff 100644 --- a/src/modules/exec/Pcf8574/Pcf8574.cpp +++ b/src/modules/exec/Pcf8574/Pcf8574.cpp @@ -1,22 +1,73 @@ #include "Global.h" #include "classes/IoTItem.h" #include "classes/IoTGpio.h" -#include -/* Example for 8 input buttons that are connected from the GPIO expander pins to ground. - * Note the buttons must be connected with the other side of the switch to GROUND. There is - * a built in pull-up 'resistor' on each input, but no pull-down resistor capability. - */ +#include +#include + +#define PCF8574_I2CADDR_DEFAULT 0x20 ///< DS3502 default I2C address void scanI2C(); +class Adafruit_PCF8574_mod { + public: + Adafruit_PCF8574_mod() {}; + bool begin(uint8_t i2c_address = PCF8574_I2CADDR_DEFAULT, TwoWire *wire = &Wire) { + i2c_dev = new Adafruit_I2CDevice(i2c_address, wire); + + if (!i2c_dev->begin()) { + return false; + } + + return true; + } + + bool digitalWriteByte(uint8_t d) { + _writebuf = d; + return i2c_dev->write(&_writebuf, 1); + } + + uint8_t digitalReadByte(void) { + i2c_dev->read(&_readbuf, 1); + return _readbuf; + } + + bool digitalWrite(uint8_t pinnum, bool val) { + if (val) { + _writebuf |= 1 << pinnum; + } else { + _writebuf &= ~(1 << pinnum); + } + return i2c_dev->write(&_writebuf, 1); + } + + bool pinMode(uint8_t pinnum, uint8_t val) { + if ((val == INPUT) || (val == INPUT_PULLUP)) { + _writebuf |= 1 << pinnum; + } else { + _writebuf &= ~(1 << pinnum); + } + return i2c_dev->write(&_writebuf, 1); + } + + bool digitalRead(uint8_t pinnum) { + i2c_dev->read(&_readbuf, 1); + return (_readbuf >> pinnum) & 0x1; + } + + private: + uint8_t _readbuf = 0, _writebuf = 0; + + Adafruit_I2CDevice *i2c_dev; +}; + class Pcf8574Driver : public IoTGpio { private: - Adafruit_PCF8574 _pcf; + Adafruit_PCF8574_mod _pcf; public: Pcf8574Driver(int index, String addr) : IoTGpio(index) { - if (!_pcf.begin(hexStringToUint8(addr))) { + if (!_pcf.begin(hexStringToUint8(addr), &Wire)) { Serial.println("PCF8574 Init Error."); } } @@ -31,6 +82,7 @@ class Pcf8574Driver : public IoTGpio { int digitalRead(uint8_t pin) { return _pcf.digitalRead(pin); + //return 0; } void digitalInvert(uint8_t pin) { diff --git a/src/modules/exec/Pcf8574/modinfo.json b/src/modules/exec/Pcf8574/modinfo.json index ad66adb4..4408fd16 100644 --- a/src/modules/exec/Pcf8574/modinfo.json +++ b/src/modules/exec/Pcf8574/modinfo.json @@ -34,11 +34,11 @@ "devices": { "esp32_4mb": [ "adafruit/Adafruit PCF8574@^1.0.0", - "adafruit/Adafruit BusIO @ ^1.13.0" + "adafruit/Adafruit BusIO @ ^1.13.2" ], "esp8266_4mb": [ "adafruit/Adafruit PCF8574@^1.0.0", - "adafruit/Adafruit BusIO @ ^1.13.0" + "adafruit/Adafruit BusIO @ ^1.13.2" ] } } \ No newline at end of file