From f153086b7dd2167ff12916dea35989d42dae6307 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 26 Nov 2020 19:27:13 +0100 Subject: [PATCH] Zigbee UI change color of last seen depending on age --- tasmota/xdrv_23_zigbee_A_impl.ino | 53 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/tasmota/xdrv_23_zigbee_A_impl.ino b/tasmota/xdrv_23_zigbee_A_impl.ino index 8df3acbf9..8b3faaa6e 100644 --- a/tasmota/xdrv_23_zigbee_A_impl.ino +++ b/tasmota/xdrv_23_zigbee_A_impl.ino @@ -1601,32 +1601,26 @@ extern "C" { // Convert seconds to a string representing days, hours or minutes present in the n-value. // The string will contain the most coarse time only, rounded down (61m == 01h, 01h37m == 01h). // Inputs: -// - n: uint32_t representing some number of seconds -// - result: a buffer of suitable size (7 bytes would represent the entire solution space -// for UINT32_MAX including the trailing null-byte, or "49710d") -// - result_len: A numeric value representing the total length of the result buffer -// Returns: -// - The number of characters that would have been written were result sufficiently large -// - negatve number on encoding error from snprintf +// - seconds: uint32_t representing some number of seconds +// Outputs: +// - char for unit (d for day, h for hour, m for minute) +// - the hex color to be used to display the text // - int convert_seconds_to_dhm(uint32_t n, char *result, size_t result_len){ - char fmtstr[] = "%02dmhd"; // Don't want this in progmem, because we mutate it. - uint32_t conversions[3] = {24 * 3600, 3600, 60}; - uint32_t value; + uint32_t convert_seconds_to_dhm(uint32_t seconds, char *unit, uint8_t *color){ + static uint32_t conversions[3] = {24 * 3600, 3600, 60}; + static char units[3] = { 'd', 'h', 'm'}; // day, hour, minute + static uint8_t colors[3] = { 0x60, 0xA0, 0xEA}; for(int i = 0; i < 3; ++i) { - value = n / conversions[i]; - if(value > 0) { - fmtstr[4] = fmtstr[6-i]; - break; + *color = colors[i]; + *unit = units[i]; + if (seconds > conversions[i]) { // always pass even if 00m + return seconds / conversions[i]; } - n = n % conversions[i]; } - - // Null-terminate the string at the last "valid" index, removing any excess zero values. - fmtstr[5] = '\0'; - return snprintf(result, result_len, fmtstr, value); + return 0; } -} +} // extern "C" + void ZigbeeShow(bool json) { if (json) { @@ -1708,16 +1702,21 @@ void ZigbeeShow(bool json) WSContentSend_PD(PSTR(""), j, (num_bars < j) ? PSTR(" o30") : PSTR("")); } } - char dhm[16]; // len("🕗" + "49710d" + '\0') == 16 - snprintf_P(dhm, sizeof(dhm), PSTR(" ")); - if(device.validLastSeen()){ - snprintf_P(dhm, sizeof(dhm), PSTR("🕗")); - convert_seconds_to_dhm(now - device.last_seen, &dhm[9], 7); + char dhm[48]; + snprintf_P(dhm, sizeof(dhm), PSTR(" ")); + if (device.validLastSeen()) { + char unit; + uint8_t color; + uint16_t val = convert_seconds_to_dhm(now - device.last_seen, &unit, &color); + if (val < 100) { + snprintf_P(dhm, sizeof(dhm), PSTR("🕗%02d%c"), + color, color, color, val, unit); + } } WSContentSend_PD(PSTR( "" // Close LQI - "%s{e}" // dhm (Last Seen) + "%s{e}" // dhm (Last Seen) ), dhm ); // Sensors