From 2b1cac904b91728ec416594ef75d161b728c2e60 Mon Sep 17 00:00:00 2001 From: biver Date: Wed, 12 Jan 2022 18:02:43 +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=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D0=BB=D1=8F=20Display=20TM1637=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D1=83=20=D0=BD=D0=B0=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B7=D0=B5=20=D1=83=D0=B6=D0=B5=20=D1=81=D1=83=D1=89?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B2=D1=83=D1=8E=D1=89=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?LCD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Consts.h | 1 + include/items/vSensorTM1637.h | 40 +++++++++++ platformio.ini | 5 ++ src/items/vSensorTM1637.cpp | 128 ++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 include/items/vSensorTM1637.h create mode 100644 src/items/vSensorTM1637.cpp 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..a60ca2d5 --- /dev/null +++ b/include/items/vSensorTM1637.h @@ -0,0 +1,40 @@ +#ifdef EnableSensorTM1637 +#pragma once +#include +#include +#include "Global.h" +#include "LiquidCrystal_I2C.h" + + +class SensorTM1637; + +typedef std::vector MySensorTM1637Vector; + +class SensorTM1637 { + public: + SensorTM1637(String key, unsigned long interval, unsigned int x, unsigned int y, String val, String descr); + ~SensorTM1637(); + + void loop(); + void writeTM1637(); + void execute(String command); + String _key; + void printBlankStr(int strSize); + + private: + unsigned long currentMillis; + unsigned long prevMillis; + unsigned long difference; + + unsigned long _interval; + unsigned int _x; + unsigned int _y; + String _val; + String _descr; + int _prevStrSize; +}; + +extern MySensorTM1637Vector* mySensorTM16372; + +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/items/vSensorTM1637.cpp b/src/items/vSensorTM1637.cpp new file mode 100644 index 00000000..48dfaaeb --- /dev/null +++ b/src/items/vSensorTM1637.cpp @@ -0,0 +1,128 @@ +#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 + +LiquidCrystal_I2C *LCDI2C; + +SensorTM1637::SensorTM1637(String key, unsigned long interval, unsigned int x, unsigned int y, String val, String descr) { + _key = key; + _interval = interval * 1000; + _x = x; + _y = y; + _val = val; + _descr = descr; + _prevStrSize = 0; +} + +SensorTM1637::~SensorTM1637() {} + +//печать пустой строки нужной длинны для затирания предыдущего значения на экране +void SensorTM1637::printBlankStr(int strSize){ + String tmpStr = ""; + for(int i=0; isetCursor(_x, _y); + LCDI2C->print(tmpStr); +} + +void SensorTM1637::execute(String command) { + if (command == "noBacklight") LCDI2C->noBacklight(); + else if (command == "backlight") LCDI2C->backlight(); + else if (command == "noDisplay") LCDI2C->noDisplay(); + else if (command == "display") LCDI2C->display(); + else if (command == "x") { + printBlankStr(_prevStrSize); + String par = sCmd.next(); + _x = par.toInt(); + } + else if (command == "y") { + printBlankStr(_prevStrSize); + String par = sCmd.next(); + _y = par.toInt(); + } + else if (command == "descr") { + printBlankStr(_prevStrSize); + 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 (LCDI2C != nullptr) { + printBlankStr(_prevStrSize); + + String tmpStr = getValue(_val); + if (tmpStr == "no value") tmpStr = _val; + if (_descr != "none") tmpStr = _descr + " " + tmpStr; + LCDI2C->setCursor(_x, _y); + LCDI2C->print(tmpStr); + + _prevStrSize = tmpStr.length(); + } +} + +MySensorTM1637Vector* mySensorTM16372 = nullptr; + +void TM1637Execute() { + String key = sCmd.order(); + String command = sCmd.next(); + + for (unsigned int i = 0; i < mySensorTM16372->size(); i++) { + if (mySensorTM16372->at(i)._key == key) mySensorTM16372->at(i).execute(command); + } +} + +void TM1637() { + myLineParsing.update(); + String key = myLineParsing.gkey(); + String addr = myLineParsing.gaddr(); + String interval = myLineParsing.gint(); + String c = myLineParsing.gc(); + String k = myLineParsing.gk(); + String val = myLineParsing.gval(); + String descr = myLineParsing.gdescr(); + myLineParsing.clear(); + + int x = selectFromMarkerToMarker(c, ",", 0).toInt(); + int y = selectFromMarkerToMarker(c, ",", 1).toInt(); + int w = selectFromMarkerToMarker(k, ",", 0).toInt(); //количество столбцов + int h = selectFromMarkerToMarker(k, ",", 1).toInt(); //количество строк + + if (LCDI2C == nullptr) { //инициализации экрана еще не было + LCDI2C = new LiquidCrystal_I2C(hexStringToUint8(addr), w, h);//hexStringToUint8(addr), w, h); + if(LCDI2C != nullptr) { + LCDI2C->init(); + LCDI2C->backlight(); + } + } + + + static bool firstTime = true; + if (firstTime) mySensorTM16372 = new MySensorTM1637Vector(); + firstTime = false; + mySensorTM16372->push_back(SensorTM1637(key, interval.toInt(), x, y, val, descr)); + + sCmd.addCommand(key.c_str(), TM1637Execute); +} + +#endif