|
@ -16,12 +16,7 @@ Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
#define RF_MESSAGE_SIZE 9
|
|
|
#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_MAX_MESSAGE_SIZE (112+4)
|
|
|
#define RF_CODE_START 0xAA
|
|
|
#define RF_CODE_START 0xAA
|
|
|
#define RF_CODE_ACK 0xA0
|
|
|
#define RF_CODE_ACK 0xA0
|
|
|
#define RF_CODE_LEARN 0xA1
|
|
|
#define RF_CODE_LEARN 0xA1
|
|
@ -49,11 +44,8 @@ bool _learnStatus = true; |
|
|
bool _rfbin = false; |
|
|
bool _rfbin = false; |
|
|
|
|
|
|
|
|
typedef struct { |
|
|
typedef struct { |
|
|
byte code[RF_MAX_MESSAGE_SIZE]; |
|
|
|
|
|
|
|
|
byte code[RF_MESSAGE_SIZE]; |
|
|
byte times; |
|
|
byte times; |
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
|
|
|
byte length; |
|
|
|
|
|
#endif
|
|
|
|
|
|
} rfb_message_t; |
|
|
} rfb_message_t; |
|
|
std::vector<rfb_message_t> _rfb_message_queue; |
|
|
std::vector<rfb_message_t> _rfb_message_queue; |
|
|
Ticker _rfbTicker; |
|
|
Ticker _rfbTicker; |
|
@ -136,15 +128,6 @@ void _rfbSend() { |
|
|
_rfb_message_queue.erase(_rfb_message_queue.begin()); |
|
|
_rfb_message_queue.erase(_rfb_message_queue.begin()); |
|
|
|
|
|
|
|
|
// Send the message
|
|
|
// Send the message
|
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
|
|
|
bool sendRaw = message.times < 0; |
|
|
|
|
|
int length = sendRaw ? -message.times : message.times; |
|
|
|
|
|
if (sendRaw) { |
|
|
|
|
|
_rfbSendRaw(message.code, message.length); |
|
|
|
|
|
Serial.flush(); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
#endif
|
|
|
|
|
|
_rfbSend(message.code); |
|
|
_rfbSend(message.code); |
|
|
|
|
|
|
|
|
// If it should be further sent, push it to the stack again
|
|
|
// If it should be further sent, push it to the stack again
|
|
@ -152,12 +135,6 @@ void _rfbSend() { |
|
|
message.times = message.times - 1; |
|
|
message.times = message.times - 1; |
|
|
_rfb_message_queue.push_back(message); |
|
|
_rfb_message_queue.push_back(message); |
|
|
} |
|
|
} |
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
|
|
|
else if (message.times < -1) { |
|
|
|
|
|
message.times = message.times + 1; |
|
|
|
|
|
_rfb_message_queue.push_back(message); |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// if there are still messages in the queue...
|
|
|
// if there are still messages in the queue...
|
|
|
if (_rfb_message_queue.size() > 0) { |
|
|
if (_rfb_message_queue.size() > 0) { |
|
@ -175,26 +152,18 @@ void _rfbSend(byte * code, int times) { |
|
|
rfb_message_t message; |
|
|
rfb_message_t message; |
|
|
memcpy(message.code, code, RF_MESSAGE_SIZE); |
|
|
memcpy(message.code, code, RF_MESSAGE_SIZE); |
|
|
message.times = times; |
|
|
message.times = times; |
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
|
|
|
message.length = RF_MESSAGE_SIZE; |
|
|
|
|
|
#endif
|
|
|
|
|
|
_rfb_message_queue.push_back(message); |
|
|
_rfb_message_queue.push_back(message); |
|
|
_rfbSend(); |
|
|
_rfbSend(); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
void _rfbSendRawRepeated(byte *code, int length, int times) { |
|
|
|
|
|
char buffer[RF_MESSAGE_SIZE*2]; |
|
|
|
|
|
|
|
|
void _rfbSendRawOnce(byte *code, int length) { |
|
|
|
|
|
char buffer[length*2]; |
|
|
_rfbToChar(code, buffer); |
|
|
_rfbToChar(code, buffer); |
|
|
DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending raw MESSAGE '%s' %d time(s)\n"), buffer, times); |
|
|
|
|
|
|
|
|
DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending raw MESSAGE '%s'\n"), buffer); |
|
|
|
|
|
|
|
|
rfb_message_t message; |
|
|
|
|
|
memcpy(message.code, code, length); |
|
|
|
|
|
message.times = -times; |
|
|
|
|
|
message.length = length; |
|
|
|
|
|
_rfb_message_queue.push_back(message); |
|
|
|
|
|
_rfbSend(); |
|
|
|
|
|
|
|
|
_rfbSendRaw(code, length); |
|
|
} |
|
|
} |
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
@ -386,12 +355,13 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa |
|
|
bool isRFOut = t.equals(MQTT_TOPIC_RFOUT); |
|
|
bool isRFOut = t.equals(MQTT_TOPIC_RFOUT); |
|
|
bool isRFRaw = !isRFOut && t.equals(MQTT_TOPIC_RFRAW); |
|
|
bool isRFRaw = !isRFOut && t.equals(MQTT_TOPIC_RFRAW); |
|
|
if (isRFOut || isRFRaw) { |
|
|
if (isRFOut || isRFRaw) { |
|
|
|
|
|
byte message[RF_MAX_MESSAGE_SIZE]; |
|
|
#else
|
|
|
#else
|
|
|
if (t.equals(MQTT_TOPIC_RFOUT)) { |
|
|
if (t.equals(MQTT_TOPIC_RFOUT)) { |
|
|
|
|
|
byte message[RF_MESSAGE_SIZE]; |
|
|
#endif
|
|
|
#endif
|
|
|
// The payload may be a code in HEX format ([0-9A-Z]{18}) or
|
|
|
// The payload may be a code in HEX format ([0-9A-Z]{18}) or
|
|
|
// the code comma the number of times to transmit it.
|
|
|
// the code comma the number of times to transmit it.
|
|
|
byte message[RF_MAX_MESSAGE_SIZE]; |
|
|
|
|
|
char * tok = strtok((char *) payload, ","); |
|
|
char * tok = strtok((char *) payload, ","); |
|
|
|
|
|
|
|
|
// Check if a switch is linked to that message
|
|
|
// Check if a switch is linked to that message
|
|
@ -406,16 +376,16 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const char *tok2 = strtok(NULL, ","); |
|
|
|
|
|
byte times = (t != NULL) ? atoi(tok2) : 1; |
|
|
|
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
#ifdef RF_RAW_SUPPORT
|
|
|
int len = _rfbToArray(tok, message, 0); |
|
|
int len = _rfbToArray(tok, message, 0); |
|
|
if (len > 0 && (isRFRaw || len != RF_MESSAGE_SIZE)) { |
|
|
if (len > 0 && (isRFRaw || len != RF_MESSAGE_SIZE)) { |
|
|
_rfbSendRawRepeated(message, len, times); |
|
|
|
|
|
|
|
|
_rfbSendRawOnce(message, len); |
|
|
} else { |
|
|
} else { |
|
|
#else
|
|
|
#else
|
|
|
if (_rfbToArray(tok, message)) { |
|
|
if (_rfbToArray(tok, message)) { |
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
tok = strtok(NULL, ","); |
|
|
|
|
|
byte times = (tok != NULL) ? atoi(tok) : 1; |
|
|
_rfbSend(message, times); |
|
|
_rfbSend(message, times); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|