Skip to content

Commit

Permalink
PS_16_DZ: wait for acknoledgement before sending more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosmaster committed Nov 26, 2018
1 parent 1dc7a58 commit 7203d6a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions sonoff/xdrv_19_ps16dz_dimmer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ boolean ps16dz_ignore_dim = false; // Flag to skip serial send to pre
boolean ps16dz_power = false;
uint8_t ps16dz_bright = 0;
uint64_t ps16dz_seq = 1530000000000;
boolean ps16dz_synced = true;

char ps16dz_tx_buffer[PS16DZ_BUFFER_SIZE]; // Serial transmit buffer
char ps16dz_rx_buffer[PS16DZ_BUFFER_SIZE]; // Serial receive buffer
Expand Down Expand Up @@ -74,8 +75,9 @@ boolean PS16DZSetPower(void)
uint8_t rpower = XdrvMailbox.index;
int16_t source = XdrvMailbox.payload;

if (source != SRC_SWITCH && PS16DZSerial) { // ignore to prevent loop from pushing state from faceplate interaction
if (source != SRC_SWITCH && PS16DZSerial && ps16dz_synced) { // ignore to prevent loop from pushing state from faceplate interaction

ps16dz_synced = false;
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+UPDATE=\"sequence\":\""));
print_uint64_t(ps16dz_seq++);
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, rpower?"on":"off");
Expand All @@ -92,11 +94,12 @@ boolean PS16DZSetPower(void)

void PS16DZSerialDuty(uint8_t duty)
{
if (duty > 0 && !ps16dz_ignore_dim && PS16DZSerial) {
if (duty > 0 && !ps16dz_ignore_dim && PS16DZSerial && ps16dz_synced) {
if (duty < 25) {
duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself
}

ps16dz_synced = false;
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+UPDATE=\"sequence\":\""));
print_uint64_t(ps16dz_seq++);
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"bright\":\"%d\""), ps16dz_tx_buffer, round(duty * (100. / 255.)));
Expand Down Expand Up @@ -191,9 +194,13 @@ void PS16DZSerialInput(void)
}
}
else if(!strncmp(token2, "\"sequence\"", 10)){
ps16dz_seq = strtoull(token3+1, NULL, 10);
uint64_t ps16dz_seq_tmp = strtoull(token3+1, NULL, 10);
snprintf_P(log_data, sizeof(log_data), PSTR("PSD: sequence received: %s"), token3);
AddLog(LOG_LEVEL_DEBUG);
if(ps16dz_seq_tmp >= ps16dz_seq){
ps16dz_synced = true;
ps16dz_seq = ps16dz_seq_tmp;
}
}
token = strtok_r(NULL, ",", &end_str);
}
Expand Down

0 comments on commit 7203d6a

Please sign in to comment.