Merge pull request #137 from biveraxe/ver4dev

Добавляем кнопку
This commit is contained in:
Dmitry Borisenko
2022-02-22 21:30:15 +01:00
committed by GitHub
14 changed files with 153 additions and 55 deletions

View File

@@ -304,7 +304,25 @@
"page": "",
"descr": "",
"int": "0",
"addr": "0x20",
"index": 1
},
{
"header": "Исполнительные устройства"
},
{
"name": "23. Кнопка управляющая пином (Реле с кнопкой)",
"num": 23,
"type": "Writing",
"subtype": "ButtonOut",
"id": "btn",
"widget": "button",
"page": "Кнопки",
"descr": "",
"int": 0,
"inv": 0,
"pin": 2
}
]

View File

@@ -29,6 +29,7 @@ class IoTItem {
IoTValue value; // хранение основного значения, котрое обновляется из сценария, execute(), loop() или doByInterval()
virtual IoTGpio* getGpioDriver();
virtual void setValue(IoTValue Value);
protected:
String _subtype;

View File

@@ -40,7 +40,7 @@ void IoTGpio::analogWrite(uint8_t pin, int val) {
if (_drivers[pinH]) _drivers[pinH]->analogWrite(pin - pinH*100, val);
else {
#ifdef ESP32
// todo: написать для esp32 аналог функции analogWrite
// TODO: написать для esp32 аналог функции analogWrite
#endif
#ifdef ESP8266
::analogWrite(pin, val);

View File

@@ -64,7 +64,7 @@ void IoTItem::regEvent(float value, String consoleInfo = "") {
value = value / _round;
}
// value = (float)value / (_round ? pow(10, (int)_round) : 1); // todo: решить как указывать округление, количество знаков после запятой или десятые сотые ...
// value = (float)value / (_round ? pow(10, (int)_round) : 1); // TODO: решить как указывать округление, количество знаков после запятой или десятые сотые ...
}
if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4);
@@ -90,4 +90,10 @@ IoTItem* myIoTItem;
IoTGpio* IoTItem::getGpioDriver() {
return nullptr;
}
void IoTItem::setValue(IoTValue Value) {
value = Value;
if (value.isDecimal) regEvent(value.valD, "");
else regEvent(value.valS, "");
}

View File

@@ -64,7 +64,10 @@ public:
VariableExprAST(const String &name, IoTItem* item) : Name(name), Item(item) {}
int setValue(IoTValue *val) {
Item->value = *val; // устанавливаем значение в связанном Item модуля напрямую
if (Item) {
//Item->value = *val; // устанавливаем значение в связанном Item модуля напрямую
Item->setValue(*val);
}
return 1;
}

View File

@@ -15,6 +15,7 @@ void* getAPI_Lcd2004(String subtype, String params);
void* getAPI_SysExt(String subtype, String params);
void* getAPI_Ads1115(String subtype, String params);
void* getAPI_Mcp23017(String subtype, String params);
void* getAPI_ButtonOut(String subtype, String params);
//============================================================================================
void* getAPI(String subtype, String params) {
@@ -34,6 +35,7 @@ void* getAPI(String subtype, String params) {
if ((tmpAPI = getAPI_SysExt(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Ads1115(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI;
//================================================================================================================
return nullptr;

View File

@@ -22,7 +22,7 @@ class Aht20t : public IoTItem {
void doByInterval() {
value.valD = temp.temperature;
if (value.valD != -200) regEvent(value.valD, "Aht20t"); // todo: найти способ понимания ошибки получения данных
if (value.valD != -200) regEvent(value.valD, "Aht20t"); // TODO: найти способ понимания ошибки получения данных
else SerialPrint("E", "Sensor AHTt", "Error");
}
@@ -36,7 +36,7 @@ class Aht20h : public IoTItem {
void doByInterval() {
value.valD = humidity.relative_humidity;
if (value.valD != -200) regEvent(value.valD, "Aht20h"); // todo: найти способ понимания ошибки получения данных
if (value.valD != -200) regEvent(value.valD, "Aht20h"); // TODO: найти способ понимания ошибки получения данных
else SerialPrint("E", "Sensor AHTt", "Error");
}

View File

@@ -73,21 +73,24 @@ class Bme280p : public IoTItem {
~Bme280p();
};
void* getAPI_Bme280(String subtype, String param) {
String addr;
jsonRead(param, "addr", addr);
if (bmes.find(addr) == bmes.end()) {
bmes[addr] = new Adafruit_BME280();
bmes[addr]->begin(hexStringToUint8(addr));
}
void* getAPI_Bme280(String subtype, String param) {
if (subtype == F("Bme280t") || subtype == F("Bme280h") || subtype == F("Bme280p")) {
String addr;
jsonRead(param, "addr", addr);
if (subtype == F("Bme280t")) {
return new Bme280t(bmes[addr], param);
} else if (subtype == F("Bme280h")) {
return new Bme280h(bmes[addr], param);
} else if (subtype == F("Bme280p")) {
return new Bme280p(bmes[addr], param);
if (bmes.find(addr) == bmes.end()) {
bmes[addr] = new Adafruit_BME280();
bmes[addr]->begin(hexStringToUint8(addr));
}
if (subtype == F("Bme280t")) {
return new Bme280t(bmes[addr], param);
} else if (subtype == F("Bme280h")) {
return new Bme280h(bmes[addr], param);
} else if (subtype == F("Bme280p")) {
return new Bme280p(bmes[addr], param);
}
} else {
return nullptr;
}

View File

@@ -55,18 +55,20 @@ class Bmp280p : public IoTItem {
void* getAPI_Bmp280(String subtype, String param) {
String addr;
jsonRead(param, "addr", addr);
if (subtype == F("Bmp280t") || subtype == F("Bmp280p")) {
String addr;
jsonRead(param, "addr", addr);
if (bmps.find(addr) == bmps.end()) {
bmps[addr] = new Adafruit_BMP280();
bmps[addr]->begin(hexStringToUint8(addr));
}
if (bmps.find(addr) == bmps.end()) {
bmps[addr] = new Adafruit_BMP280();
bmps[addr]->begin(hexStringToUint8(addr));
}
if (subtype == F("Bmp280t")) {
return new Bmp280t(bmps[addr], param);
} else if (subtype == F("Bmp280p")) {
return new Bmp280p(bmps[addr], param);
if (subtype == F("Bmp280t")) {
return new Bmp280t(bmps[addr], param);
} else if (subtype == F("Bmp280p")) {
return new Bmp280p(bmps[addr], param);
}
} else {
return nullptr;
}

58
src/modules/ButtonOut.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include "Global.h"
#include "classes/IoTItem.h"
extern IoTGpio IoTgpio;
class ButtonOut : public IoTItem {
private:
int _pin, _inv;
public:
ButtonOut(String parameters): IoTItem(parameters) {
jsonRead(parameters, "pin", _pin);
jsonRead(parameters, "inv", _inv);
IoTgpio.pinMode(_pin, OUTPUT);
//TODO: прочитать состояние из памяти
IoTgpio.digitalWrite(_pin, LOW); // пока нет памяти, устанавливаем значение в ноль
value.valD = 0;
}
void doByInterval() {
//value.valD = IoTgpio.analogRead(_pin);
//regEvent(value.valD, "ButtonOut"); //обязательный вызов хотяб один
}
IoTValue execute(String command, std::vector<IoTValue> &param) {
// реакция на вызов команды модуля из сценария
// String command - имя команды после ID. (ID.Команда())
// param - вектор ("массив") значений параметров переданных вместе с командой: ID.Команда("пар1", 22, 33) -> param[0].ValS = "пар1", param[1].ValD = 22
if (command == "change") { // выполняем код при вызове спец команды из сценария: ID.reboot();
value.valD = !IoTgpio.digitalRead(_pin);
IoTgpio.digitalWrite(_pin, value.valD);
}
return {}; // команда поддерживает возвращаемое значения. Т.е. по итогу выполнения команды или общения с внешней системой, можно вернуть значение в сценарий для дальнейшей обработки
}
void setValue(IoTValue Value) {
value = Value;
IoTgpio.digitalWrite(_pin, value.valD);
if (value.isDecimal) regEvent(value.valD, "ButtonOut");
else regEvent(value.valS, "ButtonOut");
}
//=======================================================================================================
~ButtonOut();
};
void* getAPI_ButtonOut(String subtype, String param) {
if (subtype == F("ButtonOut")) {
return new ButtonOut(param);
} else {
return nullptr;
}
}

View File

@@ -53,25 +53,27 @@ class Dht1122h : public IoTItem {
void* getAPI_Dht1122(String subtype, String param) {
int pin;
String senstype;
jsonRead(param, "pin", pin);
jsonRead(param, "senstype", senstype);
if (subtype == F("Dht1122t") || subtype == F("Dht1122h")) {
int pin;
String senstype;
jsonRead(param, "pin", pin);
jsonRead(param, "senstype", senstype);
if (dhts.find(pin) == dhts.end()) {
dhts[pin] = new DHTesp();
if (senstype == "dht11") {
dhts[pin]->setup(pin, DHTesp::DHT11);
} else if (senstype == "dht22") {
dhts[pin]->setup(pin, DHTesp::DHT22);
if (dhts.find(pin) == dhts.end()) {
dhts[pin] = new DHTesp();
if (senstype == "dht11") {
dhts[pin]->setup(pin, DHTesp::DHT11);
} else if (senstype == "dht22") {
dhts[pin]->setup(pin, DHTesp::DHT22);
}
}
}
if (subtype == F("Dht1122t")) {
return new Dht1122t(dhts[pin], param);
} else if (subtype == F("Dht1122h")) {
return new Dht1122h(dhts[pin], param);
if (subtype == F("Dht1122t")) {
return new Dht1122t(dhts[pin], param);
} else if (subtype == F("Dht1122h")) {
return new Dht1122h(dhts[pin], param);
}
} else {
return nullptr;
}

View File

@@ -20,7 +20,7 @@ class GY21t : public IoTItem {
void doByInterval() {
//wire->read();
value.valD = sensor->GY21_Temperature();
if (value.valD < 300) regEvent(value.valD, "GY21"); // todo: найти способ понимания ошибки получения данных
if (value.valD < 300) regEvent(value.valD, "GY21"); // TODO: найти способ понимания ошибки получения данных
else SerialPrint("E", "Sensor GY21t", "Error");
}
@@ -34,7 +34,7 @@ class GY21h : public IoTItem {
void doByInterval() {
//sht->read();
value.valD = sensor->GY21_Humidity();
if (value.valD != 0) regEvent(value.valD, "GY21h"); // todo: найти способ понимания ошибки получения данных
if (value.valD != 0) regEvent(value.valD, "GY21h"); // TODO: найти способ понимания ошибки получения данных
else SerialPrint("E", "Sensor GY21h", "Error");
}
@@ -42,16 +42,18 @@ class GY21h : public IoTItem {
};
void* getAPI_GY21(String subtype, String param) {
if (subtype == F("GY21t") || subtype == F("GY21h")) {
if (!sensor) {
sensor = new GY21;
if (sensor) Wire.begin(SDA, SCL);
sensor = new GY21;
if (sensor) Wire.begin(SDA, SCL);
}
if (subtype == F("GY21t")) {
return new GY21t(param);
} else if (subtype == F("GY21h")) {
return new GY21h(param);
} else {
return nullptr;
}
} else {
return nullptr;
}
}

View File

@@ -43,7 +43,6 @@ class Mcp23017 : public IoTItem, IoTGpio {
void* getAPI_Mcp23017(String subtype, String param) {
if (subtype == F("Mcp23017")) {
String addr;
uint8_t deviceAddress[1];
jsonRead(param, "addr", addr);
Serial.printf("deviceAddress %s = %02x \n", addr.c_str(), hexStringToUint8(addr));

View File

@@ -37,16 +37,18 @@ class Sht20h : public IoTItem {
void* getAPI_Sht20(String subtype, String param) {
if (subtype == F("Sht20t") || subtype == F("Sht20h")) {
if (!sht) {
sht = new SHT2x;
if (sht) sht->begin();
sht = new SHT2x;
if (sht) sht->begin();
}
if (subtype == F("Sht20t")) {
return new Sht20t(param);
} else if (subtype == F("Sht20h")) {
return new Sht20h(param);
} else {
return nullptr;
}
} else {
return nullptr;
}
}