-
Notifications
You must be signed in to change notification settings - Fork 8
Added example how to use esphome component #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sashao
wants to merge
7
commits into
danja:master
Choose a base branch
from
sashao:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7acf14c
initial esphome example
b123094
works on pins 12 14 4
237785a
Fixed init error.
99b9b52
added set text service and led switches
6f4eb8e
Update TM1638-nodemcu-demo.yaml
sashao f3bad52
Update TM1638-nodemcu-demo.yaml
sashao 8ec2596
Removed commented out code
sashao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| value = value >> 1; | ||
| } | ||
| } | ||
|
|
||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!