@ -1,17 +1,17 @@
/*
/*
RF MODULE
RF BRIDGE MODULE
Copyright ( C ) 2016 - 2019 by Xose Pérez < xose dot perez at gmail dot com >
Copyright ( C ) 2016 - 2019 by Xose Pérez < xose dot perez at gmail dot com >
*/
*/
# if defined(ITEAD_SONOFF_RFBRIDGE) || RF_SUPPORT
# if RF_SUPPORT
# include <queue>
# include <queue>
# include <Ticker.h>
# include <Ticker.h>
# if RFB_DIRECT || RF_SUPPORT
# if RFB_DIRECT
# include <RCSwitch.h>
# include <RCSwitch.h>
# endif
# endif
@ -50,24 +50,25 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
unsigned char _uartbuf [ RF_MESSAGE_SIZE + 3 ] = { 0 } ;
unsigned char _uartbuf [ RF_MESSAGE_SIZE + 3 ] = { 0 } ;
unsigned char _uartpos = 0 ;
unsigned char _uartpos = 0 ;
unsigned char _learnId = 0 ;
unsigned char _learnId = 0 ;
bool _learnStatus = true ;
bool _learnStatus = true ;
bool _rfbin = false ;
bool _rfbin = false ;
# if not RF_SUPPORT
typedef struct {
typedef struct {
byte code [ RF_MESSAGE_SIZE ] ;
byte code [ RF_MESSAGE_SIZE ] ;
byte times ;
byte times ;
} rfb_message_t ;
} rfb_message_t ;
static std : : queue < rfb_message_t > _rfb_message_queue ;
static std : : queue < rfb_message_t > _rfb_message_queue ;
Ticker _rfb_ticker ;
bool _rfb_ticker_active = false ;
# endif
# if RFB_DIRECT || RF_SUPPORT
# if RFB_DIRECT
RCSwitch * _rfModem ;
RCSwitch * _rfModem ;
bool _learning = false ;
bool _learning = false ;
# endif
# endif
bool _rfb_receive = false ;
bool _rfb_transmit = false ;
unsigned char _rfb_repeat = RF_SEND_TIMES ;
# if WEB_SUPPORT
# if WEB_SUPPORT
Ticker _rfb_sendcodes ;
Ticker _rfb_sendcodes ;
# endif
# endif
@ -119,9 +120,12 @@ void _rfbWebSocketSendCodes() {
void _rfbWebSocketOnSend ( JsonObject & root ) {
void _rfbWebSocketOnSend ( JsonObject & root ) {
root [ " rfbVisible " ] = 1 ;
root [ " rfbVisible " ] = 1 ;
root [ " rfbRepeat " ] = getSetting ( " rfbRepeat " , RF_SEND_TIMES ) . toInt ( ) ;
root [ " rfbCount " ] = relayCount ( ) ;
root [ " rfbCount " ] = relayCount ( ) ;
# if RF_RAW_SUPPORT
root [ " rfbrawVisible " ] = 1 ;
# if RFB_DIRECT
root [ " rfbdirectVisible " ] = 1 ;
root [ " rfbRX " ] = getSetting ( " rfbRX " , RFB_RX_PIN ) . toInt ( ) ;
root [ " rfbTX " ] = getSetting ( " rfbTX " , RFB_TX_PIN ) . toInt ( ) ;
# endif
# endif
_rfb_sendcodes . once_ms ( 1000 , _rfbWebSocketSendCodes ) ;
_rfb_sendcodes . once_ms ( 1000 , _rfbWebSocketSendCodes ) ;
}
}
@ -132,41 +136,11 @@ void _rfbWebSocketOnAction(uint32_t client_id, const char * action, JsonObject&
if ( strcmp ( action , " rfbsend " ) = = 0 ) rfbStore ( data [ " id " ] , data [ " status " ] , data [ " data " ] . as < const char * > ( ) ) ;
if ( strcmp ( action , " rfbsend " ) = = 0 ) rfbStore ( data [ " id " ] , data [ " status " ] , data [ " data " ] . as < const char * > ( ) ) ;
}
}
# endif // WEB_SUPPORT
void _rfbAck ( ) {
# if (not RFB_DIRECT) && (not RF_SUPPORT)
DEBUG_MSG_P ( PSTR ( " [RF] Sending ACK \n " ) ) ;
Serial . println ( ) ;
Serial . write ( RF_CODE_START ) ;
Serial . write ( RF_CODE_ACK ) ;
Serial . write ( RF_CODE_STOP ) ;
Serial . flush ( ) ;
Serial . println ( ) ;
# endif
bool _rfbWebSocketOnReceive ( const char * key , JsonVariant & value ) {
return ( strncmp ( key , " rfb " , 3 ) = = 0 ) ;
}
}
void _rfbLearn ( ) {
# if RFB_DIRECT || RF_SUPPORT
DEBUG_MSG_P ( PSTR ( " [RF] Entering LEARN mode \n " ) ) ;
_learning = true ;
# else
DEBUG_MSG_P ( PSTR ( " [RF] Sending LEARN \n " ) ) ;
Serial . println ( ) ;
Serial . write ( RF_CODE_START ) ;
Serial . write ( RF_CODE_LEARN ) ;
Serial . write ( RF_CODE_STOP ) ;
Serial . flush ( ) ;
Serial . println ( ) ;
# endif
# if WEB_SUPPORT
char buffer [ 100 ] ;
snprintf_P ( buffer , sizeof ( buffer ) , PSTR ( " { \" action \" : \" rfbLearn \" , \" data \" :{ \" id \" : %d, \" status \" : %d}} " ) , _learnId , _learnStatus ? 1 : 0 ) ;
wsSend ( buffer ) ;
# endif
}
# endif // WEB_SUPPORT
/*
/*
From an hexa char array ( " A220EE... " ) to a byte array ( half the size )
From an hexa char array ( " A220EE... " ) to a byte array ( half the size )
@ -183,70 +157,43 @@ static int _rfbToArray(const char * in, byte * out, int length = RF_MESSAGE_SIZE
return n ;
return n ;
}
}
# if not RF_SUPPORT
void _rfbSendRaw ( const byte * message , const unsigned char n = RF_MESSAGE_SIZE ) {
void _rfbSendRaw ( const byte * message , const unsigned char n = RF_MESSAGE_SIZE ) {
for ( unsigned char j = 0 ; j < n ; j + + ) {
for ( unsigned char j = 0 ; j < n ; j + + ) {
Serial . write ( message [ j ] ) ;
Serial . write ( message [ j ] ) ;
}
}
}
}
void _rfbSend ( byte * message ) {
# if RFB_DIRECT
unsigned int protocol = message [ 1 ] ;
unsigned int timing =
( message [ 2 ] < < 8 ) |
( message [ 3 ] < < 0 ) ;
unsigned int bitlength = message [ 4 ] ;
unsigned long rf_code =
( message [ 5 ] < < 24 ) |
( message [ 6 ] < < 16 ) |
( message [ 7 ] < < 8 ) |
( message [ 8 ] < < 0 ) ;
_rfModem - > setProtocol ( protocol ) ;
if ( timing > 0 ) {
_rfModem - > setPulseLength ( timing ) ;
}
_rfModem - > send ( rf_code , bitlength ) ;
_rfModem - > resetAvailable ( ) ;
# else
Serial . println ( ) ;
Serial . write ( RF_CODE_START ) ;
Serial . write ( RF_CODE_RFOUT ) ;
_rfbSendRaw ( message ) ;
Serial . write ( RF_CODE_STOP ) ;
Serial . flush ( ) ;
Serial . println ( ) ;
# endif
}
void _rfbSend ( ) {
void _rfbSend ( ) {
if ( ! _rfb_transmit ) return ;
// Check if there is something in the queue
// Check if there is something in the queue
if ( _rfb_message_queue . empty ( ) ) return ;
if ( _rfb_message_queue . empty ( ) ) return ;
// Pop the first element
static unsigned long last = 0 ;
if ( millis ( ) - last < RF_SEND_DELAY ) return ;
last = millis ( ) ;
// Pop the first message and send it
rfb_message_t message = _rfb_message_queue . front ( ) ;
rfb_message_t message = _rfb_message_queue . front ( ) ;
_rfb_message_queue . pop ( ) ;
_rfb_message_queue . pop ( ) ;
// Send the message
_rfbSend ( message . code ) ;
_rfbSend ( message . code ) ;
// If it should be further sent, push it to the stack again
// Push it to the stack again if we need to send it more than once
if ( message . times > 1 ) {
if ( message . times > 1 ) {
message . times = message . times - 1 ;
message . times = message . times - 1 ;
_rfb_message_queue . push ( message ) ;
_rfb_message_queue . push ( message ) ;
}
}
// if there are still messages in the queue...
if ( _rfb_message_queue . empty ( ) ) {
_rfb_ticker . detach ( ) ;
_rfb_ticker_active = false ;
}
yield ( ) ;
}
}
void _rfbSend ( byte * code , unsigned char times ) {
void _rfbSend ( byte * code , unsigned char times ) {
if ( ! _rfb_transmit ) return ;
// rc-switch will repeat on its own
# if RFB_DIRECT
# if RFB_DIRECT
times = 1 ;
times = 1 ;
# endif
# endif
@ -260,18 +207,8 @@ void _rfbSend(byte * code, unsigned char times) {
message . times = times ;
message . times = times ;
_rfb_message_queue . push ( message ) ;
_rfb_message_queue . push ( message ) ;
// Enable the ticker if not running
if ( ! _rfb_ticker_active ) {
_rfb_ticker_active = true ;
_rfb_ticker . attach_ms ( RF_SEND_DELAY , _rfbSend ) ;
}
}
}
# endif // not RF_SUPPORT
# if RF_RAW_SUPPORT
void _rfbSendRawOnce ( byte * code , unsigned char length ) {
void _rfbSendRawOnce ( byte * code , unsigned char length ) {
char buffer [ length * 2 ] ;
char buffer [ length * 2 ] ;
_rfbToChar ( code , buffer , length ) ;
_rfbToChar ( code , buffer , length ) ;
@ -279,8 +216,6 @@ void _rfbSendRawOnce(byte *code, unsigned char length) {
_rfbSendRaw ( code , length ) ;
_rfbSendRaw ( code , length ) ;
}
}
# endif // RF_RAW_SUPPORT
bool _rfbMatch ( char * code , unsigned char & relayID , unsigned char & value , char * buffer = NULL ) {
bool _rfbMatch ( char * code , unsigned char & relayID , unsigned char & value , char * buffer = NULL ) {
if ( strlen ( code ) ! = 18 ) return false ;
if ( strlen ( code ) ! = 18 ) return false ;
@ -386,80 +321,6 @@ void _rfbDecode() {
}
}
void _rfbReceive ( ) {
# if RFB_DIRECT || RF_SUPPORT
static long learn_start = 0 ;
if ( ! _learning & & learn_start ) {
learn_start = 0 ;
}
if ( _learning ) {
if ( ! learn_start ) {
DEBUG_MSG_P ( PSTR ( " [RF] Arming learn timeout \n " ) ) ;
learn_start = millis ( ) ;
}
if ( learn_start > 0 & & millis ( ) - learn_start > RF_LEARN_TIMEOUT ) {
DEBUG_MSG_P ( PSTR ( " [RF] Learn timeout triggered \n " ) ) ;
memset ( _uartbuf , 0 , sizeof ( _uartbuf ) ) ;
_uartbuf [ 0 ] = RF_CODE_LEARN_KO ;
_rfbDecode ( ) ;
_learning = false ;
}
}
if ( _rfModem - > available ( ) ) {
static unsigned long last = 0 ;
if ( millis ( ) - last > RF_DEBOUNCE ) {
last = millis ( ) ;
unsigned long rf_code = _rfModem - > getReceivedValue ( ) ;
if ( rf_code > 0 ) {
DEBUG_MSG_P ( PSTR ( " [RF] Received code: %08X \n " ) , rf_code ) ;
unsigned int timing = _rfModem - > getReceivedDelay ( ) ;
memset ( _uartbuf , 0 , sizeof ( _uartbuf ) ) ;
unsigned char * msgbuf = _uartbuf + 1 ;
_uartbuf [ 0 ] = _learning ? RF_CODE_LEARN_OK : RF_CODE_RFIN ;
msgbuf [ 0 ] = 0xC0 ;
msgbuf [ 1 ] = _rfModem - > getReceivedProtocol ( ) ;
msgbuf [ 2 ] = timing > > 8 ;
msgbuf [ 3 ] = timing > > 0 ;
msgbuf [ 4 ] = _rfModem - > getReceivedBitlength ( ) ;
msgbuf [ 5 ] = rf_code > > 24 ;
msgbuf [ 6 ] = rf_code > > 16 ;
msgbuf [ 7 ] = rf_code > > 8 ;
msgbuf [ 8 ] = rf_code > > 0 ;
_rfbDecode ( ) ;
_learning = false ;
}
}
_rfModem - > resetAvailable ( ) ;
}
# else
static bool receiving = false ;
while ( Serial . available ( ) ) {
yield ( ) ;
byte c = Serial . read ( ) ;
//DEBUG_MSG_P(PSTR("[RF] Received 0x%02X\n"), c);
if ( receiving ) {
if ( c = = RF_CODE_STOP & & ( _uartpos = = 1 | | _uartpos = = RF_MESSAGE_SIZE + 1 ) ) {
_rfbDecode ( ) ;
receiving = false ;
} else if ( _uartpos < = RF_MESSAGE_SIZE ) {
_uartbuf [ _uartpos + + ] = c ;
} else {
// wrong message, should have received a RF_CODE_STOP
receiving = false ;
}
} else if ( c = = RF_CODE_START ) {
_uartpos = 0 ;
receiving = true ;
}
}
# endif
}
bool _rfbCompare ( const char * code1 , const char * code2 ) {
bool _rfbCompare ( const char * code1 , const char * code2 ) {
return strcmp ( & code1 [ 12 ] , & code2 [ 12 ] ) = = 0 ;
return strcmp ( & code1 [ 12 ] , & code2 [ 12 ] ) = = 0 ;
}
}
@ -468,6 +329,14 @@ bool _rfbSameOnOff(unsigned char id) {
return _rfbCompare ( rfbRetrieve ( id , true ) . c_str ( ) , rfbRetrieve ( id , false ) . c_str ( ) ) ;
return _rfbCompare ( rfbRetrieve ( id , true ) . c_str ( ) , rfbRetrieve ( id , false ) . c_str ( ) ) ;
}
}
void _rfbParseRaw ( char * raw ) {
byte message [ RF_MAX_MESSAGE_SIZE ] ;
int len = _rfbToArray ( raw , message , 0 ) ;
if ( len > 0 ) {
_rfbSendRawOnce ( message , len ) ;
}
}
void _rfbParseCode ( char * code ) {
void _rfbParseCode ( char * code ) {
// 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
@ -486,28 +355,179 @@ void _rfbParseCode(char * code) {
return ;
return ;
}
}
# if RF_RAW_SUPPORT
byte message [ RF_MESSAGE_SIZE ] ;
int len = _rfbToArray ( tok , message , 0 ) ;
if ( len ) {
tok = strtok ( NULL , " , " ) ;
byte times = ( tok ! = NULL ) ? atoi ( tok ) : 1 ;
_rfbSend ( message , times ) ;
}
byte message [ RF_MAX_MESSAGE_SIZE ] ;
int len = _rfbToArray ( tok , message , 0 ) ;
if ( ( len > 0 ) & & ( isRFRaw | | len ! = RF_MESSAGE_SIZE ) ) {
_rfbSendRawOnce ( message , len ) ;
} else {
tok = strtok ( NULL , " , " ) ;
byte times = ( tok ! = NULL ) ? atoi ( tok ) : 1 ;
_rfbSend ( message , times ) ;
}
//
// RF handler implementations
//
# if !RFB_DIRECT // Default for ITEAD SONOFF RFBRIDGE
void _rfbAck ( ) {
DEBUG_MSG_P ( PSTR ( " [RF] Sending ACK \n " ) ) ;
Serial . println ( ) ;
Serial . write ( RF_CODE_START ) ;
Serial . write ( RF_CODE_ACK ) ;
Serial . write ( RF_CODE_STOP ) ;
Serial . flush ( ) ;
Serial . println ( ) ;
}
void _rfbLearnImpl ( ) {
DEBUG_MSG_P ( PSTR ( " [RF] Sending LEARN \n " ) ) ;
Serial . println ( ) ;
Serial . write ( RF_CODE_START ) ;
Serial . write ( RF_CODE_LEARN ) ;
Serial . write ( RF_CODE_STOP ) ;
Serial . flush ( ) ;
Serial . println ( ) ;
}
void _rfbSend ( byte * message ) {
Serial . println ( ) ;
Serial . write ( RF_CODE_START ) ;
Serial . write ( RF_CODE_RFOUT ) ;
_rfbSendRaw ( message ) ;
Serial . write ( RF_CODE_STOP ) ;
Serial . flush ( ) ;
Serial . println ( ) ;
}
void _rfbReceive ( ) {
static bool receiving = false ;
while ( Serial . available ( ) ) {
yield ( ) ;
byte c = Serial . read ( ) ;
//DEBUG_MSG_P(PSTR("[RF] Received 0x%02X\n"), c);
if ( receiving ) {
if ( c = = RF_CODE_STOP & & ( _uartpos = = 1 | | _uartpos = = RF_MESSAGE_SIZE + 1 ) ) {
_rfbDecode ( ) ;
receiving = false ;
} else if ( _uartpos < = RF_MESSAGE_SIZE ) {
_uartbuf [ _uartpos + + ] = c ;
} else {
// wrong message, should have received a RF_CODE_STOP
receiving = false ;
}
} else if ( c = = RF_CODE_START ) {
_uartpos = 0 ;
receiving = true ;
}
}
# else // RF_RAW_SUPPORT
}
byte message [ RF_MESSAGE_SIZE ] ;
if ( _rfbToArray ( tok , message ) ) {
tok = strtok ( NULL , " , " ) ;
byte times = ( tok ! = NULL ) ? atoi ( tok ) : 1 ;
_rfbSend ( message , times ) ;
}
# else // RFB_DIRECT
void _rfbAck ( ) { }
void _rfbLearnImpl ( ) {
DEBUG_MSG_P ( PSTR ( " [RF] Entering LEARN mode \n " ) ) ;
_learning = true ;
}
void _rfbSend ( byte * message ) {
if ( ! _rfb_transmit ) return ;
unsigned int protocol = message [ 1 ] ;
unsigned int timing =
( message [ 2 ] < < 8 ) |
( message [ 3 ] < < 0 ) ;
unsigned int bitlength = message [ 4 ] ;
unsigned long rf_code =
( message [ 5 ] < < 24 ) |
( message [ 6 ] < < 16 ) |
( message [ 7 ] < < 8 ) |
( message [ 8 ] < < 0 ) ;
_rfModem - > setProtocol ( protocol ) ;
if ( timing > 0 ) {
_rfModem - > setPulseLength ( timing ) ;
}
_rfModem - > send ( rf_code , bitlength ) ;
_rfModem - > resetAvailable ( ) ;
}
void _rfbReceive ( ) {
if ( ! _rfb_receive ) return ;
static long learn_start = 0 ;
if ( ! _learning & & learn_start ) {
learn_start = 0 ;
}
if ( _learning ) {
if ( ! learn_start ) {
DEBUG_MSG_P ( PSTR ( " [RF] Arming learn timeout \n " ) ) ;
learn_start = millis ( ) ;
}
if ( learn_start > 0 & & millis ( ) - learn_start > RF_LEARN_TIMEOUT ) {
DEBUG_MSG_P ( PSTR ( " [RF] Learn timeout triggered \n " ) ) ;
memset ( _uartbuf , 0 , sizeof ( _uartbuf ) ) ;
_uartbuf [ 0 ] = RF_CODE_LEARN_KO ;
_rfbDecode ( ) ;
_learning = false ;
}
}
if ( _rfModem - > available ( ) ) {
static unsigned long last = 0 ;
if ( millis ( ) - last > RF_DEBOUNCE ) {
last = millis ( ) ;
unsigned long rf_code = _rfModem - > getReceivedValue ( ) ;
if ( rf_code > 0 ) {
DEBUG_MSG_P ( PSTR ( " [RF] Received code: %08X \n " ) , rf_code ) ;
unsigned int timing = _rfModem - > getReceivedDelay ( ) ;
memset ( _uartbuf , 0 , sizeof ( _uartbuf ) ) ;
unsigned char * msgbuf = _uartbuf + 1 ;
_uartbuf [ 0 ] = _learning ? RF_CODE_LEARN_OK : RF_CODE_RFIN ;
msgbuf [ 0 ] = 0xC0 ;
msgbuf [ 1 ] = _rfModem - > getReceivedProtocol ( ) ;
msgbuf [ 2 ] = timing > > 8 ;
msgbuf [ 3 ] = timing > > 0 ;
msgbuf [ 4 ] = _rfModem - > getReceivedBitlength ( ) ;
msgbuf [ 5 ] = rf_code > > 24 ;
msgbuf [ 6 ] = rf_code > > 16 ;
msgbuf [ 7 ] = rf_code > > 8 ;
msgbuf [ 8 ] = rf_code > > 0 ;
_rfbDecode ( ) ;
_learning = false ;
}
}
}
_rfModem - > resetAvailable ( ) ;
}
yield ( ) ;
}
# endif // RFB_DIRECT
void _rfbLearn ( ) {
_rfbLearnImpl ( ) ;
# if WEB_SUPPORT
char buffer [ 100 ] ;
snprintf_P ( buffer , sizeof ( buffer ) , PSTR ( " { \" action \" : \" rfbLearn \" , \" data \" :{ \" id \" : %d, \" status \" : %d}} " ) , _learnId , _learnStatus ? 1 : 0 ) ;
wsSend ( buffer ) ;
# endif
# endif // RF_RAW_SUPPORT
}
}
# if MQTT_SUPPORT
# if MQTT_SUPPORT
@ -515,15 +535,19 @@ void _rfbParseCode(char * code) {
void _rfbMqttCallback ( unsigned int type , const char * topic , const char * payload ) {
void _rfbMqttCallback ( unsigned int type , const char * topic , const char * payload ) {
if ( type = = MQTT_CONNECT_EVENT ) {
if ( type = = MQTT_CONNECT_EVENT ) {
char buffer [ strlen ( MQTT_TOPIC_RFLEARN ) + 3 ] ;
char buffer [ strlen ( MQTT_TOPIC_RFLEARN ) + 3 ] ;
snprintf_P ( buffer , sizeof ( buffer ) , PSTR ( " %s/+ " ) , MQTT_TOPIC_RFLEARN ) ;
snprintf_P ( buffer , sizeof ( buffer ) , PSTR ( " %s/+ " ) , MQTT_TOPIC_RFLEARN ) ;
mqttSubscribe ( buffer ) ;
mqttSubscribe ( buffer ) ;
# if not RF_SUPPORT
mqttSubscribe ( MQTT_TOPIC_RFOUT ) ;
# endif
# if RF_RAW_SUPPORT
if ( _rfb_transmit ) {
mqttSubscribe ( MQTT_TOPIC_RFOUT ) ;
}
# if !RFB_DIRECT
mqttSubscribe ( MQTT_TOPIC_RFRAW ) ;
mqttSubscribe ( MQTT_TOPIC_RFRAW ) ;
# endif
# endif
}
}
if ( type = = MQTT_MESSAGE_EVENT ) {
if ( type = = MQTT_MESSAGE_EVENT ) {
@ -545,21 +569,15 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa
}
}
# if not RF_SUPPORT
bool isRFOut = t . equals ( MQTT_TOPIC_RFOUT ) ;
# endif
# if RF_RAW_SUPPORT
bool isRFRaw = ! isRFOut & & t . equals ( MQTT_TOPIC_RFRAW ) ;
# elif not RF_SUPPORT
bool isRFRaw = false ;
# endif
if ( t . equals ( MQTT_TOPIC_RFOUT ) ) {
_rfbParseCode ( ( char * ) payload ) ;
}
# if not RF_SUPPOR T
if ( isRFOut | | isRFRaw ) {
_rfbParseCode ( ( char * ) payload ) ;
# if !RFB_DIRECT
if ( t . equals ( MQTT_TOPIC_RFRAW ) ) {
_rfbParseRaw ( ( char * ) payload ) ;
}
}
# endif // not RF_SUPPORT
# endif
}
}
@ -571,7 +589,6 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa
void _rfbAPISetup ( ) {
void _rfbAPISetup ( ) {
# if not RF_SUPPORT
apiRegister ( MQTT_TOPIC_RFOUT ,
apiRegister ( MQTT_TOPIC_RFOUT ,
[ ] ( char * buffer , size_t len ) {
[ ] ( char * buffer , size_t len ) {
snprintf_P ( buffer , len , PSTR ( " OK " ) ) ;
snprintf_P ( buffer , len , PSTR ( " OK " ) ) ;
@ -580,7 +597,6 @@ void _rfbAPISetup() {
_rfbParseCode ( ( char * ) payload ) ;
_rfbParseCode ( ( char * ) payload ) ;
}
}
) ;
) ;
# endif // RF_SUPPORT
apiRegister ( MQTT_TOPIC_RFLEARN ,
apiRegister ( MQTT_TOPIC_RFLEARN ,
[ ] ( char * buffer , size_t len ) {
[ ] ( char * buffer , size_t len ) {
@ -603,16 +619,16 @@ void _rfbAPISetup() {
}
}
) ;
) ;
# if RF_RAW_SUPPOR T
# if !RFB_DIREC T
apiRegister ( MQTT_TOPIC_RFRAW ,
apiRegister ( MQTT_TOPIC_RFRAW ,
[ ] ( char * buffer , size_t len ) {
[ ] ( char * buffer , size_t len ) {
snprintf_P ( buffer , len , PSTR ( " OK " ) ) ;
snprintf_P ( buffer , len , PSTR ( " OK " ) ) ;
} ,
} ,
[ ] ( const char * payload ) {
[ ] ( const char * payload ) {
_rfbParseCode ( payload ) ;
_rfbParseRaw ( ( char * ) payload ) ;
}
}
) ;
) ;
# endif // RF_RAW_SUPPORT
# endif
}
}
@ -685,8 +701,6 @@ String rfbRetrieve(unsigned char id, bool status) {
return getSetting ( key ) ;
return getSetting ( key ) ;
}
}
# if not RF_SUPPORT
void rfbStatus ( unsigned char id , bool status ) {
void rfbStatus ( unsigned char id , bool status ) {
String value = rfbRetrieve ( id , status ) ;
String value = rfbRetrieve ( id , status ) ;
@ -694,43 +708,29 @@ void rfbStatus(unsigned char id, bool status) {
bool same = _rfbSameOnOff ( id ) ;
bool same = _rfbSameOnOff ( id ) ;
# if RF_RAW_SUPPORT
byte message [ RF_MAX_MESSAGE_SIZE ] ;
int len = _rfbToArray ( value . c_str ( ) , message , 0 ) ;
byte message [ RF_MAX_MESSAGE_SIZE ] ;
int len = _rfbToArray ( value . c_str ( ) , message , 0 ) ;
if ( len = = RF_MESSAGE_SIZE & & // probably a standard msg
if ( len = = RF_MESSAGE_SIZE & & // probably a standard msg
( message [ 0 ] ! = RF_CODE_START | | // raw would start with 0xAA
( message [ 0 ] ! = RF_CODE_START | | // raw would start with 0xAA
message [ 1 ] ! = RF_CODE_RFOUT_BUCKET | | // followed by 0xB0,
message [ 1 ] ! = RF_CODE_RFOUT_BUCKET | | // followed by 0xB0,
message [ 2 ] + 4 ! = len | | // needs a valid length,
message [ 2 ] + 4 ! = len | | // needs a valid length,
message [ len - 1 ] ! = RF_CODE_STOP ) ) { // and finish with 0x55
message [ len - 1 ] ! = RF_CODE_STOP ) ) { // and finish with 0x55
if ( ! _rfbin ) {
unsigned char times = same ? 1 : RF_SEND_TIMES ;
_rfbSend ( message , times ) ;
}
} else {
_rfbSendRawOnce ( message , len ) ; // send a raw message
}
# else // RF_RAW_SUPPORT
if ( ! _rfbin ) {
if ( ! _rfbin ) {
byte message [ RF_MESSAGE_SIZE ] ;
_rfbToArray ( value . c_str ( ) , message ) ;
unsigned char times = same ? 1 : RF_SEND_TIMES ;
unsigned char times = same ? 1 : _rfb_repeat ;
_rfbSend ( message , times ) ;
_rfbSend ( message , times ) ;
}
}
# endif // RF_RAW_SUPPORT
} else {
_rfbSendRawOnce ( message , len ) ; // send a raw message
}
}
}
_rfbin = false ;
_rfbin = false ;
}
}
# endif // not RF_SUPPORT
void rfbLearn ( unsigned char id , bool status ) {
void rfbLearn ( unsigned char id , bool status ) {
_learnId = id ;
_learnId = id ;
@ -770,33 +770,49 @@ void rfbSetup() {
# if WEB_SUPPORT
# if WEB_SUPPORT
wsOnSendRegister ( _rfbWebSocketOnSend ) ;
wsOnSendRegister ( _rfbWebSocketOnSend ) ;
wsOnActionRegister ( _rfbWebSocketOnAction ) ;
wsOnActionRegister ( _rfbWebSocketOnAction ) ;
wsOnReceiveRegister ( _rfbWebSocketOnReceive ) ;
# endif
# endif
# if TERMINAL_SUPPORT
# if TERMINAL_SUPPORT
_rfbInitCommands ( ) ;
_rfbInitCommands ( ) ;
# endif
# endif
# if RFB_DIRECT || RF_SUPPORT
_rfb_repeat = getSetting ( " rfbRepeat " , RF_SEND_TIMES ) . toInt ( ) ;
# if RFB_DIRECT
unsigned char rx = getSetting ( " rfbRX " , RFB_RX_PIN ) . toInt ( ) ;
unsigned char tx = getSetting ( " rfbTX " , RFB_TX_PIN ) . toInt ( ) ;
_rfb_receive = gpioValid ( rx ) ;
_rfb_transmit = gpioValid ( tx ) ;
if ( ! _rfb_transmit & & ! _rfb_receive ) {
DEBUG_MSG_P ( PSTR ( " [RF] Neither RX or TX are set \n " ) ) ;
return ;
}
_rfModem = new RCSwitch ( ) ;
_rfModem = new RCSwitch ( ) ;
# if RF_SUPPORT
_rfModem - > enableReceive ( RF_PIN ) ;
DEBUG_MSG_P ( PSTR ( " [RF] RF receiver on GPIO %u \n " ) , RF_PIN ) ;
# else
_rfModem - > enableReceive ( RFB_RX_PIN ) ;
_rfModem - > enableTransmit ( RFB_TX_PIN ) ;
_rfModem - > setRepeatTransmit ( 6 ) ;
DEBUG_MSG_P ( PSTR ( " [RF] RF receiver on GPIO %u \n " ) , RFB_RX_PIN ) ;
DEBUG_MSG_P ( PSTR ( " [RF] RF transmitter on GPIO %u \n " ) , RFB_TX_PIN ) ;
# endif
if ( _rfb_receive ) {
_rfModem - > enableReceive ( rx ) ;
DEBUG_MSG_P ( PSTR ( " [RF] RF receiver on GPIO %u \n " ) , rx ) ;
}
if ( _rfb_transmit ) {
_rfModem - > enableTransmit ( tx ) ;
_rfModem - > setRepeatTransmit ( _rfb_repeat ) ;
DEBUG_MSG_P ( PSTR ( " [RF] RF transmitter on GPIO %u \n " ) , tx ) ;
}
# else
_rfb_receive = true ;
_rfb_transmit = true ;
# endif
# endif
// Register loop
// Register loop only when properly configured
espurnaRegisterLoop ( rfbLoop ) ;
espurnaRegisterLoop ( rfbLoop ) ;
}
}
void rfbLoop ( ) {
void rfbLoop ( ) {
_rfbReceive ( ) ;
_rfbReceive ( ) ;
_rfbSend ( ) ;
}
}
# endif
# endif