2018-11-24 02:29:32 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdrv_19_ps16dz.dimmer.ino - PS_16_DZ dimmer support for Tasmota
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-16 15:43:23 +01:00
|
|
|
#ifdef USE_LIGHT
|
2018-11-24 02:29:32 +00:00
|
|
|
#ifdef USE_PS_16_DZ
|
2019-07-02 11:26:04 +01:00
|
|
|
/*********************************************************************************************\
|
2019-10-17 11:20:26 +01:00
|
|
|
* PS 16 DZ Serial Dimmer
|
2019-07-02 11:26:04 +01:00
|
|
|
\*********************************************************************************************/
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
#define XDRV_19 19
|
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
#define PS16DZ_BUFFER_SIZE 80
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2018-11-24 02:29:32 +00:00
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
|
|
|
TasmotaSerial *PS16DZSerial = nullptr;
|
|
|
|
|
2019-08-17 13:48:42 +01:00
|
|
|
struct PS16DZ {
|
|
|
|
char *rx_buffer = nullptr; // Serial receive buffer
|
|
|
|
int byte_counter = 0;
|
|
|
|
uint8_t dimmer = 0;
|
|
|
|
} Ps16dz;
|
2018-11-24 02:29:32 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Internal Functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
void PS16DZSerialSend(const char *tx_buffer)
|
2019-04-10 13:54:50 +01:00
|
|
|
{
|
2021-01-23 16:24:54 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("PSZ: Send %s"), tx_buffer);
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
PS16DZSerial->print(tx_buffer);
|
2019-07-02 11:26:04 +01:00
|
|
|
PS16DZSerial->write(0x1B);
|
|
|
|
PS16DZSerial->flush();
|
2019-04-10 13:54:50 +01:00
|
|
|
}
|
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
void PS16DZSerialSendOk(void)
|
2018-11-24 02:29:32 +00:00
|
|
|
{
|
2019-10-17 11:20:26 +01:00
|
|
|
char tx_buffer[16];
|
|
|
|
snprintf_P(tx_buffer, sizeof(tx_buffer), PSTR("AT+SEND=ok"));
|
|
|
|
PS16DZSerialSend(tx_buffer);
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 21:04:24 +01:00
|
|
|
// Send a serial update command to the LED controller
|
|
|
|
// For dimmer types:
|
2019-07-02 14:49:08 +01:00
|
|
|
// AT+UPDATE="sequence":"1554682835320","switch":"on","bright":100
|
2019-07-02 11:26:04 +01:00
|
|
|
void PS16DZSerialSendUpdateCommand(void)
|
2019-04-10 13:54:50 +01:00
|
|
|
{
|
2019-07-02 11:26:04 +01:00
|
|
|
uint8_t light_state_dimmer = light_state.getDimmer();
|
2019-06-29 03:25:39 +01:00
|
|
|
// Dimming acts odd below 10% - this mirrors the threshold set on the faceplate itself
|
2021-06-11 17:14:12 +01:00
|
|
|
light_state_dimmer = (light_state_dimmer < Settings->dimmer_hw_min) ? Settings->dimmer_hw_min : light_state_dimmer;
|
|
|
|
light_state_dimmer = (light_state_dimmer > Settings->dimmer_hw_max) ? Settings->dimmer_hw_max : light_state_dimmer;
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
char tx_buffer[80];
|
|
|
|
snprintf_P(tx_buffer, sizeof(tx_buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"switch\":\"%s\",\"bright\":%d"),
|
2020-10-28 18:03:39 +00:00
|
|
|
LocalTime(), millis()%1000, TasmotaGlobal.power?"on":"off", light_state_dimmer);
|
2019-06-29 03:25:39 +01:00
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
PS16DZSerialSend(tx_buffer);
|
2019-06-30 21:04:24 +01:00
|
|
|
}
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2019-07-02 11:26:04 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* API Functions
|
|
|
|
\*********************************************************************************************/
|
2019-04-10 13:54:50 +01:00
|
|
|
|
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();
|
2019-07-02 11:26:04 +01:00
|
|
|
if (serial_in_byte != 0x1B) {
|
2019-08-17 13:48:42 +01:00
|
|
|
if (Ps16dz.byte_counter >= PS16DZ_BUFFER_SIZE - 1) {
|
|
|
|
memset(Ps16dz.rx_buffer, 0, PS16DZ_BUFFER_SIZE);
|
|
|
|
Ps16dz.byte_counter = 0;
|
2018-11-27 15:52:09 +00:00
|
|
|
}
|
2019-08-17 13:48:42 +01:00
|
|
|
if (Ps16dz.byte_counter || (!Ps16dz.byte_counter && ('A' == serial_in_byte))) {
|
|
|
|
Ps16dz.rx_buffer[Ps16dz.byte_counter++] = serial_in_byte;
|
2018-12-23 20:06:10 +00:00
|
|
|
}
|
2019-07-02 11:26:04 +01:00
|
|
|
} else {
|
2019-08-17 13:48:42 +01:00
|
|
|
Ps16dz.rx_buffer[Ps16dz.byte_counter++] = 0x00;
|
2019-07-02 11:26:04 +01:00
|
|
|
|
2019-07-22 17:21:25 +01:00
|
|
|
// AT+RESULT="sequence":"1554682835320"
|
2021-01-23 16:24:54 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("PSZ: Rcvd %s"), Ps16dz.rx_buffer);
|
2019-10-17 11:20:26 +01:00
|
|
|
|
|
|
|
if (!strncmp(Ps16dz.rx_buffer+3, "RESULT", 6)) {
|
2019-07-02 11:26:04 +01:00
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
}
|
|
|
|
else if (!strncmp(Ps16dz.rx_buffer+3, "UPDATE", 6)) {
|
|
|
|
// AT+UPDATE="switch":"on","bright":100
|
2018-11-25 21:43:28 +00:00
|
|
|
char *end_str;
|
2019-08-17 13:48:42 +01:00
|
|
|
char *string = Ps16dz.rx_buffer+10;
|
2019-07-02 11:26:04 +01:00
|
|
|
char *token = strtok_r(string, ",", &end_str);
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2019-06-30 19:32:34 +01:00
|
|
|
bool is_switch_change = false;
|
2019-06-29 03:25:39 +01:00
|
|
|
bool is_brightness_change = false;
|
|
|
|
|
2019-03-26 17:26:50 +00:00
|
|
|
while (token != nullptr) {
|
2018-11-25 21:43:28 +00:00
|
|
|
char* end_token;
|
|
|
|
char* token2 = strtok_r(token, ":", &end_token);
|
2019-03-26 17:26:50 +00:00
|
|
|
char* token3 = strtok_r(nullptr, ":", &end_token);
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2019-07-02 11:26:04 +01:00
|
|
|
if (!strncmp(token2, "\"switch\"", 8)) {
|
2019-10-17 11:20:26 +01:00
|
|
|
bool switch_state = !strncmp(token3, "\"on\"", 4) ? true : false;
|
2019-06-30 19:32:34 +01:00
|
|
|
|
2021-01-23 16:24:54 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("PSZ: Switch %d"), switch_state);
|
2019-06-30 19:32:34 +01:00
|
|
|
|
2020-10-28 18:03:39 +00:00
|
|
|
is_switch_change = (switch_state != TasmotaGlobal.power);
|
2019-07-02 11:26:04 +01:00
|
|
|
if (is_switch_change) {
|
2019-10-17 11:20:26 +01:00
|
|
|
ExecuteCommandPower(1, switch_state, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
|
2018-11-25 21:43:28 +00:00
|
|
|
}
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
2019-07-02 11:26:04 +01:00
|
|
|
else if (!strncmp(token2, "\"bright\"", 8)) {
|
2019-08-17 13:48:42 +01:00
|
|
|
Ps16dz.dimmer = atoi(token3);
|
2019-07-02 11:26:04 +01:00
|
|
|
|
2021-01-23 16:24:54 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("PSZ: Brightness %d"), Ps16dz.dimmer);
|
2018-11-24 02:29:32 +00:00
|
|
|
|
2021-06-11 17:14:12 +01:00
|
|
|
is_brightness_change = Ps16dz.dimmer != Settings->light_dimmer;
|
2020-10-28 18:03:39 +00:00
|
|
|
if (TasmotaGlobal.power && (Ps16dz.dimmer > 0) && is_brightness_change) {
|
2019-08-17 13:48:42 +01:00
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), Ps16dz.dimmer);
|
2019-06-29 03:25:39 +01:00
|
|
|
ExecuteCommand(scmnd, SRC_SWITCH);
|
|
|
|
}
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
2019-07-02 11:26:04 +01:00
|
|
|
else if (!strncmp(token2, "\"sequence\"", 10)) {
|
2019-07-02 14:49:08 +01:00
|
|
|
|
2021-01-23 16:24:54 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("PSZ: Sequence %s"), token3);
|
2019-07-02 14:49:08 +01:00
|
|
|
|
2018-11-27 10:55:54 +00:00
|
|
|
}
|
2019-03-26 17:26:50 +00:00
|
|
|
token = strtok_r(nullptr, ",", &end_str);
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
2019-04-10 13:54:50 +01:00
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
if (!is_brightness_change) {
|
2019-07-02 14:49:08 +01:00
|
|
|
|
2021-01-23 16:24:54 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("PSZ: Update"));
|
2019-07-02 11:26:04 +01:00
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
PS16DZSerialSendOk();
|
2019-04-10 13:54:50 +01:00
|
|
|
}
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
2019-08-17 13:48:42 +01:00
|
|
|
else if (!strncmp(Ps16dz.rx_buffer+3, "SETTING", 7)) {
|
2019-07-23 13:40:33 +01:00
|
|
|
// AT+SETTING=enterESPTOUCH - When ON button is held for over 5 seconds
|
|
|
|
// AT+SETTING=exitESPTOUCH - When ON button is pressed
|
2021-06-11 17:14:12 +01:00
|
|
|
if (!Settings->flag.button_restrict) { // SetOption1 - Control button multipress
|
2019-07-23 13:40:33 +01:00
|
|
|
int state = WIFI_MANAGER;
|
2019-08-17 13:48:42 +01:00
|
|
|
if (!strncmp(Ps16dz.rx_buffer+10, "=exit", 5)) { state = WIFI_RETRY; }
|
2021-06-11 17:14:12 +01:00
|
|
|
if (state != Settings->sta_config) {
|
2019-07-23 13:40:33 +01:00
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_WIFICONFIG " %d"), state);
|
|
|
|
ExecuteCommand(scmnd, SRC_BUTTON);
|
|
|
|
}
|
2019-07-02 11:26:04 +01:00
|
|
|
}
|
2018-11-25 21:43:28 +00:00
|
|
|
}
|
2019-08-17 13:48:42 +01:00
|
|
|
memset(Ps16dz.rx_buffer, 0, PS16DZ_BUFFER_SIZE);
|
|
|
|
Ps16dz.byte_counter = 0;
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 11:20:26 +01:00
|
|
|
bool PS16DZSerialSendUpdateCommandIfRequired(void)
|
|
|
|
{
|
|
|
|
if (!PS16DZSerial) { return true; }
|
|
|
|
|
|
|
|
bool is_switch_change = (XdrvMailbox.payload != SRC_SWITCH);
|
|
|
|
bool is_brightness_change = (light_state.getDimmer() != Ps16dz.dimmer);
|
|
|
|
|
|
|
|
if (is_switch_change || is_brightness_change) {
|
|
|
|
PS16DZSerialSendUpdateCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS16DZInit(void)
|
|
|
|
{
|
|
|
|
Ps16dz.rx_buffer = (char*)(malloc(PS16DZ_BUFFER_SIZE));
|
|
|
|
if (Ps16dz.rx_buffer != nullptr) {
|
2020-04-26 16:33:27 +01:00
|
|
|
PS16DZSerial = new TasmotaSerial(Pin(GPIO_RXD), Pin(GPIO_TXD), 2);
|
2019-10-17 11:20:26 +01:00
|
|
|
if (PS16DZSerial->begin(19200)) {
|
|
|
|
if (PS16DZSerial->hardwareSerial()) { ClaimSerial(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PS16DZModuleSelected(void)
|
|
|
|
{
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.devices_present++;
|
|
|
|
TasmotaGlobal.light_type = LT_SERIAL1;
|
2019-10-17 11:20:26 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-24 02:29:32 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xdrv19(uint32_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
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
if (PS_16_DZ == TasmotaGlobal.module_type) {
|
2018-11-24 02:29:32 +00:00
|
|
|
switch (function) {
|
2019-03-30 12:03:45 +00:00
|
|
|
case FUNC_LOOP:
|
|
|
|
if (PS16DZSerial) { PS16DZSerialInput(); }
|
|
|
|
break;
|
2019-10-17 11:20:26 +01:00
|
|
|
case FUNC_SET_DEVICE_POWER:
|
|
|
|
case FUNC_SET_CHANNELS:
|
|
|
|
result = PS16DZSerialSendUpdateCommandIfRequired();
|
2018-11-24 02:29:32 +00:00
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
PS16DZInit();
|
|
|
|
break;
|
2019-10-17 11:20:26 +01:00
|
|
|
case FUNC_MODULE_INIT:
|
|
|
|
result = PS16DZModuleSelected();
|
2018-12-17 16:34:55 +00:00
|
|
|
break;
|
2018-11-24 02:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PS_16_DZ
|
2019-06-16 15:43:23 +01:00
|
|
|
#endif // USE_LIGHT
|