Code refactoring

Code refactoring
This commit is contained in:
Theo Arends 2019-07-02 15:49:08 +02:00
parent 9bc7809ea5
commit 4938b1e31e
2 changed files with 76 additions and 98 deletions

View File

@ -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,12 +83,13 @@ 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;
payload_buffer[1] = type;
switch(type){
switch (type) {
case TUYA_TYPE_BOOL:
payload_len += 1;
payload_buffer[2] = 0x00;
@ -108,12 +110,14 @@ 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){
TuyaSendState(id, TUYA_TYPE_BOOL, (uint8_t*)&value);
void TuyaSendBool(uint8_t id, bool value)
{
TuyaSendState(id, TUYA_TYPE_BOOL, (uint8_t*)&value);
}
void TuyaSendValue(uint8_t id, uint32_t value){
TuyaSendState(id, TUYA_TYPE_VALUE, (uint8_t*)(&value));
void TuyaSendValue(uint8_t id, uint32_t value)
{
TuyaSendState(id, TUYA_TYPE_VALUE, (uint8_t*)(&value));
}
bool TuyaSetPower(void)
@ -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]);
TuyaSendValue(Settings.param[P_TUYA_DIMMER_ID], duty);
}
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)
{
AddLog_P2(LOG_LEVEL_DEBUG, PSTR( "TYA: Send Dim Level skipped due to 0 or already set. Value=%d"), duty);
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){
if(TuyaSerial) {
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);
}
@ -183,11 +182,11 @@ void TuyaPacketProcess(void)
{
char scmnd[20];
switch(tuya_buffer[3]) {
switch (tuya_buffer[3]) {
case TUYA_CMD_HEARTBEAT:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Heartbeat"));
if(tuya_buffer[6] == 0){
if (tuya_buffer[6] == 0) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Detected MCU restart"));
tuya_wifi_state = -2;
}
@ -196,33 +195,28 @@ void TuyaPacketProcess(void)
case TUYA_CMD_STATE:
if (tuya_buffer[5] == 5) { // on/off packet
/*if((power || Settings.light_dimmer > 0) && (power != tuya_buffer[10])) {
/*if ((power || Settings.light_dimmer > 0) && (power != tuya_buffer[10])) {
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)))
{
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
}
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))) {
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];
}
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)) {
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);
}
}
@ -252,11 +246,11 @@ void TuyaPacketProcess(void)
if (Settings.my_gp.io[i] == GPIO_LED1) led1_set = true;
else if (Settings.my_gp.io[i] == GPIO_KEY1) key1_set = true;
}
if(!Settings.my_gp.io[led1_gpio] && !led1_set){
if (!Settings.my_gp.io[led1_gpio] && !led1_set) {
Settings.my_gp.io[led1_gpio] = GPIO_LED1;
restart_flag = 2;
}
if(!Settings.my_gp.io[key1_gpio] && !key1_set){
if (!Settings.my_gp.io[key1_gpio] && !key1_set) {
Settings.my_gp.io[key1_gpio] = GPIO_KEY1;
restart_flag = 2;
}
@ -317,7 +311,7 @@ void TuyaSerialInput(void)
tuya_buffer[tuya_byte_counter++] = serial_in_byte;
tuya_cmd_checksum += serial_in_byte;
}
else if (tuya_cmd_status == 1 && serial_in_byte == 0xAA){ // Only packtes with header 0x55AA are valid
else if (tuya_cmd_status == 1 && serial_in_byte == 0xAA) { // Only packtes with header 0x55AA are valid
tuya_cmd_status = 2;
tuya_byte_counter = 0;
@ -325,15 +319,15 @@ void TuyaSerialInput(void)
tuya_buffer[tuya_byte_counter++] = 0xAA;
tuya_cmd_checksum = 0xFF;
}
else if (tuya_cmd_status == 2){
if(tuya_byte_counter == 5){ // Get length of data
else if (tuya_cmd_status == 2) {
if (tuya_byte_counter == 5) { // Get length of data
tuya_cmd_status = 3;
tuya_data_len = serial_in_byte;
}
tuya_cmd_checksum += serial_in_byte;
tuya_buffer[tuya_byte_counter++] = serial_in_byte;
}
else if ((tuya_cmd_status == 3) && (tuya_byte_counter == (6 + tuya_data_len)) && (tuya_cmd_checksum == serial_in_byte)){ // Compare checksum and process packet
else if ((tuya_cmd_status == 3) && (tuya_byte_counter == (6 + tuya_data_len)) && (tuya_cmd_checksum == serial_in_byte)) { // Compare checksum and process packet
tuya_buffer[tuya_byte_counter++] = serial_in_byte;
snprintf_P(log_data, sizeof(log_data), PSTR("TYA: RX Packet: \""));
@ -349,7 +343,7 @@ void TuyaSerialInput(void)
tuya_cmd_checksum = 0;
tuya_data_len = 0;
} // read additional packets from TUYA
else if(tuya_byte_counter < TUYA_BUFFER_SIZE -1) { // add char to string if it still fits
else if (tuya_byte_counter < TUYA_BUFFER_SIZE -1) { // add char to string if it still fits
tuya_buffer[tuya_byte_counter++] = serial_in_byte;
tuya_cmd_checksum += serial_in_byte;
} else {
@ -361,7 +355,6 @@ void TuyaSerialInput(void)
}
}
bool TuyaButtonPressed(void)
{
if (!XdrvMailbox.index && ((PRESSED == XdrvMailbox.payload) && (NOT_PRESSED == lastbutton[XdrvMailbox.index]))) {
@ -372,27 +365,27 @@ bool TuyaButtonPressed(void)
return false; // Don't serve other buttons
}
void TuyaSetWifiLed(void){
uint8_t wifi_state = 0x02;
switch(WifiState()){
case WIFI_SMARTCONFIG:
wifi_state = 0x00;
break;
case WIFI_MANAGER:
case WIFI_WPSCONFIG:
wifi_state = 0x01;
break;
case WIFI_RESTART:
wifi_state = 0x03;
break;
}
void TuyaSetWifiLed(void)
{
uint8_t wifi_state = 0x02;
switch(WifiState()){
case WIFI_SMARTCONFIG:
wifi_state = 0x00;
break;
case WIFI_MANAGER:
case WIFI_WPSCONFIG:
wifi_state = 0x01;
break;
case WIFI_RESTART:
wifi_state = 0x03;
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);
TuyaSendCmd(TUYA_CMD_WIFI_STATE, &wifi_state, 1);
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
@ -419,11 +412,11 @@ bool Xdrv16(uint8_t function)
result = TuyaButtonPressed();
break;
case FUNC_EVERY_SECOND:
if(TuyaSerial && tuya_wifi_state!=WifiState()) { TuyaSetWifiLed(); }
if (TuyaSerial && tuya_wifi_state != WifiState()) { TuyaSetWifiLed(); }
tuya_heartbeat_timer++;
if (tuya_heartbeat_timer > 10) {
tuya_heartbeat_timer = 0;
TuyaSendCmd(TUYA_CMD_HEARTBEAT);
tuya_heartbeat_timer = 0;
TuyaSendCmd(TUYA_CMD_HEARTBEAT);
}
break;
case FUNC_SET_CHANNELS:

View File

@ -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,16 +73,16 @@ 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();
// Dimming acts odd below 10% - this mirrors the threshold set on the faceplate itself
light_state_dimmer = (light_state_dimmer < 10) ? 10 : light_state_dimmer;
snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR( "AT+UPDATE=\"sequence\":\"%d%03d\",\"switch\":\"%s\",\"bright\":%d"),
snprintf_P(ps16dz_tx_buffer, PS16DZ_BUFFER_SIZE, PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"switch\":\"%s\",\"bright\":%d"),
LocalTime(), millis()%1000, power?"on":"off", light_state_dimmer);
if (ps16dz_supports_color) {
@ -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);
}