diff --git a/RFLink/4_Display.cpp b/RFLink/4_Display.cpp index 3aa11c87..1b0cd6ed 100644 --- a/RFLink/4_Display.cpp +++ b/RFLink/4_Display.cpp @@ -6,6 +6,7 @@ // ************************************* // #include +#include "3_Serial.h" #include "4_Display.h" byte PKSequenceNumber = 0; // 1 byte packet counter @@ -335,6 +336,175 @@ void display_RGBW(unsigned int input) strcat(pbuffer, dbuffer); } +// --------------------- // +// get label shared func // +// --------------------- // + +char *ptr; +const char c_delim[2] = ";"; +char c_label[10]; + +boolean retrieve_Init10() +{ + // 10 + ptr = strtok(InputBuffer_Serial, c_delim); + if (ptr != NULL) + { + if (strncasecmp(ptr, "10", strlen("10")) != 0) + return false; + return true; + } + else + return false; +} + +boolean retrieve_Name(const char *c_Name) +{ + // Newkaku + ptr = strtok(NULL, c_delim); + if (ptr != NULL) + { + if (strncasecmp(ptr, c_Name, strlen(c_Name)) != 0) + return false; + return true; + } + else + return false; +} + +boolean retrieve_ID(unsigned long &ul_ID) +{ + // ID + char c_ID[10]; + + ptr = strtok(NULL, c_delim); + if (ptr != NULL) + { + strcpy(c_label, "ID="); + if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) + ptr += strlen(c_label); + + if (strlen(ptr) > 8) + return false; + + for (byte i = 0; i < strlen(ptr); i++) + if (!isxdigit(ptr[i])) + return false; + + strcpy(c_ID, ptr); + c_ID[8] = 0; + + ul_ID = strtoul(c_ID, NULL, HEX); + ul_ID &= 0x03FFFFFF; + + return true; + } + else + return false; +} + +boolean retrieve_Switch(byte &b_Switch) +{ + // Switch + char c_Switch[10]; + + ptr = strtok(NULL, c_delim); + if (ptr != NULL) + { + strcpy(c_label, "SWITCH="); + if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) + ptr += strlen(c_label); + + if (strlen(ptr) > 1) + return false; + + for (byte i = 0; i < strlen(ptr); i++) + if (!isxdigit(ptr[i])) + return false; + + strcpy(c_Switch, ptr); + + b_Switch = (byte)strtoul(c_Switch, NULL, HEX); + b_Switch--; // 1 to 16 -> 0 to 15 (displayed value is one more) + if (b_Switch > 0xF) + return false; // invalid address + + return true; + } + else + return false; +} + +boolean retrieve_Command(byte &b_Cmd, byte &b_Cmd2) +{ + // Command + char c_Cmd[10]; + + ptr = strtok(NULL, c_delim); + if (ptr != NULL) + { + strcpy(c_label, "SET_LEVEL="); + if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) + ptr += strlen(c_label); + + strcpy(c_label, "CMD="); + if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) + ptr += strlen(c_label); + + if (strlen(ptr) > 7) + return false; + + for (byte i = 0; i < strlen(ptr); i++) + if (!isalnum(ptr[i])) + return false; + + strcpy(c_Cmd, ptr); + + b_Cmd2 = str2cmd(c_Cmd); // Get ON/OFF etc. command + if (b_Cmd2 == false) // Not a valid command received? ON/OFF/ALLON/ALLOFF + b_Cmd2 = (byte)strtoul(c_Cmd, NULL, HEX); + // ON + switch (b_Cmd2) + { + case VALUE_ON: + case VALUE_ALLON: + b_Cmd |= B01; + break; + } + // Group + switch (b_Cmd2) + { + case VALUE_ALLON: + case VALUE_ALLOFF: + b_Cmd |= B10; + break; + } + // Dimmer + switch (b_Cmd2) + { + case VALUE_ON: + case VALUE_OFF: + case VALUE_ALLON: + case VALUE_ALLOFF: + b_Cmd2 = 0xFF; + break; + } + + return true; + } + else + return false; +} + +boolean retrieve_End() +{ + // End + ptr = strtok(NULL, c_delim); + if (ptr != NULL) + return false; + return true; +} + /*********************************************************************************************\ Convert string to command code \*********************************************************************************************/ @@ -367,13 +537,13 @@ int str2cmd(char *command) return false; } -void replacechar(char *str, char orig, char rep) -{ - char *ix = str; - int n = 0; - while ((ix = strchr(ix, orig)) != NULL) - { - *ix++ = rep; - n++; - } -} \ No newline at end of file +// void replacechar(char *str, char orig, char rep) +// { +// char *ix = str; +// int n = 0; +// while ((ix = strchr(ix, orig)) != NULL) +// { +// *ix++ = rep; +// n++; +// } +// } \ No newline at end of file diff --git a/RFLink/4_Display.h b/RFLink/4_Display.h index aaaf32cb..2ff51672 100644 --- a/RFLink/4_Display.h +++ b/RFLink/4_Display.h @@ -22,12 +22,27 @@ void display_IDn(unsigned int, byte); void display_IDc(const char *); void display_SWITCH(byte); void display_SWITCHc(const char *); -enum CMD_Group {CMD_Single, CMD_All}; -enum CMD_OnOff {CMD_Off, CMD_On, CMD_Bright, CMD_Dim, CMD_Unknown}; +enum CMD_Group +{ + CMD_Single, + CMD_All +}; +enum CMD_OnOff +{ + CMD_Off, + CMD_On, + CMD_Bright, + CMD_Dim, + CMD_Unknown +}; void display_CMD(boolean, byte); void display_SET_LEVEL(byte); void display_TEMP(unsigned int); -enum HUM_Type {HUM_HEX, HUM_BCD}; +enum HUM_Type +{ + HUM_HEX, + HUM_BCD +}; void display_HUM(byte, boolean); void display_BARO(unsigned int); void display_HSTATUS(byte); @@ -44,9 +59,17 @@ void display_WINDIR(unsigned int); void display_WINCHL(unsigned int); void display_WINTMP(unsigned int); void display_CHIME(unsigned int); -enum SMOKE_OnOff {SMOKE_Off, SMOKE_On}; +enum SMOKE_OnOff +{ + SMOKE_Off, + SMOKE_On +}; void display_SMOKEALERT(boolean); -enum PIR_OnOff {PIR_Off, PIR_On}; +enum PIR_OnOff +{ + PIR_Off, + PIR_On +}; void display_PIR(boolean); void display_CO2(unsigned int); void display_SOUND(unsigned int); @@ -58,20 +81,27 @@ void display_METER(unsigned int); void display_VOLT(unsigned int); void display_RGBW(unsigned int); -#define VALUE_PAIR 44 -#define VALUE_ALLOFF 55 -#define VALUE_OFF 74 -#define VALUE_ON 75 -#define VALUE_DIM 76 -#define VALUE_BRIGHT 77 -#define VALUE_UP 78 -#define VALUE_DOWN 79 -#define VALUE_STOP 80 -#define VALUE_CONFIRM 81 -#define VALUE_LIMIT 82 -#define VALUE_ALLON 141 +boolean retrieve_Init10(); +boolean retrieve_Name(const char *); +boolean retrieve_ID(unsigned long &); +boolean retrieve_Switch(byte &); +boolean retrieve_Command(byte &, byte &); +boolean retrieve_End(); + +#define VALUE_PAIR 44 +#define VALUE_ALLOFF 55 +#define VALUE_OFF 74 +#define VALUE_ON 75 +#define VALUE_DIM 76 +#define VALUE_BRIGHT 77 +#define VALUE_UP 78 +#define VALUE_DOWN 79 +#define VALUE_STOP 80 +#define VALUE_CONFIRM 81 +#define VALUE_LIMIT 82 +#define VALUE_ALLON 141 int str2cmd(char *); -void replacechar(char *, char, char); +// void replacechar(char *, char, char); #endif \ No newline at end of file diff --git a/RFLink/Plugins/Plugin_004.c b/RFLink/Plugins/Plugin_004.c index af43d8de..e0daf399 100644 --- a/RFLink/Plugins/Plugin_004.c +++ b/RFLink/Plugins/Plugin_004.c @@ -165,188 +165,45 @@ boolean PluginTX_004(byte function, char *string) //10;NewKaku;306070b;10;ON; //01234567890123456789012 - // Start message split - // Serial.print("*** Spliting message ***\n"); - - static const char c_delim[2] = ";"; - static char c_label[10]; - static char c_ID[10]; - static char c_Switch[10]; - static char c_Cmd[10]; + unsigned long bitstream = 0L; // 32 bits complete packet + unsigned long ID_bitstream = 0L; // 26 bits Address + byte Switch_bitstream = 0; // 4 bits Unit + byte Cmd_bitstream = 0; // 2 bits Command + byte Cmd_dimmer = 0; // 4 bits Alt Command - // 10 - char *ptr = strtok(InputBuffer_Serial, c_delim); - if (ptr != NULL) - { - strcpy(c_label, "10"); - if (strncasecmp(ptr, c_label, strlen(c_label)) != 0) - return false; - } - else + if (!retrieve_Init10()) return false; - - // Newkaku - ptr = strtok(NULL, c_delim); - if (ptr != NULL) - { - strcpy(c_label, "NEWKAKU"); - if (strncasecmp(ptr, c_label, strlen(c_label)) != 0) - return false; - } - else + if (!retrieve_Name("Newkaku")) return false; - - // ID - ptr = strtok(NULL, c_delim); - if (ptr != NULL) - { - strcpy(c_label, "ID="); - if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) - ptr += strlen(c_label); - - if (strlen(ptr) > 8) - return false; - - for (byte i = 0; i < strlen(ptr); i++) - if (!isxdigit(ptr[i])) - return false; - - strcpy(c_ID, ptr); - c_ID[8] = 0; - } - else + if (!retrieve_ID(ID_bitstream)) return false; - - // Switch - ptr = strtok(NULL, c_delim); - if (ptr != NULL) - { - strcpy(c_label, "SWITCH="); - if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) - ptr += strlen(c_label); - - if (strlen(ptr) > 1) - return false; - - for (byte i = 0; i < strlen(ptr); i++) - if (!isxdigit(ptr[i])) - return false; - - strcpy(c_Switch, ptr); - } - else + if (!retrieve_Switch(Switch_bitstream)) return false; - - // Command - ptr = strtok(NULL, c_delim); - if (ptr != NULL) - { - strcpy(c_label, "SET_LEVEL="); - if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) - ptr += strlen(c_label); - - strcpy(c_label, "CMD="); - if (strncasecmp(ptr, c_label, strlen(c_label)) == 0) - ptr += strlen(c_label); - - if (strlen(ptr) > 7) - return false; - - for (byte i = 0; i < strlen(ptr); i++) - if (!isalnum(ptr[i])) - return false; - - strcpy(c_Cmd, ptr); - } - else + if (!retrieve_Command(Cmd_bitstream, Cmd_dimmer)) return false; - - // End - ptr = strtok(NULL, c_delim); - if (ptr != NULL) + if (!retrieve_End()) return false; - // Summary message split - // Serial.print("*** Split OK ***\n"); - // Serial.println(c_ID); - // Serial.println(c_Switch); - // Serial.println(c_Cmd); - // --------------- Prepare bitstream ------------ // Dimming of groups is also possible but not supported yet! // when level=0 is it better to transmit just the off command ? // Serial.print("*** Creating bitstream ***\n"); - unsigned long bitstream = 0L; // 32 bits complete packet - unsigned long ID_bitstream = 0L; // 26 bits Address - ID_bitstream = strtoul(c_ID, NULL, HEX); - ID_bitstream &= 0x03FFFFFF; - // Serial.println("ID_bitstream"); - // Serial.println(ID_bitstream, HEX); bitstream = (ID_bitstream << 6); // 26 bits on top - - byte Switch_bitstream = 0; // 4 bits Unit - Switch_bitstream = (byte)strtoul(c_Switch, NULL, HEX); - Switch_bitstream--; // 1 to 16 -> 0 to 15 (displayed value is one more) - if (Switch_bitstream > 0xF) - return false; // invalid address - // Serial.println("Switch_bitstream"); - // Serial.println(Switch_bitstream, HEX); - bitstream |= Switch_bitstream; // Complete transmitted address - bitstream &= 0xFFFFFFCF; // Bit 4 and 5 are left for cmd - - byte Cmd_bitstream = 0x00; // 2 bits Command - byte cmd = 0; - cmd = str2cmd(c_Cmd); // Get ON/OFF etc. command - if (cmd == false) // Not a valid command received? ON/OFF/ALLON/ALLOFF - cmd = (byte)strtoul(c_Cmd, NULL, HEX); - // ON - switch (cmd) - { - case VALUE_ON: - case VALUE_ALLON: - Cmd_bitstream |= B01; - break; - } - // Group - switch (cmd) - { - case VALUE_ALLON: - case VALUE_ALLOFF: - Cmd_bitstream |= B10; - break; - } - // Dimmer - switch (cmd) - { - case VALUE_ON: - case VALUE_OFF: - case VALUE_ALLON: - case VALUE_ALLOFF: - cmd = 0xFF; - break; - } - // Serial.println("Cmd_bitstream"); - // Serial.println(Cmd_bitstream, HEX); - + // bitstream &= 0xFFFFFFCF; // Bit 4 and 5 are left for cmd bitstream |= (Cmd_bitstream << 4); - // Serial.println("Complete bitstream"); - // Serial.println(bitstream, HEX); - // Serial.println("cmd"); - // Serial.println(cmd, HEX); // bitstream now contains the AC/NewKAKU-bits that have to be transmitted // --------------- NEWKAKU SEND ------------ - AC_Send(bitstream, cmd); + AC_Send(bitstream, Cmd_dimmer); // -------------------------------------- return true; } - #endif // Plugin_TX_004