-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdown-clock.yaml
493 lines (449 loc) · 13.4 KB
/
countdown-clock.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
substitutions:
device_name: "countdown-clock"
device_friendly_name: "Countdown clock"
device_description: "Cute Astronaut prop"
time_timezone: "Europe/Amsterdam"
globals:
# ESPHome sensor (float) is too small to accurately
# represent time in seconds, so we need a global
- id: duration_ticks
type: int
restore_value: no
initial_value: '0'
- id: brightness_delay
type: int
restore_value: no
initial_value: '500'
- id: shift_register_delay
type: int
restore_value: no
initial_value: '10'
# Segments that need to be lit for [0-9] and :
- id: digitCodeMap
type: uint8_t[11]
restore_value: no
# GFEDCBA
initial_value: '{
0b0111111,
0b0000110,
0b1011011,
0b1001111,
0b1100110,
0b1101101,
0b1111101,
0b0000111,
0b1111111,
0b1101111,
0b1110000}'
# Maps which segment (1-7) need to be lit for which module (1-11)
- id: segmentMap
type: uint16_t[7]
restore_value: no
# 1110987654321
initial_value: '{
0b00000000000,
0b00000000000,
0b00000000000,
0b00000000000,
0b00000000000,
0b00000000000,
0b00000000000}'
esphome:
name: $device_name
friendly_name: $device_friendly_name
comment: $device_description
includes:
- my_custom_output.h
esp32:
board: esp32doit-devkit-v1
# Enable logging
logger:
level: DEBUG
# Disable logging via UART
baud_rate: 0
# Enable Home Assistant API
api:
encryption:
key: !secret home_assistant_key
# Secure Over The Air updates
ota:
password: !secret ota_password
ethernet:
type: LAN8720
mdc_pin: GPIO23
mdio_pin: GPIO18
clk_mode: GPIO17_OUT
phy_addr: 0
power_pin: GPIO5
# Optional manual IP
# manual_ip:
# static_ip: 10.0.0.2
# gateway: 10.0.0.1
# subnet: 255.255.255.0
# use_address: 10.0.0.2
# Configure segments, directly connected to GPIO
output:
- platform: gpio
pin: GPIO14
id: 'segment_A'
- platform: gpio
pin: GPIO13
id: 'segment_B'
- platform: gpio
pin: GPIO32
id: 'segment_C'
- platform: gpio
pin: GPIO12
id: 'segment_D'
- platform: gpio
pin: GPIO16
id: 'segment_E'
- platform: gpio
pin: GPIO33
id: 'segment_F'
- platform: gpio
pin: GPIO15
id: 'segment_G'
# Configure shift registers
- platform: gpio
pin: GPIO4
id: 'shift_clock'
- platform: gpio
pin: GPIO0
id: 'shift_latch'
- platform: gpio
pin: GPIO2
id: 'shift_data'
# Output for display light, allow us to set the brightness
# Using delay, but RMT seems a good future candidate
- platform: template
id: display_brightness
type: float
write_action:
- lambda: id(brightness_delay) = state * 500;
# Output that blinks the status LED
- platform: template
id: status_output
type: binary
write_action:
- if:
condition:
lambda: return (state);
then:
# Enable the PM led
- lambda: |-
id(preload_modules)->execute(0b00000100000);
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_A).turn_on();
else:
# Disable the PM led
- lambda: |-
id(segment_A).turn_off();
# A custom output to change the CPU frequency
- platform: custom
type: binary
lambda: |-
auto my_high_frequency_loop = new HighFrequencyLoop();
App.register_component(my_high_frequency_loop);
return {my_high_frequency_loop};
outputs:
id: high_frequency_loop
select:
# Select the mode for clock functions
- platform: template
name: Mode
id: mode
optimistic: true
icon: "mdi:history"
options:
- pause
- countdown
- countup
initial_option: pause
set_action:
- logger.log:
level: INFO
format: "Mode: %s"
args: ["x.c_str()"]
number:
# Sets the duration of the timer
- platform: template
name: "Duration Days"
id: duration_days
unit_of_measurement: d
mode: box
icon: "mdi:clock-digital"
optimistic: true
min_value: 0
max_value: 999
step: 1
on_value:
then:
- script.execute: update_duration
- platform: template
name: "Duration Hours"
id: duration_hours
unit_of_measurement: h
mode: box
icon: "mdi:clock-digital"
optimistic: true
min_value: 0
max_value: 24
step: 1
on_value:
then:
- script.execute: update_duration
- platform: template
name: "Duration Minutes"
id: duration_minutes
unit_of_measurement: min
mode: box
icon: "mdi:clock-digital"
optimistic: true
min_value: 0
max_value: 60
step: 1
on_value:
then:
- script.execute: update_duration
- platform: template
name: "Duration Seconds"
id: duration_seconds
unit_of_measurement: s
mode: box
icon: "mdi:clock-digital"
optimistic: true
min_value: 0
max_value: 60
step: 1
on_value:
then:
- script.execute: update_duration
- platform: template
name: "Auto pause time"
id: auto_pause_seconds
unit_of_measurement: s
mode: box
icon: "mdi:timer-pause-outline"
optimistic: true
min_value: 0
max_value: 3600
step: 1
switch:
# Set the ESP32 clock speed to high
- platform: output
name: "High Speed"
output: 'high_frequency_loop'
restore_mode: RESTORE_DEFAULT_ON
entity_category: "config"
disabled_by_default: True
- platform: template
name: "Auto Pause"
icon: "mdi:cog-pause"
id: auto_pause
optimistic: True
restore_mode: RESTORE_DEFAULT_OFF
button:
# Boot in safe mode
- platform: safe_mode
name: Safe Mode Boot
entity_category: diagnostic
time:
# Get the time from HA, so we can use it for uptime
- platform: homeassistant
id: time_homeassistant
timezone: "${time_timezone}"
on_time_sync:
- component.update: sensor_uptime_timestamp
sensor:
# Uptime is used internally only
- platform: uptime
id: sensor_uptime
# This sensor is an alternative for the uptime sensor, which only sends the
# startup timestamp of the device to home assistant once
- platform: template
id: sensor_uptime_timestamp
name: "Uptime"
entity_category: diagnostic
device_class: "timestamp"
accuracy_decimals: 0
update_interval: never
lambda: |-
static float timestamp = (
id(time_homeassistant).utcnow().timestamp - id(sensor_uptime).state
);
return timestamp;
text_sensor:
- platform: ethernet_info
ip_address:
name: IP Address
light:
# Enable status LED
- platform: status_led
output: status_output
name: "Status Led"
internal: True
# Switch off the display and change its brightness
- platform: monochromatic
name: "Display"
id: display
output: display_brightness
default_transition_length: 0s
restore_mode: ALWAYS_OFF
interval:
# Refresh the screen as fast as possible.
- interval: 0s
then:
- if:
condition:
light.is_on: display
then:
- script.execute:
id: draw_display
# Modes: Countup/down/pause
- interval: 1s
then:
- lambda: |-
if(id(mode).state != "pause") {
if(id(mode).state == "countdown") {
if(id(duration_ticks) > 0)
id(duration_ticks) -= 1;
else {
// Reached 0, switch to counting up
auto call = id(mode).make_call();
call.set_option("countup");
call.perform();
id(duration_ticks) += 1;
}
}
else if(id(mode).state == "countup")
id(duration_ticks) += 1;
id(update_display)->execute(id(duration_ticks));
// Auto pause
if(id(auto_pause).state && id(auto_pause_seconds).state == id(duration_ticks))
id(mode).make_call().set_option("pause").perform();
}
script:
# Updates the duration to the numbers inputs
- id: update_duration
then:
- lambda: |-
id(duration_ticks) = static_cast<int>(id(duration_seconds).state);
id(duration_ticks) += static_cast<int>(id(duration_minutes).state)*60;
id(duration_ticks) += static_cast<int>(id(duration_hours).state)*3600;
id(duration_ticks) += static_cast<int>(id(duration_days).state)*86400;
- script.execute:
id: update_display
duration: !lambda return id(duration_ticks);
# Update the display segmentMap with the current output_string
- id: update_display
mode: single
parameters:
duration: int
then:
- lambda: |-
// Get the amount of segments in the display
int segments = sizeof(id(segmentMap)) / sizeof(uint16_t);
// Build the string to display on the screen
std::string str = str_sprintf("%03d%02d:%02d:%02d", duration/86400, duration/3600 % 24, duration/60 % 60, duration % 60);
// Reverse the string
reverse(str.begin(), str.end());
// Convert the string to binary map
for (int digit: str) {
int digitLookup = digit - 48;
for (int i = (segments - 1); i >= 0; i--) {
id(segmentMap)[i] = (id(segmentMap)[i] << 1) | ((id(digitCodeMap)[digitLookup] >> i)& 0x01);
}
}
# Draws the display
- id: draw_display
mode: single
then:
# Draw the characters per segment
# We can't abstract this into a loop because ESPHome can't handle dynamic id's
- lambda: |-
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_A).turn_on();
id(preload_modules)->execute(id(segmentMap)[1]);
delayMicroseconds(id(brightness_delay));
id(segment_A).turn_off();
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_B).turn_on();
id(preload_modules)->execute(id(segmentMap)[2]);
delayMicroseconds(id(brightness_delay));
id(segment_B).turn_off();
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_C).turn_on();
id(preload_modules)->execute(id(segmentMap)[3]);
delayMicroseconds(id(brightness_delay));
id(segment_C).turn_off();
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_D).turn_on();
id(preload_modules)->execute(id(segmentMap)[4]);
delayMicroseconds(id(brightness_delay));
id(segment_D).turn_off();
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_E).turn_on();
id(preload_modules)->execute(id(segmentMap)[5]);
delayMicroseconds(id(brightness_delay));
id(segment_E).turn_off();
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_F).turn_on();
id(preload_modules)->execute(id(segmentMap)[6]);
delayMicroseconds(id(brightness_delay));
id(segment_F).turn_off();
id(shift_latch).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_latch).turn_off();
id(segment_G).turn_on();
id(preload_modules)->execute(id(segmentMap)[0]);
delayMicroseconds(id(brightness_delay));
id(segment_G).turn_off();
# Preloads the shift register
- id: preload_modules
parameters:
modules: int
mode: single
then:
- lambda: |-
uint16_t outputs = 0;
// Map the register outputs to the LED modules
outputs = (outputs << 0) | ((modules >> 5)& 0x01); // PM (segment A) and first divider (segment G and E)
outputs = (outputs << 2) | ((modules >> 6)& 0x01); // Digit Minutes_1
outputs = (outputs << 1) | ((modules >> 7)& 0x01); // Digit Minutes_2
outputs = (outputs << 1) | ((modules >> 8)& 0x01); // Second divider (segment F and G)
outputs = (outputs << 1) | ((modules >> 9)& 0x01); // Digit Seconds_1
outputs = (outputs << 1) | ((modules >> 10)& 0x01); // Digit Seconds_2
outputs = (outputs << 2) | ((modules >> 0)& 0x01); // Digit Days_1
outputs = (outputs << 1) | ((modules >> 3)& 0x01); // Digit Hours_1
outputs = (outputs << 1) | ((modules >> 4)& 0x01); // Digit Hours_2
outputs = (outputs << 1) | ((modules >> 2)& 0x01); // Digit Days_3
outputs = (outputs << 1) | ((modules >> 1)& 0x01); // Digit Days_2
outputs = (outputs << 2);
// Clock the register
for(int i = 0, mask = 1; i < 16; i++, mask = mask << 1)
{
if (outputs & mask) {
id(shift_data).turn_on();
}
else
id(shift_data).turn_off();
delayMicroseconds(id(shift_register_delay));
id(shift_clock).turn_on();
delayMicroseconds(id(shift_register_delay));
id(shift_clock).turn_off();
}