From 7a2c5154aa7ee6838aaa64006fac3ef59788a274 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 27 Nov 2018 16:11:14 +0100 Subject: [PATCH 1/6] PS_16_DZ: only update brightness if it actually changed --- sonoff/xdrv_19_ps16dz_dimmer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index 04c9a1099..8f569278f 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -165,7 +165,7 @@ void PS16DZSerialInput(void) ps16dz_bright = atoi(token3); snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: brightness received: %d"), ps16dz_bright); AddLog(LOG_LEVEL_DEBUG); - if(power && ps16dz_bright > 0) { + if(power && ps16dz_bright > 0 && ps16dz_bright != Settings.light_dimmer) { snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), ps16dz_bright ); From 5099d8f20d7176e0c5e4f797351898568bda86d2 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 27 Nov 2018 16:52:09 +0100 Subject: [PATCH 2/6] PS_16_DZ: reduce buffer-size and actually check for overflow --- sonoff/xdrv_19_ps16dz_dimmer.ino | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index 8f569278f..755390029 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -21,7 +21,7 @@ #define XDRV_19 19 -#define PS16DZ_BUFFER_SIZE 256 +#define PS16DZ_BUFFER_SIZE 80 #include @@ -29,8 +29,6 @@ TasmotaSerial *PS16DZSerial = nullptr; boolean ps16dz_ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction -boolean ps16dz_power = false; -uint8_t ps16dz_bright = 0; //uint64_t ps16dz_seq = 0; char *ps16dz_tx_buffer = NULL; // Serial transmit buffer @@ -136,6 +134,10 @@ void PS16DZSerialInput(void) yield(); byte serial_in_byte = PS16DZSerial->read(); if (serial_in_byte != 0x1B){ + if (ps16dz_byte_counter >= PS16DZ_BUFFER_SIZE) { + memset(ps16dz_rx_buffer, 0, PS16DZ_BUFFER_SIZE); + ps16dz_byte_counter = 0; + } if (ps16dz_byte_counter || (!ps16dz_byte_counter && serial_in_byte == 'A')); ps16dz_rx_buffer[ps16dz_byte_counter++] = serial_in_byte; } @@ -154,7 +156,7 @@ void PS16DZSerialInput(void) char* token2 = strtok_r(token, ":", &end_token); char* token3 = strtok_r(NULL, ":", &end_token); if(!strncmp(token2, "\"switch\"", 8)){ - ps16dz_power = !strncmp(token3, "\"on\"", 4)?true:false; + boolean ps16dz_power = !strncmp(token3, "\"on\"", 4)?true:false; snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: power received: %s"), token3); AddLog(LOG_LEVEL_DEBUG); if((power || Settings.light_dimmer > 0) && (power !=ps16dz_power)) { @@ -162,7 +164,7 @@ void PS16DZSerialInput(void) } } else if(!strncmp(token2, "\"bright\"", 8)){ - ps16dz_bright = atoi(token3); + uint8_t ps16dz_bright = atoi(token3); snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: brightness received: %d"), ps16dz_bright); AddLog(LOG_LEVEL_DEBUG); if(power && ps16dz_bright > 0 && ps16dz_bright != Settings.light_dimmer) { From 6825b781537af678e1b111d54c792e21df3f4d57 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 27 Nov 2018 17:18:41 +0100 Subject: [PATCH 3/6] PS_16_DZ: optimize for size --- sonoff/xdrv_19_ps16dz_dimmer.ino | 58 ++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index 755390029..c77aaf554 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -23,6 +23,10 @@ #define PS16DZ_BUFFER_SIZE 80 +#define PS16DZ_TYPE_ACK 0 +#define PS16DZ_TYPE_PWR 1 +#define PS16DZ_TYPE_DIM 2 + #include TasmotaSerial *PS16DZSerial = nullptr; @@ -44,6 +48,32 @@ void printTimestamp(void) snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s%d%03d"), ps16dz_tx_buffer, LocalTime(), millis()%1000); } +void PS16DZSendCommand(char type = 0, uint8_t value = 0) +{ + switch(type){ + case PS16DZ_TYPE_ACK: + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+SEND=ok")); + break; + case PS16DZ_TYPE_PWR: + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"")); + printTimestamp(); + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, value?"on":"off"); + break; + case PS16DZ_TYPE_DIM: + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"")); + printTimestamp(); + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(value * (100. / 255.))); + break; + } + + snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); + AddLog(LOG_LEVEL_DEBUG); + + PS16DZSerial->print(ps16dz_tx_buffer); + PS16DZSerial->write(0x1B); + PS16DZSerial->flush(); +} + boolean PS16DZSetPower(void) { boolean status = false; @@ -53,15 +83,7 @@ boolean PS16DZSetPower(void) if (source != SRC_SWITCH && PS16DZSerial) { // ignore to prevent loop from pushing state from faceplate interaction - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"")); - printTimestamp(); - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, rpower?"on":"off"); - snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); - AddLog(LOG_LEVEL_DEBUG); - - PS16DZSerial->print(ps16dz_tx_buffer); - PS16DZSerial->write(0x1B); - PS16DZSerial->flush(); + PS16DZSendCommand(PS16DZ_TYPE_PWR, rpower); status = true; } @@ -75,15 +97,7 @@ void PS16DZSerialDuty(uint8_t duty) duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself } - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"")); - printTimestamp(); - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(duty * (100. / 255.))); - snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); - AddLog(LOG_LEVEL_DEBUG); - - PS16DZSerial->print(ps16dz_tx_buffer); - PS16DZSerial->write(0x1B); - PS16DZSerial->flush(); + PS16DZSendCommand(PS16DZ_TYPE_DIM, duty); } else { ps16dz_ignore_dim = false; // reset flag @@ -194,13 +208,7 @@ void PS16DZSerialInput(void) memset(ps16dz_rx_buffer, 0, PS16DZ_BUFFER_SIZE); ps16dz_byte_counter = 0; - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+SEND=ok")); - snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); - AddLog(LOG_LEVEL_DEBUG); - - PS16DZSerial->print(ps16dz_tx_buffer); - PS16DZSerial->write(0x1B); - PS16DZSerial->flush(); + PS16DZSendCommand(); } } } From 49403243d50e4c34180a4e7a79bf83472b96326a Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 27 Nov 2018 18:02:00 +0100 Subject: [PATCH 4/6] PS_16_DZ: further optimization --- sonoff/xdrv_19_ps16dz_dimmer.ino | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index c77aaf554..6a57b4686 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -55,14 +55,15 @@ void PS16DZSendCommand(char type = 0, uint8_t value = 0) snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+SEND=ok")); break; case PS16DZ_TYPE_PWR: - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"")); - printTimestamp(); - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, value?"on":"off"); - break; case PS16DZ_TYPE_DIM: snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"")); printTimestamp(); - snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(value * (100. / 255.))); + if ( type == PS16DZ_TYPE_PWR) { + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, value?"on":"off"); + } + else if ( type == PS16DZ_TYPE_DIM) { + snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(value * (100. / 255.))); + } break; } From f75c4a27add655d37f55e323c659a11075a1d5c1 Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Tue, 27 Nov 2018 19:01:06 +0100 Subject: [PATCH 5/6] PS_16_DZ: leave space for trailing 0-byte --- sonoff/xdrv_19_ps16dz_dimmer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index 6a57b4686..72f9ea287 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -149,7 +149,7 @@ void PS16DZSerialInput(void) yield(); byte serial_in_byte = PS16DZSerial->read(); if (serial_in_byte != 0x1B){ - if (ps16dz_byte_counter >= PS16DZ_BUFFER_SIZE) { + if (ps16dz_byte_counter >= PS16DZ_BUFFER_SIZE - 1) { memset(ps16dz_rx_buffer, 0, PS16DZ_BUFFER_SIZE); ps16dz_byte_counter = 0; } From 0ce7537371322a3d7a59908baeec8263450ec3fc Mon Sep 17 00:00:00 2001 From: Joel Stein Date: Wed, 28 Nov 2018 00:04:06 +0100 Subject: [PATCH 6/6] PS_16_DZ: cleanup --- sonoff/xdrv_19_ps16dz_dimmer.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index 72f9ea287..d8c8138e1 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -166,8 +166,6 @@ void PS16DZSerialInput(void) char* token = strtok_r(string, ",", &end_str); while (token != NULL) { char* end_token; - snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: token = %s"), token); - AddLog(LOG_LEVEL_DEBUG); char* token2 = strtok_r(token, ":", &end_token); char* token3 = strtok_r(NULL, ":", &end_token); if(!strncmp(token2, "\"switch\"", 8)){