mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 20:09:14 +03:00
Добавляем модуль PCF8574
This commit is contained in:
87
src/modules/exec/Pcf8574/Pcf8574.cpp
Normal file
87
src/modules/exec/Pcf8574/Pcf8574.cpp
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#include "Global.h"
|
||||||
|
#include "classes/IoTItem.h"
|
||||||
|
#include "classes/IoTGpio.h"
|
||||||
|
#include <Adafruit_PCF8574.h>
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/modules/exec/Pcf8574/modinfo.json
Normal file
44
src/modules/exec/Pcf8574/modinfo.json
Normal file
@@ -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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user