From 7acf14c12ad86739ae92299b537e668707670a37 Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk Date: Thu, 9 Jan 2020 23:51:25 +0200 Subject: [PATCH 1/7] initial esphome example --- examples/esphome/.gitignore | 10 ++++++ examples/esphome/TM1638-nodemcu-demo.yaml | 43 +++++++++++++++++++++++ examples/esphome/TM1638Sensor.h | 43 +++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 examples/esphome/.gitignore create mode 100644 examples/esphome/TM1638-nodemcu-demo.yaml create mode 100644 examples/esphome/TM1638Sensor.h diff --git a/examples/esphome/.gitignore b/examples/esphome/.gitignore new file mode 100644 index 0000000..0bbf89c --- /dev/null +++ b/examples/esphome/.gitignore @@ -0,0 +1,10 @@ +# Gitignore settings for ESPHome +# This is an example and may include too much for your use-case. +# You can modify this file to suit your needs. +/.esphome/ +**/.pioenvs/ +**/.piolibdeps/ +**/lib/ +**/src/ +**/platformio.ini +/secrets.yaml diff --git a/examples/esphome/TM1638-nodemcu-demo.yaml b/examples/esphome/TM1638-nodemcu-demo.yaml new file mode 100644 index 0000000..7fb6f6d --- /dev/null +++ b/examples/esphome/TM1638-nodemcu-demo.yaml @@ -0,0 +1,43 @@ +esphome: + name: tm1638_nodemcu_lolin + platform: ESP8266 + board: nodemcuv2 + includes: +# - ../../src/TM1638lite.h +# - ../../src/TM1638lite.cpp + - TM1638Sensor.h + libraries: + - TM1638lite + +logger: + + +custom_component: +- lambda: |- + static auto my_component = new TM1638Component(); + App.register_component(my_component); + return {my_component}; + id: tm1638 + + +binary_sensor: + - platform: custom + lambda: |- + TM1638Component *c = static_cast(const_cast(&tm1638)->get_component(0)); + return { c->leds}; + + binary_sensors: + name: "M1638 buttons" + + + +output: + - platform: custom + type: binary + lambda: |- + TM1638Component *c = static_cast(const_cast(&tm1638)->get_component(0)); + return { c->buttons }; + + outputs: + id: tm_buttons +# name: "TM1638 led" diff --git a/examples/esphome/TM1638Sensor.h b/examples/esphome/TM1638Sensor.h new file mode 100644 index 0000000..fd97ec6 --- /dev/null +++ b/examples/esphome/TM1638Sensor.h @@ -0,0 +1,43 @@ +#include "esphome.h" +//#include "switch.h" +#include "TM1638lite.h" + + +//esphome::lcd_base::LCDDisplay + +using namespace esphome; +//using namespace esphome::switch_; + + + +class TM1638Led : public BinaryOutput { + public: + + void write_state(bool state) override { +// digitalWrite(5, state); + } +}; + +class MyCustomBinarySensor : public BinarySensor { + public: +}; + +class TM1638Component : public PollingComponent { + public: + TM1638lite tm = TM1638lite(4, 7, 8); + std::vector buttons; + std::vector leds; + + TM1638Component() : PollingComponent(5000) { } + + void setup() override { + tm.reset(); + } + + void update() override { + // This is the actual sensor reading logic. + //float temperature = bmp.readTemperature(); + //temperature_sensor->publish_state(temperature); + + } +}; From b12309454468ebde0c2fa148e63c852a1aa9b8e1 Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk Date: Fri, 10 Jan 2020 01:43:00 +0200 Subject: [PATCH 2/7] works on pins 12 14 4 --- examples/esphome/TM1638-nodemcu-demo.yaml | 13 +++++-- examples/esphome/TM1638Sensor.h | 44 ++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/examples/esphome/TM1638-nodemcu-demo.yaml b/examples/esphome/TM1638-nodemcu-demo.yaml index 7fb6f6d..a4bf751 100644 --- a/examples/esphome/TM1638-nodemcu-demo.yaml +++ b/examples/esphome/TM1638-nodemcu-demo.yaml @@ -24,10 +24,17 @@ binary_sensor: - platform: custom lambda: |- TM1638Component *c = static_cast(const_cast(&tm1638)->get_component(0)); - return { c->leds}; + return { c->buttons }; binary_sensors: - name: "M1638 buttons" + - name: "M1638 buttons 1" + - name: "M1638 buttons 2" + - name: "M1638 buttons 3" + - name: "M1638 buttons 4" + - name: "M1638 buttons 5" + - name: "M1638 buttons 6" + - name: "M1638 buttons 7" + - name: "M1638 buttons 8" @@ -36,7 +43,7 @@ output: type: binary lambda: |- TM1638Component *c = static_cast(const_cast(&tm1638)->get_component(0)); - return { c->buttons }; + return { c->leds }; outputs: id: tm_buttons diff --git a/examples/esphome/TM1638Sensor.h b/examples/esphome/TM1638Sensor.h index fd97ec6..40a8a99 100644 --- a/examples/esphome/TM1638Sensor.h +++ b/examples/esphome/TM1638Sensor.h @@ -8,36 +8,70 @@ using namespace esphome; //using namespace esphome::switch_; - +class TM1638Component; class TM1638Led : public BinaryOutput { public: + int i = 0; + TM1638Component *parent = nullptr; void write_state(bool state) override { // digitalWrite(5, state); + } }; class MyCustomBinarySensor : public BinarySensor { public: + int i = 0; + TM1638Component *parent = nullptr; }; class TM1638Component : public PollingComponent { public: - TM1638lite tm = TM1638lite(4, 7, 8); - std::vector buttons; - std::vector leds; + TM1638lite tm = TM1638lite(12, 14, 4); // arduino pin numbers strobe/clock/data + std::vector buttons; + std::vector leds; - TM1638Component() : PollingComponent(5000) { } + TM1638Component() : PollingComponent(100) { + for (uint8_t position = 0; position < 8; position++) { + buttons.push_back(new MyCustomBinarySensor); + leds.push_back(new TM1638Led); + } + } void setup() override { + ESP_LOGI("tm1651", "Creation"); tm.reset(); + + tm.displayText("Hell0 !!"); + tm.setLED(0, 1); } void update() override { +// return; + static uint8_t btns = 0; + uint8_t buttons = tm.readButtons(); + if (btns != buttons) { + doLEDs(buttons); + ESP_LOGI("tm1651", " update %d", buttons); + btns = buttons; + } +// ESP_LOGI("tm1651", " u %d", buttons); + // This is the actual sensor reading logic. //float temperature = bmp.readTemperature(); //temperature_sensor->publish_state(temperature); } + + void doLEDs(uint8_t value) { + for (uint8_t position = 0; position < 8; position++) { + tm.setLED(position, value & 1); + buttons[position]->publish_state(value & 1); + + value = value >> 1; + } + } + }; From 237785a68c40398d8d6d13f35c57b353570d0f9f Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk Date: Fri, 10 Jan 2020 02:18:14 +0200 Subject: [PATCH 3/7] Fixed init error. --- examples/esphome/TM1638Sensor.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/esphome/TM1638Sensor.h b/examples/esphome/TM1638Sensor.h index 40a8a99..924490d 100644 --- a/examples/esphome/TM1638Sensor.h +++ b/examples/esphome/TM1638Sensor.h @@ -17,11 +17,11 @@ class TM1638Led : public BinaryOutput { void write_state(bool state) override { // digitalWrite(5, state); - +// parent.setLed(i, state); } }; -class MyCustomBinarySensor : public BinarySensor { +class TM1638Button : public BinarySensor { public: int i = 0; TM1638Component *parent = nullptr; @@ -32,11 +32,18 @@ class TM1638Component : public PollingComponent { TM1638lite tm = TM1638lite(12, 14, 4); // arduino pin numbers strobe/clock/data std::vector buttons; std::vector leds; + std::vector buttons_; + std::vector leds_; TM1638Component() : PollingComponent(100) { for (uint8_t position = 0; position < 8; position++) { - buttons.push_back(new MyCustomBinarySensor); - leds.push_back(new TM1638Led); + auto btn = new TM1638Button; + buttons_.push_back(btn); + buttons.push_back(btn); + auto led = new TM1638Led; + leds_.push_back(led); + leds.push_back(led); + led->i = btn->i = position; } } @@ -46,11 +53,13 @@ class TM1638Component : public PollingComponent { tm.displayText("Hell0 !!"); tm.setLED(0, 1); + + update(); } void update() override { // return; - static uint8_t btns = 0; + static uint8_t btns = 3; uint8_t buttons = tm.readButtons(); if (btns != buttons) { doLEDs(buttons); @@ -69,7 +78,7 @@ class TM1638Component : public PollingComponent { for (uint8_t position = 0; position < 8; position++) { tm.setLED(position, value & 1); buttons[position]->publish_state(value & 1); - +// leds[position]->set_state(value & 1); value = value >> 1; } } From 99b9b52160cea6942fcce2202dd2c7f4558632e6 Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk Date: Sat, 22 Feb 2020 00:33:13 +0200 Subject: [PATCH 4/7] added set text service and led switches --- examples/esphome/TM1638-nodemcu-demo.yaml | 35 ++++++++--- examples/esphome/TM1638Sensor.h | 76 ++++++++++++++++++----- 2 files changed, 88 insertions(+), 23 deletions(-) diff --git a/examples/esphome/TM1638-nodemcu-demo.yaml b/examples/esphome/TM1638-nodemcu-demo.yaml index a4bf751..9b570bc 100644 --- a/examples/esphome/TM1638-nodemcu-demo.yaml +++ b/examples/esphome/TM1638-nodemcu-demo.yaml @@ -9,8 +9,30 @@ esphome: libraries: - TM1638lite + +wifi: + networks: + ssid: "esphassio" + bssid: "42:31:3C:DF:4B:43" + password: "21212121" + hidden: True + + +captive_portal: + +# Enable logging logger: +# Enable Home Assistant API +api: + password: "chugaister" + +ota: + password: "chugaister" + +time: + - platform: homeassistant + id: homeassistant_time custom_component: - lambda: |- @@ -38,13 +60,10 @@ binary_sensor: -output: - - platform: custom - type: binary - lambda: |- +switch: +- platform: custom + lambda: |- TM1638Component *c = static_cast(const_cast(&tm1638)->get_component(0)); return { c->leds }; - - outputs: - id: tm_buttons -# name: "TM1638 led" + switches: + name: "tm1638led" \ No newline at end of file diff --git a/examples/esphome/TM1638Sensor.h b/examples/esphome/TM1638Sensor.h index 924490d..cfaa604 100644 --- a/examples/esphome/TM1638Sensor.h +++ b/examples/esphome/TM1638Sensor.h @@ -1,6 +1,7 @@ #include "esphome.h" //#include "switch.h" #include "TM1638lite.h" +#include //esphome::lcd_base::LCDDisplay @@ -10,15 +11,20 @@ using namespace esphome; class TM1638Component; -class TM1638Led : public BinaryOutput { +class TM1638Led : public Switch, public Component { public: - int i = 0; - TM1638Component *parent = nullptr; + const int i; + TM1638lite &tm; + + TM1638Led(TM1638lite &t, const int n, const std::string name): Switch(name), i(n), tm(t) + { + } void write_state(bool state) override { -// digitalWrite(5, state); -// parent.setLed(i, state); + tm.setLED(i, state & 1); + publish_state(state); } + }; class TM1638Button : public BinarySensor { @@ -27,38 +33,79 @@ class TM1638Button : public BinarySensor { TM1638Component *parent = nullptr; }; -class TM1638Component : public PollingComponent { +class TM1638Component : public PollingComponent, public CustomAPIDevice { public: TM1638lite tm = TM1638lite(12, 14, 4); // arduino pin numbers strobe/clock/data std::vector buttons; - std::vector leds; + std::vector leds; std::vector buttons_; std::vector leds_; + char txt[10]; + int show_text = 0; + int scroll = 0; TM1638Component() : PollingComponent(100) { for (uint8_t position = 0; position < 8; position++) { auto btn = new TM1638Button; buttons_.push_back(btn); buttons.push_back(btn); - auto led = new TM1638Led; + + + const char *n ="012345678"; + char name[] = "led "; + name[3]=n[position]; + + auto led = new TM1638Led(tm, position, name); + App.register_switch(led); leds_.push_back(led); leds.push_back(led); - led->i = btn->i = position; } + strncpy(txt, "Hello ", 10); } void setup() override { ESP_LOGI("tm1651", "Creation"); tm.reset(); - tm.displayText("Hell0 !!"); - tm.setLED(0, 1); + tm.displayText(txt); +// tm.setLED(0, 1); + + register_service(&TM1638Component::on_set_text, "display_text", {"text", "time"}); update(); } + + void on_set_text(std::string text, int time) + { + strncpy(txt, text.c_str(), std::min(text.size(), size_t(10))); + tm.displayText(txt); + show_text = time; + } + + void do_scroll() + { + if (show_text == 0) { + auto time = id(homeassistant_time).now(); + time.strftime(txt, 10, "%T"); +/* char first = txt[0]; + for (int i = 0; i != 7; ++i) + { + txt[i] = txt[i+1]; + } + txt[7] = first;*/ + tm.displayText(txt); + } + else { + --show_text; + } + } void update() override { // return; + if (scroll < 0) { + scroll = 10; + do_scroll(); + } static uint8_t btns = 3; uint8_t buttons = tm.readButtons(); if (btns != buttons) { @@ -71,14 +118,13 @@ class TM1638Component : public PollingComponent { // This is the actual sensor reading logic. //float temperature = bmp.readTemperature(); //temperature_sensor->publish_state(temperature); - + --scroll; } void doLEDs(uint8_t value) { for (uint8_t position = 0; position < 8; position++) { - tm.setLED(position, value & 1); - buttons[position]->publish_state(value & 1); -// leds[position]->set_state(value & 1); + tm.setLED(position, value & 1 || leds[position]->state); + buttons_[position]->publish_state(value); value = value >> 1; } } From 6f4eb8eb20003f6148fbddecf25a512cbfe04be6 Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk <25319+sashao@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:17:32 +0200 Subject: [PATCH 5/7] Update TM1638-nodemcu-demo.yaml --- examples/esphome/TM1638-nodemcu-demo.yaml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/esphome/TM1638-nodemcu-demo.yaml b/examples/esphome/TM1638-nodemcu-demo.yaml index 9b570bc..cdb1dc9 100644 --- a/examples/esphome/TM1638-nodemcu-demo.yaml +++ b/examples/esphome/TM1638-nodemcu-demo.yaml @@ -12,11 +12,8 @@ esphome: wifi: networks: - ssid: "esphassio" - bssid: "42:31:3C:DF:4B:43" - password: "21212121" - hidden: True - + ssid: "esphome" + password: "12345678" captive_portal: @@ -66,4 +63,4 @@ switch: TM1638Component *c = static_cast(const_cast(&tm1638)->get_component(0)); return { c->leds }; switches: - name: "tm1638led" \ No newline at end of file + name: "tm1638led" From f3bad5259461f957cc47dbc97041a5c60760ee32 Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk <25319+sashao@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:21:18 +0200 Subject: [PATCH 6/7] Update TM1638-nodemcu-demo.yaml --- examples/esphome/TM1638-nodemcu-demo.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/esphome/TM1638-nodemcu-demo.yaml b/examples/esphome/TM1638-nodemcu-demo.yaml index cdb1dc9..3a1a8f0 100644 --- a/examples/esphome/TM1638-nodemcu-demo.yaml +++ b/examples/esphome/TM1638-nodemcu-demo.yaml @@ -3,8 +3,6 @@ esphome: platform: ESP8266 board: nodemcuv2 includes: -# - ../../src/TM1638lite.h -# - ../../src/TM1638lite.cpp - TM1638Sensor.h libraries: - TM1638lite From 8ec2596dd5a047825c2a90557665be3f079022c6 Mon Sep 17 00:00:00 2001 From: Oleksandr Omelchuk <25319+sashao@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:22:54 +0200 Subject: [PATCH 7/7] Removed commented out code --- examples/esphome/TM1638Sensor.h | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/examples/esphome/TM1638Sensor.h b/examples/esphome/TM1638Sensor.h index cfaa604..885f978 100644 --- a/examples/esphome/TM1638Sensor.h +++ b/examples/esphome/TM1638Sensor.h @@ -1,13 +1,8 @@ #include "esphome.h" -//#include "switch.h" #include "TM1638lite.h" #include - -//esphome::lcd_base::LCDDisplay - using namespace esphome; -//using namespace esphome::switch_; class TM1638Component; @@ -68,7 +63,6 @@ class TM1638Component : public PollingComponent, public CustomAPIDevice { tm.reset(); tm.displayText(txt); -// tm.setLED(0, 1); register_service(&TM1638Component::on_set_text, "display_text", {"text", "time"}); @@ -85,15 +79,9 @@ class TM1638Component : public PollingComponent, public CustomAPIDevice { void do_scroll() { if (show_text == 0) { - auto time = id(homeassistant_time).now(); - time.strftime(txt, 10, "%T"); -/* char first = txt[0]; - for (int i = 0; i != 7; ++i) - { - txt[i] = txt[i+1]; - } - txt[7] = first;*/ - tm.displayText(txt); + auto time = id(homeassistant_time).now(); + time.strftime(txt, 10, "%T"); + tm.displayText(txt); } else { --show_text; @@ -101,7 +89,6 @@ class TM1638Component : public PollingComponent, public CustomAPIDevice { } void update() override { -// return; if (scroll < 0) { scroll = 10; do_scroll(); @@ -113,11 +100,6 @@ class TM1638Component : public PollingComponent, public CustomAPIDevice { ESP_LOGI("tm1651", " update %d", buttons); btns = buttons; } -// ESP_LOGI("tm1651", " u %d", buttons); - - // This is the actual sensor reading logic. - //float temperature = bmp.readTemperature(); - //temperature_sensor->publish_state(temperature); --scroll; }