Fix rf_bridge send and receive (#1180)

* Fix rf_bridge send and receive

* rf_bridge clang-format changes

* rf_bridge pased data bug fix

* rf_bridge logvv included for parsed data
This commit is contained in:
Víctor Ferrer García 2020-07-23 15:27:22 +02:00 committed by GitHub
parent 10e411f8c1
commit f29622abe1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 36 deletions

View file

@ -15,59 +15,74 @@ void RFBridgeComponent::ack_() {
this->flush(); this->flush();
} }
void RFBridgeComponent::decode_() { bool RFBridgeComponent::parse_bridge_byte_(uint8_t byte) {
uint8_t action = uartbuf_[0]; size_t at = this->rx_buffer_.size();
RFBridgeData data{}; this->rx_buffer_.push_back(byte);
const uint8_t *raw = &this->rx_buffer_[0];
// Byte 0: Start
if (at == 0)
return byte == RF_CODE_START;
// Byte 1: Action
if (at == 1)
return byte >= RF_CODE_ACK && byte <= RF_CODE_RFOUT;
uint8_t action = raw[1];
switch (action) { switch (action) {
case RF_CODE_ACK: case RF_CODE_ACK:
ESP_LOGD(TAG, "Action OK"); ESP_LOGD(TAG, "Action OK");
break; break;
case RF_CODE_LEARN_KO: case RF_CODE_LEARN_KO:
this->ack_(); ESP_LOGD(TAG, "Learning timeout");
ESP_LOGD(TAG, "Learn timeout");
break; break;
case RF_CODE_LEARN_OK: case RF_CODE_LEARN_OK:
ESP_LOGD(TAG, "Learn started");
case RF_CODE_RFIN: case RF_CODE_RFIN:
this->ack_(); if (at < RF_MESSAGE_SIZE + 2)
return true;
data.sync = (uartbuf_[1] << 8) | uartbuf_[2]; RFBridgeData data;
data.low = (uartbuf_[3] << 8) | uartbuf_[4]; data.sync = (raw[2] << 8) | raw[3];
data.high = (uartbuf_[5] << 8) | uartbuf_[6]; data.low = (raw[4] << 8) | raw[5];
data.code = (uartbuf_[7] << 16) | (uartbuf_[8] << 8) | uartbuf_[9]; data.high = (raw[6] << 8) | raw[7];
data.code = (raw[8] << 16) | (raw[9] << 8) | raw[10];
if (action == RF_CODE_LEARN_OK)
ESP_LOGD(TAG, "Learning success");
ESP_LOGD(TAG, "Received RFBridge Code: sync=0x%04X low=0x%04X high=0x%04X code=0x%06X", data.sync, data.low, ESP_LOGD(TAG, "Received RFBridge Code: sync=0x%04X low=0x%04X high=0x%04X code=0x%06X", data.sync, data.low,
data.high, data.code); data.high, data.code);
this->callback_.call(data); this->callback_.call(data);
break; break;
default: default:
ESP_LOGD(TAG, "Unknown action: 0x%02X", action); ESP_LOGW(TAG, "Unknown action: 0x%02X", action);
break; break;
} }
this->last_ = millis();
ESP_LOGVV(TAG, "Parsed: 0x%02X", byte);
if (byte == RF_CODE_STOP && action != RF_CODE_ACK)
this->ack_();
// return false to reset buffer
return false;
} }
void RFBridgeComponent::loop() { void RFBridgeComponent::loop() {
bool receiving = false; const uint32_t now = millis();
if (this->last_ != 0 && millis() - this->last_ > RF_DEBOUNCE) { if (now - this->last_bridge_byte_ > 50) {
this->last_ = 0; this->rx_buffer_.clear();
this->last_bridge_byte_ = now;
} }
while (this->available()) { while (this->available()) {
uint8_t c = this->read(); uint8_t byte;
if (receiving) { this->read_byte(&byte);
if (c == RF_CODE_STOP && (this->uartpos_ == 1 || this->uartpos_ == RF_MESSAGE_SIZE + 1)) { if (this->parse_bridge_byte_(byte)) {
this->decode_(); ESP_LOGVV(TAG, "Parsed: 0x%02X", byte);
receiving = false; this->last_bridge_byte_ = now;
} else if (this->uartpos_ <= RF_MESSAGE_SIZE) { } else {
this->uartbuf_[uartpos_++] = c; this->rx_buffer_.clear();
} else {
receiving = false;
}
} else if (c == RF_CODE_START) {
this->uartpos_ = 0;
receiving = true;
} }
} }
} }
@ -77,11 +92,17 @@ void RFBridgeComponent::send_code(RFBridgeData data) {
data.code); data.code);
this->write(RF_CODE_START); this->write(RF_CODE_START);
this->write(RF_CODE_RFOUT); this->write(RF_CODE_RFOUT);
this->write(data.sync); this->write((data.sync >> 8) & 0xFF);
this->write(data.low); this->write(data.sync & 0xFF);
this->write(data.high); this->write((data.low >> 8) & 0xFF);
this->write(data.code); this->write(data.low & 0xFF);
this->write((data.high >> 8) & 0xFF);
this->write(data.high & 0xFF);
this->write((data.code >> 16) & 0xFF);
this->write((data.code >> 8) & 0xFF);
this->write(data.code & 0xFF);
this->write(RF_CODE_STOP); this->write(RF_CODE_STOP);
this->flush();
} }
void RFBridgeComponent::learn() { void RFBridgeComponent::learn() {
@ -89,6 +110,7 @@ void RFBridgeComponent::learn() {
this->write(RF_CODE_START); this->write(RF_CODE_START);
this->write(RF_CODE_LEARN); this->write(RF_CODE_LEARN);
this->write(RF_CODE_STOP); this->write(RF_CODE_STOP);
this->flush();
} }
void RFBridgeComponent::dump_config() { void RFBridgeComponent::dump_config() {

View file

@ -45,10 +45,10 @@ class RFBridgeComponent : public uart::UARTDevice, public Component {
protected: protected:
void ack_(); void ack_();
void decode_(); void decode_();
bool parse_bridge_byte_(uint8_t byte);
unsigned long last_ = 0; std::vector<uint8_t> rx_buffer_;
unsigned char uartbuf_[RF_MESSAGE_SIZE + 3] = {0}; uint32_t last_bridge_byte_{0};
unsigned char uartpos_ = 0;
CallbackManager<void(RFBridgeData)> callback_; CallbackManager<void(RFBridgeData)> callback_;
}; };