mirror of
https://github.com/arendst/Tasmota.git
synced 2025-08-03 07:57:43 +00:00
Code refactoring
Code refactoring
This commit is contained in:
parent
9bc7809ea5
commit
4938b1e31e
@ -62,7 +62,8 @@ int tuya_byte_counter = 0; // Index in serial receive buffer
|
||||
* Internal Functions
|
||||
\*********************************************************************************************/
|
||||
|
||||
void TuyaSendCmd(uint8_t cmd, uint8_t payload[] = nullptr, uint16_t payload_len = 0){
|
||||
void TuyaSendCmd(uint8_t cmd, uint8_t payload[] = nullptr, uint16_t payload_len = 0)
|
||||
{
|
||||
uint8_t checksum = (0xFF + cmd + (payload_len >> 8) + (payload_len & 0xFF));
|
||||
TuyaSerial->write(0x55); // Tuya header 55AA
|
||||
TuyaSerial->write(0xAA);
|
||||
@ -70,7 +71,7 @@ void TuyaSendCmd(uint8_t cmd, uint8_t payload[] = nullptr, uint16_t payload_len
|
||||
TuyaSerial->write(cmd); // Tuya command
|
||||
TuyaSerial->write(payload_len >> 8); // following data length (Hi)
|
||||
TuyaSerial->write(payload_len & 0xFF); // following data length (Lo)
|
||||
snprintf_P(log_data, sizeof(log_data), PSTR("TYA: TX Packet: \"55aa00%02x%02x%02x"), cmd, payload_len >> 8, payload_len & 0xFF);
|
||||
snprintf_P(log_data, sizeof(log_data), PSTR("TYA: Send \"55aa00%02x%02x%02x"), cmd, payload_len >> 8, payload_len & 0xFF);
|
||||
for (uint32_t i = 0; i < payload_len; ++i) {
|
||||
TuyaSerial->write(payload[i]);
|
||||
checksum += payload[i];
|
||||
@ -82,7 +83,8 @@ void TuyaSendCmd(uint8_t cmd, uint8_t payload[] = nullptr, uint16_t payload_len
|
||||
AddLog(LOG_LEVEL_DEBUG);
|
||||
}
|
||||
|
||||
void TuyaSendState(uint8_t id, uint8_t type, uint8_t* value){
|
||||
void TuyaSendState(uint8_t id, uint8_t type, uint8_t* value)
|
||||
{
|
||||
uint16_t payload_len = 4;
|
||||
uint8_t payload_buffer[8];
|
||||
payload_buffer[0] = id;
|
||||
@ -108,11 +110,13 @@ void TuyaSendState(uint8_t id, uint8_t type, uint8_t* value){
|
||||
TuyaSendCmd(TUYA_CMD_SET_DP, payload_buffer, payload_len);
|
||||
}
|
||||
|
||||
void TuyaSendBool(uint8_t id, bool value){
|
||||
void TuyaSendBool(uint8_t id, bool value)
|
||||
{
|
||||
TuyaSendState(id, TUYA_TYPE_BOOL, (uint8_t*)&value);
|
||||
}
|
||||
|
||||
void TuyaSendValue(uint8_t id, uint32_t value){
|
||||
void TuyaSendValue(uint8_t id, uint32_t value)
|
||||
{
|
||||
TuyaSendState(id, TUYA_TYPE_VALUE, (uint8_t*)(&value));
|
||||
}
|
||||
|
||||
@ -139,32 +143,27 @@ bool TuyaSetChannels(void)
|
||||
void LightSerialDuty(uint8_t duty)
|
||||
{
|
||||
if (duty > 0 && !tuya_ignore_dim && TuyaSerial) {
|
||||
if (duty < 25) {
|
||||
duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself
|
||||
}
|
||||
if (duty < 25) { duty = 25; } // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself
|
||||
|
||||
if (Settings.flag3.tuya_show_dimmer == 0) {
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim value=%d (id=%d)"), duty, Settings.param[P_TUYA_DIMMER_ID]);
|
||||
|
||||
if(Settings.flag3.tuya_show_dimmer == 0)
|
||||
{
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR( "TYA: Send Serial Packet Dim Value=%d (id=%d)"), duty, Settings.param[P_TUYA_DIMMER_ID]);
|
||||
TuyaSendValue(Settings.param[P_TUYA_DIMMER_ID], duty);
|
||||
}
|
||||
|
||||
} else {
|
||||
tuya_ignore_dim = false; // reset flag
|
||||
if(Settings.flag3.tuya_show_dimmer == 0)
|
||||
if (Settings.flag3.tuya_show_dimmer == 0) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim skipped value=%d"), duty); // due to 0 or already set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaRequestState(void)
|
||||
{
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR( "TYA: Send Dim Level skipped due to 0 or already set. Value=%d"), duty);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void TuyaRequestState(void){
|
||||
if (TuyaSerial) {
|
||||
|
||||
// Get current status of MCU
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Request MCU state"));
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Read MCU state"));
|
||||
|
||||
TuyaSendCmd(TUYA_CMD_QUERY_STATE);
|
||||
}
|
||||
@ -200,16 +199,14 @@ void TuyaPacketProcess(void)
|
||||
ExecuteCommandPower(1, tuya_buffer[10], SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
|
||||
}*/
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Device-%d --> MCU State: %s Current State:%s"),tuya_buffer[6],tuya_buffer[10]?"On":"Off",bitRead(power, tuya_buffer[6]-1)?"On":"Off");
|
||||
if((power || Settings.light_dimmer > 0) && (tuya_buffer[10] != bitRead(power, tuya_buffer[6]-1)))
|
||||
{
|
||||
if ((power || Settings.light_dimmer > 0) && (tuya_buffer[10] != bitRead(power, tuya_buffer[6]-1))) {
|
||||
ExecuteCommandPower(tuya_buffer[6], tuya_buffer[10], SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
|
||||
}
|
||||
}
|
||||
else if (tuya_buffer[5] == 8) { // dim packet
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), tuya_buffer[13]);
|
||||
if(Settings.flag3.tuya_show_dimmer == 0) //
|
||||
{
|
||||
if (Settings.flag3.tuya_show_dimmer == 0) {
|
||||
if (!Settings.param[P_TUYA_DIMMER_ID]) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Autoconfiguring Dimmer ID %d"), tuya_buffer[6]);
|
||||
Settings.param[P_TUYA_DIMMER_ID] = tuya_buffer[6];
|
||||
@ -217,12 +214,9 @@ void TuyaPacketProcess(void)
|
||||
|
||||
tuya_new_dim = round(tuya_buffer[13] * (100. / 255.));
|
||||
if ((power || Settings.flag3.tuya_apply_o20) && (tuya_new_dim > 0) && (abs(tuya_new_dim - Settings.light_dimmer) > 1)) {
|
||||
tuya_ignore_dim = true;
|
||||
|
||||
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), tuya_new_dim );
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send CMND_DIMMER_STR=%s"), scmnd );
|
||||
|
||||
tuya_ignore_dim = true;
|
||||
ExecuteCommand(scmnd, SRC_SWITCH);
|
||||
}
|
||||
}
|
||||
@ -361,7 +355,6 @@ void TuyaSerialInput(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool TuyaButtonPressed(void)
|
||||
{
|
||||
if (!XdrvMailbox.index && ((PRESSED == XdrvMailbox.payload) && (NOT_PRESSED == lastbutton[XdrvMailbox.index]))) {
|
||||
@ -372,7 +365,8 @@ bool TuyaButtonPressed(void)
|
||||
return false; // Don't serve other buttons
|
||||
}
|
||||
|
||||
void TuyaSetWifiLed(void){
|
||||
void TuyaSetWifiLed(void)
|
||||
{
|
||||
uint8_t wifi_state = 0x02;
|
||||
switch(WifiState()){
|
||||
case WIFI_SMARTCONFIG:
|
||||
@ -387,12 +381,11 @@ void TuyaSetWifiLed(void){
|
||||
break;
|
||||
}
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Set WiFi LED to state %d (%d)"), wifi_state, WifiState());
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Set WiFi LED %d (%d)"), wifi_state, WifiState());
|
||||
|
||||
TuyaSendCmd(TUYA_CMD_WIFI_STATE, &wifi_state, 1);
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Interface
|
||||
\*********************************************************************************************/
|
||||
|
@ -58,7 +58,7 @@ bool ps16dz_switch = false;
|
||||
|
||||
void PS16DZSerialSendTxBuffer(void)
|
||||
{
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Send serial command: %s"), ps16dz_tx_buffer);
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Send %s"), ps16dz_tx_buffer);
|
||||
|
||||
PS16DZSerial->print(ps16dz_tx_buffer);
|
||||
PS16DZSerial->write(0x1B);
|
||||
@ -73,9 +73,9 @@ void PS16DZSerialSendOkCommand(void)
|
||||
|
||||
// Send a serial update command to the LED controller
|
||||
// For dimmer types:
|
||||
// AT+UPDATE="sequence":"1554682835320","switch":"on":"bright":100
|
||||
// AT+UPDATE="sequence":"1554682835320","switch":"on","bright":100
|
||||
// For color types:
|
||||
// AT+UPDATE="sequence":"1554682835320","switch":"on":,"bright":100,"mode":1,"colorR":255,"colorG":46,"colorB":101,"light_types":1
|
||||
// AT+UPDATE="sequence":"1554682835320","switch":"on","bright":100,"mode":1,"colorR":255,"colorG":46,"colorB":101,"light_types":1
|
||||
void PS16DZSerialSendUpdateCommand(void)
|
||||
{
|
||||
uint8_t light_state_dimmer = light_state.getDimmer();
|
||||
@ -166,7 +166,7 @@ void PS16DZSerialInput(void)
|
||||
} else {
|
||||
ps16dz_rx_buffer[ps16dz_byte_counter++] = 0x00;
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: command received: %s"), ps16dz_rx_buffer);
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Received %s"), ps16dz_rx_buffer);
|
||||
|
||||
if (!strncmp(ps16dz_rx_buffer+3, "UPDATE", 6)) {
|
||||
char *end_str;
|
||||
@ -185,17 +185,12 @@ void PS16DZSerialInput(void)
|
||||
char* token3 = strtok_r(nullptr, ":", &end_token);
|
||||
|
||||
if (!strncmp(token2, "\"switch\"", 8)) {
|
||||
|
||||
ps16dz_switch = !strncmp(token3, "\"on\"", 4) ? true : false;
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: switch received: %d"), ps16dz_switch);
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Switch %d"), ps16dz_switch);
|
||||
|
||||
is_switch_change = (ps16dz_switch != power);
|
||||
|
||||
if (is_switch_change) {
|
||||
|
||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Send CMND_POWER=%d"), ps16dz_switch );
|
||||
|
||||
ExecuteCommandPower(1, ps16dz_switch, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
|
||||
}
|
||||
}
|
||||
@ -203,7 +198,6 @@ void PS16DZSerialInput(void)
|
||||
|
||||
char color_channel_name = token2[6];
|
||||
int color_index;
|
||||
|
||||
switch(color_channel_name)
|
||||
{
|
||||
case 'R': color_index = 0;
|
||||
@ -213,60 +207,51 @@ void PS16DZSerialInput(void)
|
||||
case 'B': color_index = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
int color_value = atoi(token3);
|
||||
ps16dz_color[color_index] = color_value;
|
||||
color_updated[color_index] = true;
|
||||
|
||||
bool all_color_channels_updated = color_updated[0] && color_updated[1] && color_updated[2];
|
||||
|
||||
if (all_color_channels_updated) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: color received: R:%d, G:%d, B:%d"), ps16dz_color[0], ps16dz_color[1], ps16dz_color[2]);
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Color R:%d, G:%d, B:%d"), ps16dz_color[0], ps16dz_color[1], ps16dz_color[2]);
|
||||
|
||||
is_color_change = (memcmp(ps16dz_color, Settings.light_color, 3) != 0);
|
||||
}
|
||||
|
||||
if (power && is_color_change) {
|
||||
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_COLOR "2 %02x%02x%02x"), ps16dz_color[0], ps16dz_color[1], ps16dz_color[2]);
|
||||
|
||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Send CMND_COLOR_STR=%s"), scmnd );
|
||||
|
||||
ExecuteCommand(scmnd, SRC_SWITCH);
|
||||
}
|
||||
}
|
||||
else if (!strncmp(token2, "\"bright\"", 8)) {
|
||||
|
||||
ps16dz_dimmer = atoi(token3);
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: brightness received: %d"), ps16dz_dimmer);
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Brightness %d"), ps16dz_dimmer);
|
||||
|
||||
is_brightness_change = ps16dz_dimmer != Settings.light_dimmer;
|
||||
|
||||
if (power && (ps16dz_dimmer > 0) && is_brightness_change) {
|
||||
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), ps16dz_dimmer);
|
||||
|
||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Send CMND_DIMMER_STR=%s"), scmnd);
|
||||
|
||||
ExecuteCommand(scmnd, SRC_SWITCH);
|
||||
}
|
||||
}
|
||||
else if (!strncmp(token2, "\"sequence\"", 10)) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: sequence received: %s"), token3);
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Sequence %s"), token3);
|
||||
|
||||
}
|
||||
token = strtok_r(nullptr, ",", &end_str);
|
||||
}
|
||||
|
||||
if (!is_color_change && !is_brightness_change) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Update received"));
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PSZ: Update"));
|
||||
|
||||
PS16DZSerialSendOkCommand();
|
||||
}
|
||||
}
|
||||
else if (!strncmp(ps16dz_rx_buffer+3, "SETTING", 7)) {
|
||||
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("PSZ: Reset"));
|
||||
|
||||
if (!Settings.flag.button_restrict) {
|
||||
char scmnd[20];
|
||||
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_WIFICONFIG " 2"));
|
||||
ExecuteCommand(scmnd, SRC_BUTTON);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user