From 217cdfc080d2a85add98953f7190b8dd651757d3 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Fri, 31 Jul 2020 00:56:10 +0200 Subject: [PATCH] Buttons done --- include/Class/{Item.h => LineParsing.h} | 117 ++++++++++++------------ include/Class/button.h | 45 +++++---- src/Class/Button.cpp | 3 +- src/Class/Item.cpp | 4 - src/Cmd.cpp | 91 +++++++----------- 5 files changed, 117 insertions(+), 143 deletions(-) rename include/Class/{Item.h => LineParsing.h} (57%) delete mode 100644 src/Class/Item.cpp diff --git a/include/Class/Item.h b/include/Class/LineParsing.h similarity index 57% rename from include/Class/Item.h rename to include/Class/LineParsing.h index f1afa218..aabdd3ec 100644 --- a/include/Class/Item.h +++ b/include/Class/LineParsing.h @@ -1,38 +1,39 @@ #pragma once #include -#include "Class/Button.h" #include "Global.h" -class Item { +class LineParsing { protected: - String type; - String key; - String file; - String page; - String descr; - String order; + String _type; + String _key; + String _file; + String _page; + String _descr; + String _order; - String addr; - String pin; - String map; - String c; - String inv; - String state; + String _addr; + String _pin; + String _map; + String _c; + String _inv; + String _state; public: - Item() : type{""}, - key{""}, - file{""}, - page{""}, - descr{""}, - order{""}, - addr{""}, - pin{""}, - map{""}, - c{""}, - inv{""}, - state{""} + LineParsing() : + + _type{""}, + _key{""}, + _file{""}, + _page{""}, + _descr{""}, + _order{""}, + _addr{""}, + _pin{""}, + _map{""}, + _c{""}, + _inv{""}, + _state{""} {}; @@ -40,72 +41,72 @@ class Item { //String order = sCmd.order(); //pm.info("create '" + order + "'"); for (int i = 1; i < 12; i++) { - if (i == 1) type = sCmd.next(); - if (i == 2) key = sCmd.next(); - if (i == 3) file = sCmd.next(); - if (i == 4) page = sCmd.next(); - if (i == 5) descr = sCmd.next(); - if (i == 6) order = sCmd.next(); + if (i == 1) _type = sCmd.next(); + if (i == 2) _key = sCmd.next(); + if (i == 3) _file = sCmd.next(); + if (i == 4) _page = sCmd.next(); + if (i == 5) _descr = sCmd.next(); + if (i == 6) _order = sCmd.next(); } for (int i = 1; i < 6; i++) { String arg = sCmd.next(); if (arg != "") { if (arg.indexOf("pin[") != -1) { - pin = extractInner(arg); + _pin = extractInner(arg); } if (arg.indexOf("inv[") != -1) { - inv = extractInner(arg); + _inv = extractInner(arg); } if (arg.indexOf("st[") != -1) { - state = extractInner(arg); + _state = extractInner(arg); } } } - createWidgetClass(descr, page, order, file, key); + createWidgetClass(_descr, _page, _order, _file, _key); } String gtype() { - return type; + return _type; } String gkey() { - return key; + return _key; } String gfile() { - return file; + return _file; } String gpage() { - return page; + return _page; } String gdescr() { - return descr; + return _descr; } String gorder() { - return order; + return _order; } String gpin() { - return pin; + return _pin; } String ginv() { - return inv; + return _inv; } String gstate() { - return state; + return _state; } void clear() { - type = ""; - key = ""; - file = ""; - page = ""; - descr = ""; - order = ""; - addr = ""; - pin = ""; - map = ""; - c = ""; - inv = ""; - state = ""; + _type = ""; + _key = ""; + _file = ""; + _page = ""; + _descr = ""; + _order = ""; + _addr = ""; + _pin = ""; + _map = ""; + _c = ""; + _inv = ""; + _state = ""; } String extractInnerDigit(String str) { @@ -147,5 +148,3 @@ class Item { return "/widgets/" + name + ".json"; } }; - -extern Item* myItem; \ No newline at end of file diff --git a/include/Class/button.h b/include/Class/button.h index 7eb4a80e..8e4a55a4 100644 --- a/include/Class/button.h +++ b/include/Class/button.h @@ -1,35 +1,44 @@ #pragma once #include - -#include "Class/Item.h" +#include "Class/LineParsing.h" #include "Global.h" -class Button : public Item { +class Button : public LineParsing { public: - Button() : Item() {}; + Button() : LineParsing(){}; - void pinModeSet(int pinf) { - if (pin != "") { - pinMode(pinf, OUTPUT); + void pinModeSet() { + if (_pin != "") { + Serial.println(_pin); + pinMode(_pin.toInt(), OUTPUT); } } - void pinStateSet(int pinf, int statef) { - if (state != "") { - digitalWrite(pinf, statef); - jsonWriteInt(configLiveJson, key, statef); - MqttClient::publishStatus(key, String(statef)); + void pinStateSetDefault() { + if (_inv == "" && _state != "") { + pinChange(_key, _pin, _state, true); } } - void pinStateSetInv(int pinf, int statef) { - if (inv != "" && state != "") { - digitalWrite(pinf, !statef); - jsonWriteInt(configLiveJson, key, !statef); - MqttClient::publishStatus(key, String(!statef)); + void pinStateSetInvDefault() { + if (_inv != "" && _state != "") { + pinChange(_key, _pin, _state, false); } } + + void pinChange(String key, String pin, String state, bool rev) { + int pinInt = pin.toInt(); + int stateInt; + if (rev) { + digitalWrite(pinInt, state.toInt()); + } else { + digitalWrite(pinInt, !state.toInt()); + } + eventGen(key, ""); + jsonWriteInt(configLiveJson, key, state.toInt()); + MqttClient::publishStatus(key, state); + } }; -extern Button* myClass; \ No newline at end of file +extern Button* myButton; \ No newline at end of file diff --git a/src/Class/Button.cpp b/src/Class/Button.cpp index 199acaff..b97e7b54 100644 --- a/src/Class/Button.cpp +++ b/src/Class/Button.cpp @@ -1,4 +1,3 @@ #include "Class/Button.h" - -Button* myClass; \ No newline at end of file +Button* myButton; \ No newline at end of file diff --git a/src/Class/Item.cpp b/src/Class/Item.cpp deleted file mode 100644 index 04b24dee..00000000 --- a/src/Class/Item.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "Class/Item.h" - - -Item* myItem; \ No newline at end of file diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 584feea1..c24ab29f 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -1,7 +1,7 @@ #include "Cmd.h" -#include "Class/Item.h" #include "Class/Button.h" +#include "Class/LineParsing.h" #include "Global.h" #include "Module/Terminal.h" #include "Servo/Servos.h" @@ -101,63 +101,35 @@ void cmd_init() { handle_time_init(); - myItem = new Item(); - myClass = new Button(); + myButton = new Button(); } //==========================================Модуль кнопок=================================================== //button out light toggle Кнопки Свет 1 pin[12] inv[1] st[1] //========================================================================================================== void button() { - myItem->update(); - String key = myItem->gkey(); - String pin = myItem->gpin(); - String inv = myItem->ginv(); - String state = myItem->gstate(); - - + myButton->update(); + String key = myButton->gkey(); + String pin = myButton->gpin(); + String inv = myButton->ginv(); sCmd.addCommand(key.c_str(), buttonSet); - - - myClass->pinModeSet(pin.toInt()); - - if (pin != "") { - pinMode(pin.toInt(), OUTPUT); - jsonWriteInt(configOptionJson, key + "_pin", pin.toInt()); - } - - if (inv != "") { - digitalWrite(pin.toInt(), !state.toInt()); - jsonWriteStr(configLiveJson, key, state); - MqttClient::publishStatus(key, state); - } - - if (state != "") { - digitalWrite(pin.toInt(), state.toInt()); - jsonWriteStr(configLiveJson, key, state); - MqttClient::publishStatus(key, state); - } - myItem->clear(); + jsonWriteStr(configOptionJson, key + "_pin", pin); + jsonWriteStr(configOptionJson, key + "_inv", inv); + myButton->pinModeSet(); + myButton->pinStateSetDefault(); + myButton->pinStateSetInvDefault(); + myButton->clear(); } void buttonSet() { String key = sCmd.order(); String state = sCmd.next(); - int pin = jsonReadInt(configOptionJson, key + "_pin"); - - if (state == "change") { - int newState = !digitalRead(pin); - digitalWrite(pin, newState); - eventGen(key, ""); - jsonWriteStr(configLiveJson, key, String(newState)); - MqttClient::publishStatus(key, String(newState)); - } - - if (state == "0" || state == "1") { - digitalWrite(pin, state.toInt()); - eventGen(key, ""); - jsonWriteStr(configLiveJson, key, state); - MqttClient::publishStatus(key, state); + String pin = jsonReadStr(configOptionJson, key + "_pin"); + String inv = jsonReadStr(configOptionJson, key + "_inv"); + if (inv == "") { + myButton->pinChange(key, pin, state, true); + } else { + myButton->pinChange(key, pin, state, false); } } @@ -165,20 +137,20 @@ void buttonSet() { //pwm out volume range Кнопки Свет 1 pin[12] st[500] //================================================================================================================== void pwm() { - myItem->update(); - String key = myItem->gkey(); - String pin = myItem->gpin(); - String state = myItem->gstate(); - myItem->clear(); + //line->update(); + //String key = line->gkey(); + //String pin = line->gpin(); + //String state = line->gstate(); + //line->clear(); - sCmd.addCommand(key.c_str(), pwmSet); - - if (pin != "") { - jsonWriteInt(configOptionJson, key + "_pin", pin.toInt()); - analogWrite(pin.toInt(), state.toInt()); - jsonWriteInt(configLiveJson, key, state.toInt()); - MqttClient::publishStatus(key, String(state)); - } + //sCmd.addCommand(key.c_str(), pwmSet); + // + //if (pin != "") { + // jsonWriteInt(configOptionJson, key + "_pin", pin.toInt()); + // analogWrite(pin.toInt(), state.toInt()); + // jsonWriteInt(configLiveJson, key, state.toInt()); + // MqttClient::publishStatus(key, String(state)); + //} } void pwmSet() { @@ -192,7 +164,6 @@ void pwmSet() { MqttClient::publishStatus(key, state); } - //================================================================================================================== //==========================================Модуль физической кнопки================================================ void switch_() {