ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
tm1621.cpp
Go to the documentation of this file.
1#include "tm1621.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace tm1621 {
8
9static const char *const TAG = "tm1621";
10
11const uint8_t TM1621_PULSE_WIDTH = 10; // microseconds (Sonoff = 100)
12
13const uint8_t TM1621_SYS_EN = 0x01; // 0b00000001
14const uint8_t TM1621_LCD_ON = 0x03; // 0b00000011
15const uint8_t TM1621_TIMER_DIS = 0x04; // 0b00000100
16const uint8_t TM1621_WDT_DIS = 0x05; // 0b00000101
17const uint8_t TM1621_TONE_OFF = 0x08; // 0b00001000
18const uint8_t TM1621_BIAS = 0x29; // 0b00101001 = LCD 1/3 bias 4 commons option
19const uint8_t TM1621_IRQ_DIS = 0x80; // 0b100x0xxx
20
22
25
26const char TM1621_KCHAR[] PROGMEM = {"0|1|2|3|4|5|6|7|8|9|-| "};
27// 0 1 2 3 4 5 6 7 8 9 - off
28const uint8_t TM1621_DIGIT_ROW[2][12] = {{0x5F, 0x50, 0x3D, 0x79, 0x72, 0x6B, 0x6F, 0x51, 0x7F, 0x7B, 0x20, 0x00},
29 {0xF5, 0x05, 0xB6, 0x97, 0x47, 0xD3, 0xF3, 0x85, 0xF7, 0xD7, 0x02, 0x00}};
30
32 this->cs_pin_->setup(); // OUTPUT
33 this->cs_pin_->digital_write(true);
34 this->data_pin_->setup(); // OUTPUT
35 this->data_pin_->digital_write(true);
36 this->read_pin_->setup(); // OUTPUT
37 this->read_pin_->digital_write(true);
38 this->write_pin_->setup(); // OUTPUT
39 this->write_pin_->digital_write(true);
40
41 this->state_ = 100;
42
43 this->cs_pin_->digital_write(false);
45 this->read_pin_->digital_write(false);
47 this->write_pin_->digital_write(false);
49 this->data_pin_->digital_write(false);
50 delayMicroseconds(TM1621_PULSE_WIDTH);
51 this->data_pin_->digital_write(true);
52
53 for (uint8_t tm1621_command : TM1621_COMMANDS) {
54 this->send_command_(tm1621_command);
55 }
56
57 this->send_address_(0x00);
58 for (uint32_t segment = 0; segment < 16; segment++) {
59 this->send_common_(0);
60 }
61 this->stop_();
62
63 snprintf(this->row_[0], sizeof(this->row_[0]), "----");
64 snprintf(this->row_[1], sizeof(this->row_[1]), "----");
65
66 this->display();
67}
69 ESP_LOGCONFIG(TAG, "TM1621:");
70 LOG_PIN(" CS Pin: ", this->cs_pin_);
71 LOG_PIN(" DATA Pin: ", this->data_pin_);
72 LOG_PIN(" READ Pin: ", this->read_pin_);
73 LOG_PIN(" WRITE Pin: ", this->write_pin_);
74 LOG_UPDATE_INTERVAL(this);
75}
76
78 // memset(this->row, 0, sizeof(this->row));
79 if (this->writer_.has_value())
80 (*this->writer_)(*this);
81 this->display();
82}
83
86
88 this->cs_pin_->digital_write(true); // Stop command sequence
89 delayMicroseconds(TM1621_PULSE_WIDTH / 2);
90 this->data_pin_->digital_write(true); // Reset data
91}
92
94 // Tm1621.row[x] = "text", "----", " " or a number with one decimal like "0.4", "237.5", "123456.7"
95 // "123456.7" will be shown as "9999" being a four digit overflow
96
97 // AddLog(LOG_LEVEL_DEBUG, PSTR("TM1: Row1 '%s', Row2 '%s'"), Tm1621.row[0], Tm1621.row[1]);
98
99 uint8_t buffer[8] = {0}; // TM1621 16-segment 4-bit common buffer
100 char row[4];
101 for (uint32_t j = 0; j < 2; j++) {
102 // 0.4V => " 04", 0.0A => " ", 1234.5V => "1234"
103 uint32_t len = strlen(this->row_[j]);
104 char *dp = nullptr; // Expect number larger than "123"
105 int row_idx = len - 3; // "1234.5"
106 if (len <= 5) { // "----", " ", "0.4", "237.5"
107 dp = strchr(this->row_[j], '.');
108 row_idx = len - 1;
109 } else if (len > 6) { // "12345.6"
110 snprintf(this->row_[j], sizeof(this->row_[j]), "9999");
111 row_idx = 3;
112 }
113 row[3] = (row_idx >= 0) ? this->row_[j][row_idx--] : ' ';
114 if ((row_idx >= 0) && dp) {
115 row_idx--;
116 }
117 row[2] = (row_idx >= 0) ? this->row_[j][row_idx--] : ' ';
118 row[1] = (row_idx >= 0) ? this->row_[j][row_idx--] : ' ';
119 row[0] = (row_idx >= 0) ? this->row_[j][row_idx--] : ' ';
120
121 // AddLog(LOG_LEVEL_DEBUG, PSTR("TM1: Dump%d %4_H"), j +1, row);
122
123 char command[10];
124 char needle[2] = {0};
125 for (uint32_t i = 0; i < 4; i++) {
126 needle[0] = row[i];
127 int index = this->get_command_code_(command, sizeof(command), (const char *) needle, TM1621_KCHAR);
128 if (-1 == index) {
129 index = 11;
130 }
131 uint32_t bidx = (0 == j) ? i : 7 - i;
132 buffer[bidx] = TM1621_DIGIT_ROW[j][index];
133 }
134 if (dp) {
135 if (0 == j) {
136 buffer[2] |= 0x80; // Row 1 decimal point
137 } else {
138 buffer[5] |= 0x08; // Row 2 decimal point
139 }
140 }
141 }
142
143 if (this->fahrenheit_) {
144 buffer[1] |= 0x80;
145 }
146 if (this->celsius_) {
147 buffer[3] |= 0x80;
148 }
149 if (this->kwh_) {
150 buffer[4] |= 0x08;
151 }
152 if (this->humidity_) {
153 buffer[6] |= 0x08;
154 }
155 if (this->voltage_) {
156 buffer[7] |= 0x08;
157 }
158
159 // AddLog(LOG_LEVEL_DEBUG, PSTR("TM1: Dump3 %8_H"), buffer);
160
161 this->send_address_(0x10); // Sonoff only uses the upper 16 Segments
162 for (uint8_t i : buffer) {
163 this->send_common_(i);
164 }
165 this->stop_();
166}
167
168bool TM1621Display::send_command_(uint16_t command) {
169 uint16_t full_command = (0x0400 | command) << 5; // 0b100cccccccc00000
170 this->cs_pin_->digital_write(false); // Start command sequence
171 delayMicroseconds(TM1621_PULSE_WIDTH / 2);
172 for (uint32_t i = 0; i < 12; i++) {
173 this->write_pin_->digital_write(false); // Start write sequence
174 if (full_command & 0x8000) {
175 this->data_pin_->digital_write(true); // Set data
176 } else {
177 this->data_pin_->digital_write(false); // Set data
178 }
179 delayMicroseconds(TM1621_PULSE_WIDTH);
180 this->write_pin_->digital_write(true); // Read data
181 delayMicroseconds(TM1621_PULSE_WIDTH);
182 full_command <<= 1;
183 }
184 this->stop_();
185 return true;
186}
187
188bool TM1621Display::send_common_(uint8_t common) {
189 for (uint32_t i = 0; i < 8; i++) {
190 this->write_pin_->digital_write(false); // Start write sequence
191 if (common & 1) {
192 this->data_pin_->digital_write(true); // Set data
193 } else {
194 this->data_pin_->digital_write(false); // Set data
195 }
196 delayMicroseconds(TM1621_PULSE_WIDTH);
197 this->write_pin_->digital_write(true); // Read data
198 delayMicroseconds(TM1621_PULSE_WIDTH);
199 common >>= 1;
200 }
201 return true;
202}
203
205 uint16_t full_address = (address | 0x0140) << 7; // 0b101aaaaaa0000000
206 this->cs_pin_->digital_write(false); // Start command sequence
207 delayMicroseconds(TM1621_PULSE_WIDTH / 2);
208 for (uint32_t i = 0; i < 9; i++) {
209 this->write_pin_->digital_write(false); // Start write sequence
210 if (full_address & 0x8000) {
211 this->data_pin_->digital_write(true); // Set data
212 } else {
213 this->data_pin_->digital_write(false); // Set data
214 }
215 delayMicroseconds(TM1621_PULSE_WIDTH);
216 this->write_pin_->digital_write(true); // Read data
217 delayMicroseconds(TM1621_PULSE_WIDTH);
218 full_address <<= 1;
219 }
220 return true;
221}
222
223uint8_t TM1621Display::print(uint8_t start_pos, const char *str) {
224 // ESP_LOGD(TAG, "Print at %d: %s", start_pos, str);
225 return snprintf(this->row_[start_pos], sizeof(this->row_[start_pos]), "%s", str);
226}
227uint8_t TM1621Display::print(const char *str) { return this->print(0, str); }
228uint8_t TM1621Display::printf(uint8_t pos, const char *format, ...) {
229 va_list arg;
230 va_start(arg, format);
231 char buffer[64];
232 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
233 va_end(arg);
234 if (ret > 0)
235 return this->print(pos, buffer);
236 return 0;
237}
238uint8_t TM1621Display::printf(const char *format, ...) {
239 va_list arg;
240 va_start(arg, format);
241 char buffer[64];
242 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
243 va_end(arg);
244 if (ret > 0)
245 return this->print(buffer);
246 return 0;
247}
248
249int TM1621Display::get_command_code_(char *destination, size_t destination_size, const char *needle,
250 const char *haystack) {
251 // Returns -1 of not found
252 // Returns index and command if found
253 int result = -1;
254 const char *read = haystack;
255 char *write = destination;
256
257 while (true) {
258 result++;
259 size_t size = destination_size - 1;
260 write = destination;
261 char ch = '.';
262 while ((ch != '\0') && (ch != '|')) {
263 ch = *(read++);
264 if (size && (ch != '|')) {
265 *write++ = ch;
266 size--;
267 }
268 }
269 *write = '\0';
270 if (!strcasecmp(needle, destination)) {
271 break;
272 }
273 if (0 == ch) {
274 result = -1;
275 break;
276 }
277 }
278 return result;
279}
280} // namespace tm1621
281} // namespace esphome
uint8_t address
Definition bl0906.h:4
virtual void setup()=0
virtual void digital_write(bool value)=0
bool has_value() const
Definition optional.h:92
int get_command_code_(char *destination, size_t destination_size, const char *needle, const char *haystack)
Definition tm1621.cpp:249
void dump_config() override
Definition tm1621.cpp:68
bool send_command_(uint16_t command)
Definition tm1621.cpp:168
uint8_t printf(uint8_t pos, const char *format,...) __attribute__((format(printf
Evaluate the printf-format and print the result at the given position.
Definition tm1621.cpp:228
uint8_t uint8_t uint8_t print(uint8_t pos, const char *str)
Print str at the given position.
Definition tm1621.cpp:223
bool send_common_(uint8_t common)
Definition tm1621.cpp:188
bool send_address_(uint16_t address)
Definition tm1621.cpp:204
float get_setup_priority() const override
Definition tm1621.cpp:84
optional< tm1621_writer_t > writer_
Definition tm1621.h:62
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:51
const uint8_t TM1621_LCD_ON
Definition tm1621.cpp:14
const uint8_t TM1621_WDT_DIS
Definition tm1621.cpp:16
const char TM1621_KCHAR[] PROGMEM
Definition tm1621.cpp:26
const uint8_t TM1621_SYS_EN
Definition tm1621.cpp:13
const uint8_t TM1621_TONE_OFF
Definition tm1621.cpp:17
const uint8_t TM1621_TIMER_DIS
Definition tm1621.cpp:15
const uint8_t TM1621_PULSE_WIDTH
Definition tm1621.cpp:11
const uint8_t TM1621_BIAS
Definition tm1621.cpp:18
const uint8_t TM1621_IRQ_DIS
Definition tm1621.cpp:19
const uint8_t TM1621_DIGIT_ROW[2][12]
Definition tm1621.cpp:28
const uint8_t TM1621_COMMANDS[]
Definition tm1621.cpp:23
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
std::string print()