From 6b47aac0215e2e4461b43fa538c5c377fd24ab06 Mon Sep 17 00:00:00 2001 From: biver Date: Mon, 3 Oct 2022 01:02:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20PCF8574?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/exec/Pcf8574/Pcf8574.cpp | 87 +++++++++++++++++++++++++++ src/modules/exec/Pcf8574/modinfo.json | 44 ++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/modules/exec/Pcf8574/Pcf8574.cpp create mode 100644 src/modules/exec/Pcf8574/modinfo.json diff --git a/src/modules/exec/Pcf8574/Pcf8574.cpp b/src/modules/exec/Pcf8574/Pcf8574.cpp new file mode 100644 index 00000000..8d661e1f --- /dev/null +++ b/src/modules/exec/Pcf8574/Pcf8574.cpp @@ -0,0 +1,87 @@ +#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. + */ + +void scanI2C(); + +class Pcf8574Driver : public IoTGpio { + private: + Adafruit_PCF8574 _pcf; + + public: + Pcf8574Driver(int index, String addr) : IoTGpio(index) { + if (!_pcf.begin(hexStringToUint8(addr))) { + Serial.println("PCF8574 Init Error."); + } + } + + void pinMode(uint8_t pin, uint8_t mode) { + _pcf.pinMode(pin, mode); + } + + void digitalWrite(uint8_t pin, uint8_t val) { + _pcf.digitalWrite(pin, val); + } + + int digitalRead(uint8_t pin) { + return _pcf.digitalRead(pin); + } + + void digitalInvert(uint8_t pin) { + _pcf.digitalWrite(pin, 1 - _pcf.digitalRead(pin)); + } + + ~Pcf8574Driver() {}; +}; + + +class Pcf8574 : public IoTItem { + private: + Pcf8574Driver* _driver; + + public: + Pcf8574(String parameters) : IoTItem(parameters) { + _driver = nullptr; + + String addr; + jsonRead(parameters, "addr", addr); + if (addr == "") { + scanI2C(); + return; + } + + int index; + jsonRead(parameters, "index", index); + if (index > 4) { + Serial.println("Pcf8574 wrong index. Must be 0 - 4"); + return; + } + + _driver = new Pcf8574Driver(index, addr); + } + + void doByInterval() {} + + //возвращает ссылку на экземпляр класса Pcf8574Driver + IoTGpio* getGpioDriver() { + return _driver; + } + + ~Pcf8574() { + delete _driver; + }; +}; + +void* getAPI_Pcf8574(String subtype, String param) { + if (subtype == F("Pcf8574")) { + return new Pcf8574(param); + } else { + return nullptr; + } +} diff --git a/src/modules/exec/Pcf8574/modinfo.json b/src/modules/exec/Pcf8574/modinfo.json new file mode 100644 index 00000000..ad66adb4 --- /dev/null +++ b/src/modules/exec/Pcf8574/modinfo.json @@ -0,0 +1,44 @@ +{ + "menuSection": "Исполнительные устройства", + + "configItem": [{ + "name": "Расширитель портов Pcf8574", + "type": "Reading", + "subtype": "Pcf8574", + "id": "Pcf", + "widget": "", + "page": "", + "descr": "", + "int": "0", + "addr": "0x20", + "index": 1 + }], + + "about": { + "authorName": "Serghei Crasnicov", + "authorContact": "https://t.me/Serghei63", + "authorGit": "https://github.com/Serghei63", + "specialThanks": "Ilya Belyakov @Biveraxe , @AEV_LAB ", + "moduleName": "Pcf8574", + "moduleVersion": "1.0", + "moduleDesc": "Добавляет в систему дополнительные GPIO для элементов, которые поддерживают такую функцию.", + "propInfo": { + "int": "Не используется", + "addr": "Адрес устройства на шине, обычно 0x20", + "index": "Значения от 1 до 4, где при выборе 1 будет нумерация pin 100-115, при выборе 2 200-215 и т.д." + } + }, + + "defActive": true, + + "devices": { + "esp32_4mb": [ + "adafruit/Adafruit PCF8574@^1.0.0", + "adafruit/Adafruit BusIO @ ^1.13.0" + ], + "esp8266_4mb": [ + "adafruit/Adafruit PCF8574@^1.0.0", + "adafruit/Adafruit BusIO @ ^1.13.0" + ] + } +} \ No newline at end of file