-
-
Save dacoffey/787522faed2448757a9e48f56ff5200d to your computer and use it in GitHub Desktop.
#include <Adafruit_LEDBackpack.h> | |
Adafruit_7segment LEDSEG; | |
esphome::time::ESPTime DTNOW; | |
char DTSTR[17]; | |
time_t DT; |
logger: | |
level: WARN | |
esphome: | |
name: test | |
platform: ESP32 | |
board: esp32dev | |
includes: CLOCK.h | |
libraries: | |
- "SPI" | |
- "Wire" | |
- "Adafruit BusIO" | |
- "Adafruit GFX Library" | |
- "Adafruit LED Backpack Library" | |
on_boot: | |
then: | |
- lambda: !lambda |- | |
DTNOW = id(DTHA).now(); | |
id(LEDSEGCOLON) = true; | |
LEDSEG = Adafruit_7segment(); | |
LEDSEG.begin(0x70); | |
LEDSEG.setBrightness(15); | |
LEDSEG.blinkRate(HT16K33_BLINK_OFF); | |
globals: | |
- id: LEDSEGCOLON | |
type: bool | |
restore_value: no | |
<<: !include CONFIG_WIFI.yaml | |
time: | |
- platform: homeassistant | |
id: DTHA | |
i2c: | |
scl: 22 | |
sda: 21 | |
frequency: 50000 | |
scan: true | |
sensor: | |
- platform: template | |
name: "T_DT" | |
update_interval: 100ms | |
lambda: |- | |
DTNOW = id(DTHA).now(); | |
DT = DTNOW.timestamp; | |
strftime(DTSTR, sizeof(DTSTR), "%I%M", localtime(&DT)); | |
return DT; | |
- platform: template | |
name: "T_LEDSEG" | |
update_interval: 750ms | |
lambda: |- | |
LEDSEG.print(strtoul(DTSTR,NULL,10)); | |
LEDSEG.drawColon(id(LEDSEGCOLON) = !id(LEDSEGCOLON)); | |
LEDSEG.writeDisplay(); | |
return id(LEDSEGCOLON); |
Hi,
I'm using your code for a simple clock based on an Adafruit 7 segment display. I've changed the strftime format to "%H%M", as used as a normal way in Europe (24h clock). However, as from midnight until 1:00 am, the clock shows only the minutes (e.g.: ":45" for 0:45). I'd like to change it to 0:45 or even 00:45. How can I achieve this? I think I have to replace "LEDSEG.print(strtoul(DTSTR,NULL,10));" somehow, but unfortunately, I'm not a C++ (or Lambda?) programmer , sorry...
Anyway, thanks in advance for your kind help!
substitutions:
plug_name: ESP32 DEVKITV1
esphome:
name: esp-clock
comment: ${plug_name}
includes: include/Adafruit_LEDBackpack.h
libraries:
- "SPI"
- "Wire"
- "Adafruit BusIO"
- "Adafruit GFX Library"
- "Adafruit LED Backpack Library"
esp32:
board: nodemcu-32s
wifi:
ssid: "yourssid_wifi"
password: "yourpass_wifi"
power_save_mode: none
api:
web_server:
port: 80
logger:
baud_rate: 0
ota:
i2c:
scl: 22
sda: 21
scan: true
text_sensor:
- platform: template
name: "LED time"
id: led_time
icon: mdi:web-clock
update_interval: 1s
lambda: |-
Adafruit_7segment display;
if (!display.begin()) {
display = Adafruit_7segment();
}
if (id(ha_sun).elevation() < 0) {
display.setBrightness(1);
} else {
display.setBrightness(10);
}
auto now = id(ha_time).now();
char text[6];
if (now.hour == 0) {
snprintf(text, sizeof(text), "0%02d", now.minute);
} else if (now.hour < 10) {
snprintf(text, sizeof(text), " %d%02d", now.hour, now.minute);
} else {
snprintf(text, sizeof(text), "%d%02d", now.hour, now.minute);
}
display.print(text);
static int i = 0;
if (i == 1) {
display.drawColon(true);
} else {
display.drawColon(false);
}
i++;
if (i == 2) {
i = 0;
}
display.writeDisplay();
std::string str(text);
str.insert(str.length() - 2, ":");
return str;
binary_sensor:
- platform: status
name: "${plug_name} Status"
switch:
- platform: restart
name: "${plug_name} Restart"
status_led:
pin:
number: GPIO2
inverted: false
time:
- platform: homeassistant
id: ha_time
sun:
latitude: 88.88°
longitude: 88.88°
id: ha_sun
https://www.adafruit.com/product/1269