|
| 1 | +/* |
| 2 | + xdrv_19_ps16dz_dimmer.ino - PS_16_DZ dimmer support for Sonoff-Tasmota |
| 3 | +
|
| 4 | + Copyright (C) 2018 Joel Stein and Theo Arends |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +#ifdef USE_PS_16_DZ |
| 21 | + |
| 22 | +#define XDRV_19 19 |
| 23 | + |
| 24 | +#define PS16DZ_BUFFER_SIZE 256 |
| 25 | + |
| 26 | +#include <TasmotaSerial.h> |
| 27 | + |
| 28 | +TasmotaSerial *PS16DZSerial = nullptr; |
| 29 | + |
| 30 | +boolean ps16dz_ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction |
| 31 | + |
| 32 | +boolean ps16dz_power = false; |
| 33 | +uint8_t ps16dz_bright = 0; |
| 34 | +//uint64_t ps16dz_seq = 0; |
| 35 | + |
| 36 | +char ps16dz_tx_buffer[PS16DZ_BUFFER_SIZE]; // Serial transmit buffer |
| 37 | +char ps16dz_rx_buffer[PS16DZ_BUFFER_SIZE]; // Serial receive buffer |
| 38 | +int ps16dz_byte_counter = 0; |
| 39 | + |
| 40 | +/*********************************************************************************************\ |
| 41 | + * Internal Functions |
| 42 | +\*********************************************************************************************/ |
| 43 | + |
| 44 | +void printTimestamp(void) |
| 45 | +{ |
| 46 | + snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s%d%03d"), ps16dz_tx_buffer, LocalTime(), millis()%1000); |
| 47 | +} |
| 48 | + |
| 49 | +boolean PS16DZSetPower(void) |
| 50 | +{ |
| 51 | + boolean status = false; |
| 52 | + |
| 53 | + uint8_t rpower = XdrvMailbox.index; |
| 54 | + int16_t source = XdrvMailbox.payload; |
| 55 | + |
| 56 | + if (source != SRC_SWITCH && PS16DZSerial) { // ignore to prevent loop from pushing state from faceplate interaction |
| 57 | + |
| 58 | + snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+UPDATE=\"sequence\":\"")); |
| 59 | + printTimestamp(); |
| 60 | + snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, rpower?"on":"off"); |
| 61 | + snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); |
| 62 | + AddLog(LOG_LEVEL_DEBUG); |
| 63 | + |
| 64 | + PS16DZSerial->print(ps16dz_tx_buffer); |
| 65 | + PS16DZSerial->write(0x1B); |
| 66 | + PS16DZSerial->flush(); |
| 67 | + |
| 68 | + status = true; |
| 69 | + } |
| 70 | + return status; |
| 71 | +} |
| 72 | + |
| 73 | +void PS16DZSerialDuty(uint8_t duty) |
| 74 | +{ |
| 75 | + if (duty > 0 && !ps16dz_ignore_dim && PS16DZSerial) { |
| 76 | + if (duty < 25) { |
| 77 | + duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself |
| 78 | + } |
| 79 | + |
| 80 | + snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+UPDATE=\"sequence\":\"")); |
| 81 | + printTimestamp(); |
| 82 | + snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(duty * (100. / 255.))); |
| 83 | + snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); |
| 84 | + AddLog(LOG_LEVEL_DEBUG); |
| 85 | + |
| 86 | + PS16DZSerial->print(ps16dz_tx_buffer); |
| 87 | + PS16DZSerial->write(0x1B); |
| 88 | + PS16DZSerial->flush(); |
| 89 | + |
| 90 | + } else { |
| 91 | + ps16dz_ignore_dim = false; // reset flag |
| 92 | + |
| 93 | + snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send Dim Level skipped due to 0 or already set. Value=%d"), duty); |
| 94 | + AddLog(LOG_LEVEL_DEBUG); |
| 95 | + |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void PS16DZResetWifi(void) |
| 100 | +{ |
| 101 | + if (!Settings.flag.button_restrict) { |
| 102 | + char scmnd[20]; |
| 103 | + snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2); |
| 104 | + ExecuteCommand(scmnd, SRC_BUTTON); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/*********************************************************************************************\ |
| 109 | + * API Functions |
| 110 | +\*********************************************************************************************/ |
| 111 | + |
| 112 | +boolean PS16DZModuleSelected(void) |
| 113 | +{ |
| 114 | + light_type = LT_SERIAL1; |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +void PS16DZInit(void) |
| 119 | +{ |
| 120 | + PS16DZSerial = new TasmotaSerial(pin[GPIO_RXD], pin[GPIO_TXD], 2); |
| 121 | + if (PS16DZSerial->begin(19200)) { |
| 122 | + if (PS16DZSerial->hardwareSerial()) { ClaimSerial(); } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void PS16DZSerialInput(void) |
| 127 | +{ |
| 128 | + char scmnd[20]; |
| 129 | + while (PS16DZSerial->available()) { |
| 130 | + yield(); |
| 131 | + byte serial_in_byte = PS16DZSerial->read(); |
| 132 | + if (serial_in_byte != 0x1B){ |
| 133 | + if (ps16dz_byte_counter || (!ps16dz_byte_counter && serial_in_byte == 'A')); |
| 134 | + ps16dz_rx_buffer[ps16dz_byte_counter++] = serial_in_byte; |
| 135 | + } |
| 136 | + else { |
| 137 | + ps16dz_rx_buffer[ps16dz_byte_counter++] = 0x00; |
| 138 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: command received: %s"), ps16dz_rx_buffer); |
| 139 | + AddLog(LOG_LEVEL_DEBUG); |
| 140 | + if(!strncmp(ps16dz_rx_buffer+3, "UPDATE", 6) || !strncmp(ps16dz_rx_buffer+3, "RESULT", 6)) { |
| 141 | + char *end_str; |
| 142 | + char *string = ps16dz_rx_buffer+10; |
| 143 | + char* token = strtok_r(string, ",", &end_str); |
| 144 | + while (token != NULL) { |
| 145 | + char* end_token; |
| 146 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: token = %s"), token); |
| 147 | + AddLog(LOG_LEVEL_DEBUG); |
| 148 | + char* token2 = strtok_r(token, ":", &end_token); |
| 149 | + char* token3 = strtok_r(NULL, ":", &end_token); |
| 150 | + if(!strncmp(token2, "\"switch\"", 8)){ |
| 151 | + ps16dz_power = !strncmp(token3, "\"on\"", 4)?true:false; |
| 152 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: power received: %s"), token3); |
| 153 | + AddLog(LOG_LEVEL_DEBUG); |
| 154 | + if((power || Settings.light_dimmer > 0) && (power !=ps16dz_power)) { |
| 155 | + ExecuteCommandPower(1, ps16dz_power, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction |
| 156 | + } |
| 157 | + } |
| 158 | + else if(!strncmp(token2, "\"bright\"", 8)){ |
| 159 | + ps16dz_bright = atoi(token3); |
| 160 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: brightness received: %d"), ps16dz_bright); |
| 161 | + AddLog(LOG_LEVEL_DEBUG); |
| 162 | + if(power && ps16dz_bright > 0) { |
| 163 | + |
| 164 | + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), ps16dz_bright ); |
| 165 | + |
| 166 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: Send CMND_DIMMER_STR=%s"), scmnd ); |
| 167 | + AddLog(LOG_LEVEL_DEBUG); |
| 168 | + |
| 169 | + ps16dz_ignore_dim = true; |
| 170 | + ExecuteCommand(scmnd, SRC_SWITCH); |
| 171 | + } |
| 172 | + } |
| 173 | + else if(!strncmp(token2, "\"sequence\"", 10)){ |
| 174 | + //ps16dz_seq = strtoull(token3+1, NULL, 10); |
| 175 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: sequence received: %s"), token3); |
| 176 | + AddLog(LOG_LEVEL_DEBUG); |
| 177 | + } |
| 178 | + token = strtok_r(NULL, ",", &end_str); |
| 179 | + } |
| 180 | + } |
| 181 | + else if(!strncmp(ps16dz_rx_buffer+3, "SETTING", 7)) { |
| 182 | + snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: Reset")); |
| 183 | + AddLog(LOG_LEVEL_DEBUG); |
| 184 | + PS16DZResetWifi(); |
| 185 | + } |
| 186 | + memset(ps16dz_rx_buffer, 0, sizeof(ps16dz_rx_buffer)); |
| 187 | + ps16dz_byte_counter = 0; |
| 188 | + |
| 189 | + snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+SEND=ok")); |
| 190 | + snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); |
| 191 | + AddLog(LOG_LEVEL_DEBUG); |
| 192 | + |
| 193 | + PS16DZSerial->print(ps16dz_tx_buffer); |
| 194 | + PS16DZSerial->write(0x1B); |
| 195 | + PS16DZSerial->flush(); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +/*********************************************************************************************\ |
| 203 | + * Interface |
| 204 | +\*********************************************************************************************/ |
| 205 | + |
| 206 | +boolean Xdrv19(byte function) |
| 207 | +{ |
| 208 | + boolean result = false; |
| 209 | + |
| 210 | + if (PS_16_DZ == Settings.module) { |
| 211 | + switch (function) { |
| 212 | + case FUNC_MODULE_INIT: |
| 213 | + result = PS16DZModuleSelected(); |
| 214 | + break; |
| 215 | + case FUNC_INIT: |
| 216 | + PS16DZInit(); |
| 217 | + break; |
| 218 | + case FUNC_LOOP: |
| 219 | + if (PS16DZSerial) { PS16DZSerialInput(); } |
| 220 | + break; |
| 221 | + case FUNC_SET_DEVICE_POWER: |
| 222 | + result = PS16DZSetPower(); |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + return result; |
| 227 | +} |
| 228 | + |
| 229 | +#endif // USE_PS_16_DZ |
0 commit comments