2018-11-24 02:29:32 +00:00
|
|
|
/*
|
2018-11-24 15:24:44 +00:00
|
|
|
xdrv_19_ps16dz_dimmer.ino - PS_16_DZ dimmer support for Sonoff-Tasmota
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Joel Stein and Theo Arends
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_PS_16_DZ
|
|
|
|
|
|
|
|
#define XDRV_19 19
|
|
|
|
|
2018-11-27 15:52:09 +00:00
|
|
|
#define PS16DZ_BUFFER_SIZE 80
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-27 16:18:41 +00:00
|
|
|
#define PS16DZ_TYPE_ACK 0
|
|
|
|
#define PS16DZ_TYPE_PWR 1
|
|
|
|
#define PS16DZ_TYPE_DIM 2
|
|
|
|
|
2018-11-24 02:29:32 +00:00
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
|
|
|
TasmotaSerial *PS16DZSerial = nullptr;
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool ps16dz_ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-27 01:04:59 +00:00
|
|
|
//uint64_t ps16dz_seq = 0;
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-27 10:55:54 +00:00
|
|
|
char *ps16dz_tx_buffer = NULL; // Serial transmit buffer
|
|
|
|
char *ps16dz_rx_buffer = NULL; // Serial receive buffer
|
2018-11-25 21:43:28 +00:00
|
|
|
int ps16dz_byte_counter = 0;
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Internal Functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-27 01:04:59 +00:00
|
|
|
void printTimestamp(void)
|
2018-11-26 12:49:21 +00:00
|
|
|
{
|
2018-11-27 11:18:27 +00:00
|
|
|
snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "%s%d%03d"), ps16dz_tx_buffer, LocalTime(), millis()%1000);
|
2018-11-26 12:49:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 16:18:41 +00:00
|
|
|
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:
|
|
|
|
case PS16DZ_TYPE_DIM:
|
|
|
|
snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\""));
|
|
|
|
printTimestamp();
|
2018-11-27 17:02:00 +00:00
|
|
|
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.)));
|
|
|
|
}
|
2018-11-27 16:18:41 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool PS16DZSetPower(void)
|
2018-11-24 02:29:32 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool status = false;
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
uint8_t rpower = XdrvMailbox.index;
|
|
|
|
int16_t source = XdrvMailbox.payload;
|
|
|
|
|
2018-11-26 12:27:30 +00:00
|
|
|
if (source != SRC_SWITCH && PS16DZSerial) { // ignore to prevent loop from pushing state from faceplate interaction
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-27 16:18:41 +00:00
|
|
|
PS16DZSendCommand(PS16DZ_TYPE_PWR, rpower);
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
status = true;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool PS16DZSetChannels(void)
|
2018-12-17 16:34:55 +00:00
|
|
|
{
|
|
|
|
PS16DZSerialDuty(((uint8_t*)XdrvMailbox.data)[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-24 02:29:32 +00:00
|
|
|
void PS16DZSerialDuty(uint8_t duty)
|
|
|
|
{
|
2018-11-26 12:27:30 +00:00
|
|
|
if (duty > 0 && !ps16dz_ignore_dim && PS16DZSerial) {
|
2018-11-24 02:29:32 +00:00
|
|
|
if (duty < 25) {
|
|
|
|
duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself
|
|
|
|
}
|
|
|
|
|
2018-11-27 16:18:41 +00:00
|
|
|
PS16DZSendCommand(PS16DZ_TYPE_DIM, duty);
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ps16dz_ignore_dim = false; // reset flag
|
|
|
|
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send Dim Level skipped due to 0 or already set. Value=%d"), duty);
|
2018-11-24 02:29:32 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS16DZResetWifi(void)
|
|
|
|
{
|
|
|
|
if (!Settings.flag.button_restrict) {
|
|
|
|
char scmnd[20];
|
|
|
|
snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2);
|
|
|
|
ExecuteCommand(scmnd, SRC_BUTTON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* API Functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool PS16DZModuleSelected(void)
|
2018-11-24 02:29:32 +00:00
|
|
|
{
|
2018-11-24 04:08:14 +00:00
|
|
|
light_type = LT_SERIAL1;
|
2018-11-24 02:29:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS16DZInit(void)
|
|
|
|
{
|
2018-11-27 11:18:27 +00:00
|
|
|
ps16dz_tx_buffer = (char*)(malloc(PS16DZ_BUFFER_SIZE));
|
2018-11-27 10:55:54 +00:00
|
|
|
if (ps16dz_tx_buffer != NULL) {
|
2018-11-27 11:18:27 +00:00
|
|
|
ps16dz_rx_buffer = (char*)(malloc(PS16DZ_BUFFER_SIZE));
|
2018-11-27 10:55:54 +00:00
|
|
|
if (ps16dz_rx_buffer != NULL) {
|
|
|
|
PS16DZSerial = new TasmotaSerial(pin[GPIO_RXD], pin[GPIO_TXD], 2);
|
|
|
|
if (PS16DZSerial->begin(19200)) {
|
|
|
|
if (PS16DZSerial->hardwareSerial()) { ClaimSerial(); }
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS16DZSerialInput(void)
|
|
|
|
{
|
|
|
|
char scmnd[20];
|
|
|
|
while (PS16DZSerial->available()) {
|
|
|
|
yield();
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t serial_in_byte = PS16DZSerial->read();
|
2018-11-25 21:43:28 +00:00
|
|
|
if (serial_in_byte != 0x1B){
|
2018-11-27 18:01:06 +00:00
|
|
|
if (ps16dz_byte_counter >= PS16DZ_BUFFER_SIZE - 1) {
|
2018-11-27 15:52:09 +00:00
|
|
|
memset(ps16dz_rx_buffer, 0, PS16DZ_BUFFER_SIZE);
|
|
|
|
ps16dz_byte_counter = 0;
|
|
|
|
}
|
2018-12-23 20:06:10 +00:00
|
|
|
if (ps16dz_byte_counter || (!ps16dz_byte_counter && serial_in_byte == 'A')) {
|
|
|
|
ps16dz_rx_buffer[ps16dz_byte_counter++] = serial_in_byte;
|
|
|
|
}
|
2018-11-25 21:43:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ps16dz_rx_buffer[ps16dz_byte_counter++] = 0x00;
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: command received: %s"), ps16dz_rx_buffer);
|
2018-11-25 21:43:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
if(!strncmp(ps16dz_rx_buffer+3, "UPDATE", 6) || !strncmp(ps16dz_rx_buffer+3, "RESULT", 6)) {
|
|
|
|
char *end_str;
|
|
|
|
char *string = ps16dz_rx_buffer+10;
|
|
|
|
char* token = strtok_r(string, ",", &end_str);
|
|
|
|
while (token != NULL) {
|
|
|
|
char* end_token;
|
|
|
|
char* token2 = strtok_r(token, ":", &end_token);
|
|
|
|
char* token3 = strtok_r(NULL, ":", &end_token);
|
|
|
|
if(!strncmp(token2, "\"switch\"", 8)){
|
2019-01-28 13:08:33 +00:00
|
|
|
bool ps16dz_power = !strncmp(token3, "\"on\"", 4)?true:false;
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: power received: %s"), token3);
|
2018-11-25 21:43:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
if((power || Settings.light_dimmer > 0) && (power !=ps16dz_power)) {
|
|
|
|
ExecuteCommandPower(1, ps16dz_power, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
|
|
|
|
}
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
2018-11-25 21:43:28 +00:00
|
|
|
else if(!strncmp(token2, "\"bright\"", 8)){
|
2018-11-27 15:52:09 +00:00
|
|
|
uint8_t ps16dz_bright = atoi(token3);
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: brightness received: %d"), ps16dz_bright);
|
2018-11-25 21:43:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2018-11-27 15:11:14 +00:00
|
|
|
if(power && ps16dz_bright > 0 && ps16dz_bright != Settings.light_dimmer) {
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-25 21:43:28 +00:00
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), ps16dz_bright );
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: Send CMND_DIMMER_STR=%s"), scmnd );
|
2018-11-25 21:43:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2018-11-25 21:43:28 +00:00
|
|
|
ps16dz_ignore_dim = true;
|
|
|
|
ExecuteCommand(scmnd, SRC_SWITCH);
|
|
|
|
}
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
2018-11-25 21:43:28 +00:00
|
|
|
else if(!strncmp(token2, "\"sequence\"", 10)){
|
2018-11-26 12:49:21 +00:00
|
|
|
//ps16dz_seq = strtoull(token3+1, NULL, 10);
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: sequence received: %s"), token3);
|
2018-11-25 21:43:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2018-11-27 10:55:54 +00:00
|
|
|
}
|
2018-11-25 21:43:28 +00:00
|
|
|
token = strtok_r(NULL, ",", &end_str);
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-25 21:43:28 +00:00
|
|
|
else if(!strncmp(ps16dz_rx_buffer+3, "SETTING", 7)) {
|
2018-11-26 14:27:51 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: Reset"));
|
2018-11-25 21:43:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
PS16DZResetWifi();
|
|
|
|
}
|
2018-11-27 11:18:27 +00:00
|
|
|
memset(ps16dz_rx_buffer, 0, PS16DZ_BUFFER_SIZE);
|
2018-11-25 21:43:28 +00:00
|
|
|
ps16dz_byte_counter = 0;
|
|
|
|
|
2018-11-27 16:18:41 +00:00
|
|
|
PS16DZSendCommand();
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xdrv19(uint8_t function)
|
2018-11-24 02:29:32 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
if (PS_16_DZ == Settings.module) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_MODULE_INIT:
|
|
|
|
result = PS16DZModuleSelected();
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
PS16DZInit();
|
|
|
|
break;
|
|
|
|
case FUNC_LOOP:
|
|
|
|
if (PS16DZSerial) { PS16DZSerialInput(); }
|
|
|
|
break;
|
|
|
|
case FUNC_SET_DEVICE_POWER:
|
|
|
|
result = PS16DZSetPower();
|
|
|
|
break;
|
2018-12-17 16:34:55 +00:00
|
|
|
case FUNC_SET_CHANNELS:
|
|
|
|
result = PS16DZSetChannels();
|
|
|
|
break;
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PS_16_DZ
|