Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/esphome/.gitignore
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions examples/esphome/TM1638-nodemcu-demo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
esphome:
name: tm1638_nodemcu_lolin
platform: ESP8266
board: nodemcuv2
includes:
- TM1638Sensor.h
libraries:
- TM1638lite


wifi:
networks:
ssid: "esphome"
password: "12345678"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
password: "chugaister"

ota:
password: "chugaister"

time:
- platform: homeassistant
id: homeassistant_time

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<TM1638Component *>(const_cast<custom_component::CustomComponentConstructor *>(&tm1638)->get_component(0));
return { c->buttons };

binary_sensors:
- 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"



switch:
- platform: custom
lambda: |-
TM1638Component *c = static_cast<TM1638Component *>(const_cast<custom_component::CustomComponentConstructor *>(&tm1638)->get_component(0));
return { c->leds };
switches:
name: "tm1638led"
114 changes: 114 additions & 0 deletions examples/esphome/TM1638Sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "esphome.h"
#include "TM1638lite.h"
#include <string.h>

using namespace esphome;

class TM1638Component;

class TM1638Led : public Switch, public Component {
public:
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 {
tm.setLED(i, state & 1);
publish_state(state);
}

};

class TM1638Button : public BinarySensor {
public:
int i = 0;
TM1638Component *parent = nullptr;
};

class TM1638Component : public PollingComponent, public CustomAPIDevice {
public:
TM1638lite tm = TM1638lite(12, 14, 4); // arduino pin numbers strobe/clock/data
std::vector<BinarySensor *> buttons;
std::vector<Switch *> leds;
std::vector<TM1638Button *> buttons_;
std::vector<TM1638Led *> 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);


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);
}
strncpy(txt, "Hello ", 10);
}

void setup() override {
ESP_LOGI("tm1651", "Creation");
tm.reset();

tm.displayText(txt);

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");
tm.displayText(txt);
}
else {
--show_text;
}
}

void update() override {
if (scroll < 0) {
scroll = 10;
do_scroll();
}
static uint8_t btns = 3;
uint8_t buttons = tm.readButtons();
if (btns != buttons) {
doLEDs(buttons);
ESP_LOGI("tm1651", " update %d", buttons);
btns = buttons;
}
--scroll;
}

void doLEDs(uint8_t value) {
for (uint8_t position = 0; position < 8; position++) {
tm.setLED(position, value & 1 || leds[position]->state);
buttons_[position]->publish_state(value);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to buttons_[position]->publish_state(value & 1); for accurate button reporting!

value = value >> 1;
}
}

};