From f7992ed73ce8788088a1d01c77a256830c18e171 Mon Sep 17 00:00:00 2001 From: Rene Hexel Date: Tue, 2 Jan 2018 06:27:38 +1000 Subject: [PATCH] Add RF_RAW_SUPPORT to rfbridge binaries --- code/espurna/config/general.h | 1 + code/espurna/rfbridge.ino | 111 +++++++++++++++++++++++++++------- code/platformio.ini | 4 +- 3 files changed, 92 insertions(+), 24 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 4399fc47..54beb4c3 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -467,6 +467,7 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_TOPIC_RFOUT "rfout" #define MQTT_TOPIC_RFIN "rfin" #define MQTT_TOPIC_RFLEARN "rflearn" +#define MQTT_TOPIC_RFRAW "rfraw" // Light module #define MQTT_TOPIC_CHANNEL "channel" diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index dbd60e8a..9d2b4efa 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -15,15 +15,28 @@ Copyright (C) 2017 by Xose PĂ©rez // DEFINITIONS // ----------------------------------------------------------------------------- -#define RF_MESSAGE_SIZE 9 -#define RF_CODE_START 0xAA -#define RF_CODE_ACK 0xA0 -#define RF_CODE_LEARN 0xA1 -#define RF_CODE_LEARN_KO 0xA2 -#define RF_CODE_LEARN_OK 0xA3 -#define RF_CODE_RFIN 0xA4 -#define RF_CODE_RFOUT 0xA5 -#define RF_CODE_STOP 0x55 +#define RF_MESSAGE_SIZE 9 +#ifndef RF_RAW_SUPPORT +# define RF_MAX_MESSAGE_SIZE RF_MESSAGE_SIZE +#else +# undef RF_MAX_MESSAGE_SIZE +# define RF_MAX_MESSAGE_SIZE 16 // (112+4) +#endif +#define RF_CODE_START 0xAA +#define RF_CODE_ACK 0xA0 +#define RF_CODE_LEARN 0xA1 +#define RF_CODE_LEARN_KO 0xA2 +#define RF_CODE_LEARN_OK 0xA3 +#define RF_CODE_RFIN 0xA4 +#define RF_CODE_RFOUT 0xA5 +#define RF_CODE_SNIFFING_ON 0xA6 +#define RF_CODE_SNIFFING_OFF 0xA7 +#define RF_CODE_RFOUT_NEW 0xA8 +#define RF_CODE_LEARN_NEW 0xA9 +#define RF_CODE_LEARN_KO_NEW 0xAA +#define RF_CODE_LEARN_OK_NEW 0xAB +#define RF_CODE_RFOUT_BUCKET 0xB0 +#define RF_CODE_STOP 0x55 // ----------------------------------------------------------------------------- // GLOBALS TO THE MODULE @@ -36,8 +49,11 @@ bool _learnStatus = true; bool _rfbin = false; typedef struct { - byte code[RF_MESSAGE_SIZE]; + byte code[RF_MAX_MESSAGE_SIZE]; byte times; +#ifdef RF_RAW_SUPPORT + byte length; +#endif } rfb_message_t; std::vector _rfb_message_queue; Ticker _rfbTicker; @@ -94,13 +110,17 @@ void _rfbLearn() { } +void _rfbSendRaw(const byte *message, const unsigned char n = RF_MESSAGE_SIZE) { + for (unsigned char j=0; j 0) { @@ -140,11 +175,29 @@ void _rfbSend(byte * code, int times) { rfb_message_t message; memcpy(message.code, code, RF_MESSAGE_SIZE); message.times = times; +#ifdef RF_RAW_SUPPORT + message.length = RF_MESSAGE_SIZE; +#endif _rfb_message_queue.push_back(message); _rfbSend(); } +#ifdef RF_RAW_SUPPORT +void _rfbSendRawRepeated(byte *code, int length, int times) { + char buffer[RF_MESSAGE_SIZE*2]; + _rfbToChar(code, buffer); + DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending raw MESSAGE '%s' %d time(s)\n"), buffer, times); + + rfb_message_t message; + memcpy(message.code, code, length); + message.times = -times; + message.length = length; + _rfb_message_queue.push_back(message); + _rfbSend(); +} +#endif + bool _rfbMatch(char * code, unsigned char& relayID, unsigned char& value) { if (strlen(code) != 18) return false; @@ -279,14 +332,16 @@ bool _rfbSameOnOff(unsigned char id) { /* From an hexa char array ("A220EE...") to a byte array (half the size) */ -bool _rfbToArray(const char * in, byte * out) { - if (strlen(in) != RF_MESSAGE_SIZE * 2) return false; - char tmp[3] = {0}; - for (unsigned char p = 0; p RF_MAX_MESSAGE_SIZE*2 || (length > 0 && n != length)) return 0; + char tmp[3] = {0,0,0}; + n /= 2; + for (unsigned char p = 0; p 0 && (isRFRaw || len != RF_MESSAGE_SIZE)) { + _rfbSendRawRepeated(message, len, times); + } else { +#else if (_rfbToArray(tok, message)) { - tok = strtok(NULL, ","); - byte times = (tok != NULL) ? atoi(tok) : 1; +#endif _rfbSend(message, times); } diff --git a/code/platformio.ini b/code/platformio.ini index 0387abfe..a50af3c8 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -506,7 +506,7 @@ board = esp01_1m board_flash_mode = dout lib_deps = ${common.lib_deps} lib_ignore = ${common.lib_ignore} -build_flags = ${common.build_flags_1m} -DITEAD_SONOFF_RFBRIDGE +build_flags = ${common.build_flags_1m} -DITEAD_SONOFF_RFBRIDGE -DRF_RAW_SUPPORT monitor_baud = 19200 [env:itead-sonoff-rfbridge-ota] @@ -516,7 +516,7 @@ board = esp01_1m board_flash_mode = dout lib_deps = ${common.lib_deps} lib_ignore = ${common.lib_ignore} -build_flags = ${common.build_flags_1m} -DITEAD_SONOFF_RFBRIDGE +build_flags = ${common.build_flags_1m} -DITEAD_SONOFF_RFBRIDGE -DRF_RAW_SUPPORT upload_speed = 115200 upload_port = "192.168.4.1" upload_flags = --auth=Algernon1 --port 8266