diff --git a/data_esp/items/items.txt b/data_esp/items/items.txt index 4da9efd9..64a71812 100644 --- a/data_esp/items/items.txt +++ b/data_esp/items/items.txt @@ -38,5 +38,6 @@ 0;sensor;anyid;anydata;Сенсоры;Параметр;1;c[1];int[10];type[HDC1080_hum];addr[0x76]* 0;sensor;anyid;anydata;Сенсоры;Параметр;1;c[1];int[10];type[AHTX0_temp];addr[0x76] 0;sensor;anyid;anydata;Сенсоры;Параметр;1;c[1];int[10];type[AHTX0_hum];addr[0x76]* -0;LCD2004;lcdid;anydata;Вывод;IP;1;addr[0x27];k[16,2];int[10];c[0,0];val[ip]* +0;LCD2004;lcdid;na;na;IP;1;addr[0x27];k[16,2];int[10];c[0,0];val[ip]* +0;TM1637;dispid;na;na;f;1;pin[12,13];int[10];c[4];k[0];val[1234]* 0;sensor;anyid;anydata;Сенсоры;Параметр;1;c[1];int[10];type[type1];addr[0x76]* \ No newline at end of file diff --git a/data_esp/set.device.json.gz b/data_esp/set.device.json.gz index 329723f7..d9fbdf01 100644 Binary files a/data_esp/set.device.json.gz and b/data_esp/set.device.json.gz differ diff --git a/include/Consts.h b/include/Consts.h index 02df2cb4..86e072da 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -88,6 +88,7 @@ #define EnableTelegram #define EnableUart #define EnableSensorLCD2004 +#define EnableSensorTM1637 #endif #ifdef GATE_MODE diff --git a/include/items/vSensorTM1637.h b/include/items/vSensorTM1637.h new file mode 100644 index 00000000..866dbb14 --- /dev/null +++ b/include/items/vSensorTM1637.h @@ -0,0 +1,44 @@ +#ifdef EnableSensorTM1637 +#pragma once +#include +#include +#include "Global.h" +#include + +struct DisplayObj { + TM1637Display* disp; + int curIndex; +}; + +class SensorTM1637; + +typedef std::vector MySensorTM1637Vector; + +class SensorTM1637 { + public: + SensorTM1637(String key, int pin1, int pin2, unsigned long interval, unsigned int c, unsigned int k, String val, String descr); + ~SensorTM1637(); + + void loop(); + void writeTM1637(); + void execute(String command); + String _key; + + private: + unsigned long currentMillis; + unsigned long prevMillis; + unsigned long difference; + + String _descr; + unsigned long _interval; + unsigned int _c; + unsigned int _k; + String _val; + + TM1637Display* _disp; +}; + +extern MySensorTM1637Vector* mySensorTM1637; + +extern void TM1637(); +#endif diff --git a/platformio.ini b/platformio.ini index bc2ece4d..98a65ade 100644 --- a/platformio.ini +++ b/platformio.ini @@ -45,6 +45,7 @@ lib_deps = Adafruit AHTX0 BH1750 marcoschwartz/LiquidCrystal_I2C@^1.1.4 + smougenot/TM1637@0.0.0-alpha+sha.9486982048 monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -73,6 +74,7 @@ lib_deps = Adafruit AHTX0 BH1750 marcoschwartz/LiquidCrystal_I2C@^1.1.4 + smougenot/TM1637@0.0.0-alpha+sha.9486982048 monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -91,6 +93,7 @@ lib_deps = CTBot @2.1.6 MySensors @2.3.2 marcoschwartz/LiquidCrystal_I2C@^1.1.4 + smougenot/TM1637@0.0.0-alpha+sha.9486982048 monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -120,6 +123,7 @@ lib_deps = Adafruit AHTX0 BH1750 marcoschwartz/LiquidCrystal_I2C@^1.1.4 + smougenot/TM1637@0.0.0-alpha+sha.9486982048 monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -141,6 +145,7 @@ lib_deps = MySensors @2.3.2 fmalpartida/LiquidCrystal@^1.5.0 marcoschwartz/LiquidCrystal_I2C@^1.1.4 + smougenot/TM1637@0.0.0-alpha+sha.9486982048 monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 diff --git a/src/BufferExecute.cpp b/src/BufferExecute.cpp index 4c2b483f..74817482 100644 --- a/src/BufferExecute.cpp +++ b/src/BufferExecute.cpp @@ -17,6 +17,7 @@ #include "items/vSensorCcs811.h" #include "items/vSensorDallas.h" #include "items/vSensorLCD2004.h" +#include "items/vSensorTM1637.h" #include "items/vSensorDht.h" #include "items/vSensorNode.h" #include "items/vSensorPzem.h" @@ -110,6 +111,10 @@ void csvCmdExecute(String& cmdStr) { } else if (order == F("LCD2004")) { #ifdef EnableSensorLCD2004 sCmd.addCommand(order.c_str(), lcd2004); +#endif + } else if (order == F("TM1637")) { +#ifdef EnableSensorTM1637 + sCmd.addCommand(order.c_str(), TM1637); #endif } else if (order == F("dht")) { #ifdef EnableSensorDht diff --git a/src/Init.cpp b/src/Init.cpp index 19781682..d55dd099 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -17,6 +17,7 @@ #include "items/vSensorCcs811.h" #include "items/vSensorDallas.h" #include "items/vSensorLCD2004.h" +#include "items/vSensorTM1637.h" #include "items/vSensorDht.h" #include "items/vSensorNode.h" #include "items/vSensorPzem.h" @@ -177,8 +178,12 @@ void clearVectors() { #ifdef EnableSensorLCD2004 if (mySensorLCD20042 != nullptr) { if(LCDI2C != nullptr) LCDI2C->clear(); - mySensorLCD20042->clear(); - + mySensorLCD20042->clear(); + } +#endif +#ifdef EnableSensorTM1637 + if (mySensorTM1637 != nullptr) { + mySensorTM1637->clear(); } #endif #ifdef EnableSensorUltrasonic diff --git a/src/items/vSensorTM1637.cpp b/src/items/vSensorTM1637.cpp new file mode 100644 index 00000000..c59153bd --- /dev/null +++ b/src/items/vSensorTM1637.cpp @@ -0,0 +1,124 @@ +#include "Consts.h" +#ifdef EnableSensorTM1637 +#include "items/vSensorTM1637.h" +#include "BufferExecute.h" +#include "Class/LineParsing.h" +#include "Global.h" +#include "Utils/StringUtils.h" +#include + +#include + +const uint8_t segmentsVal[] = {0x77, 0x7f, 0x39, 0x3f, 0x79, 0x71, 0x3d, 0x76, 0x1e, 0x38, 0x37, 0x3f, 0x73, 0x6d, 0x3e, 0x6e, 0x5f, 0x7c, 0x58, 0x5e, 0x7b, 0x71, 0x74, 0x10, 0x0e, 0x06, 0x54, 0x5c, 0x67, 0x50, 0x78, 0x1c, 0x6e, 0x40, 0x08, 0x48, 0x00, 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; +char segmentsIndex[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'L', 'N', 'O', 'P', 'S', 'U', 'Y', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'l', 'n', 'o', 'q', 'r', 't', 'u', 'y', '-', '_', '=', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + + +std::map displayObjects; + +uint8_t char2Segment(char ch) { + for (int i=0; isetBrightness(0x0f); + _disp->clear(); + } else { + _disp = displayObjects[pin1].disp; + } +} + +SensorTM1637::~SensorTM1637() {} + +void SensorTM1637::execute(String command) { + if (command == "noDisplay") _disp->setBrightness(0x00, false); + else if (command == "display") _disp->setBrightness(0x0f, true); + else if (command == "setBrightness") { + String par = sCmd.next(); + _disp->setBrightness(par.toInt()); + } + else if (command == "descr") { + String par = sCmd.next(); + _descr = par; + } + else { //не команда, значит данные + _val = command; + } + + writeTM1637(); +} + +void SensorTM1637::loop() { + currentMillis = millis(); + difference = currentMillis - prevMillis; + if (difference >= _interval) { + prevMillis = millis(); + writeTM1637(); + } +} + +void SensorTM1637::writeTM1637() { + if (_disp != nullptr) { + if (_descr != "none") { + uint8_t segments[] = {0}; + segments[0] = char2Segment(_descr.c_str()[0]); + _disp->setSegments(segments, 1, 0); //выводим поле описания в самом первой секции экрана, один символ + } + String tmpStr = getValue(_val); + if (tmpStr == "no value") tmpStr = _val; + + _disp->showNumberDec(tmpStr.toInt(), false, _c, _k); + } +} + +MySensorTM1637Vector* mySensorTM1637 = nullptr; + +void TM1637Execute() { + String key = sCmd.order(); + String command = sCmd.next(); + + for (unsigned int i = 0; i < mySensorTM1637->size(); i++) { + if (mySensorTM1637->at(i)._key == key) mySensorTM1637->at(i).execute(command); + } +} + +void TM1637() { + myLineParsing.update(); + String key = myLineParsing.gkey(); + String pins = myLineParsing.gpin(); + String interval = myLineParsing.gint(); + String c = myLineParsing.gc(); + String k = myLineParsing.gk(); + String val = myLineParsing.gval(); + String descr = myLineParsing.gdescr(); + myLineParsing.clear(); + + int pin1 = selectFromMarkerToMarker(pins, ",", 0).toInt(); + int pin2 = selectFromMarkerToMarker(pins, ",", 1).toInt(); + + static bool firstTime = true; + if (firstTime) mySensorTM1637 = new MySensorTM1637Vector(); + firstTime = false; + mySensorTM1637->push_back(SensorTM1637(key, pin1, pin2, interval.toInt(), c.toInt(), k.toInt(), val, descr)); + + sCmd.addCommand(key.c_str(), TM1637Execute); +} + +#endif diff --git a/src/main.cpp b/src/main.cpp index 274d5e23..c2385e92 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ - #include #include "BufferExecute.h" @@ -30,6 +29,7 @@ #include "items/vSensorCcs811.h" #include "items/vSensorDallas.h" #include "items/vSensorLCD2004.h" +#include "items/vSensorTM1637.h" #include "items/vSensorDht.h" #include "items/vSensorNode.h" #include "items/vSensorPzem.h" @@ -161,6 +161,13 @@ void loop() { } } #endif +#ifdef EnableSensorTM1637 + if (mySensorTM1637 != nullptr) { + for (unsigned int i = 0; i < mySensorTM1637->size(); i++) { + mySensorTM1637->at(i).loop(); + } + } +#endif #ifdef EnableSensorUltrasonic if (mySensorUltrasonic != nullptr) { for (unsigned int i = 0; i < mySensorUltrasonic->size(); i++) {