From a18554a3f8891fea7a9f256df2c52d90315f6909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 15 Jan 2018 22:48:41 +0100 Subject: [PATCH] Using a queue for RFBridge messages --- code/espurna/rfbridge.ino | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index 60d14882..c96e646d 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -8,7 +8,7 @@ Copyright (C) 2017-2018 by Xose PĂ©rez #ifdef ITEAD_SONOFF_RFBRIDGE -#include +#include #include // ----------------------------------------------------------------------------- @@ -47,7 +47,7 @@ typedef struct { byte code[RF_MESSAGE_SIZE]; byte times; } rfb_message_t; -std::vector _rfb_message_queue; +static std::queue _rfb_message_queue; Ticker _rfbTicker; // ----------------------------------------------------------------------------- @@ -150,11 +150,11 @@ void _rfbSend(byte * message) { void _rfbSend() { // Check if there is something in the queue - if (_rfb_message_queue.size() == 0) return; + if (_rfb_message_queue.empty()) return; // Pop the first element rfb_message_t message = _rfb_message_queue.front(); - _rfb_message_queue.erase(_rfb_message_queue.begin()); + _rfb_message_queue.pop(); // Send the message _rfbSend(message.code); @@ -162,11 +162,11 @@ void _rfbSend() { // If it should be further sent, push it to the stack again if (message.times > 1) { message.times = message.times - 1; - _rfb_message_queue.push_back(message); + _rfb_message_queue.push(message); } // if there are still messages in the queue... - if (_rfb_message_queue.size() > 0) { + if (!_rfb_message_queue.empty()) { _rfbTicker.once_ms(RF_SEND_DELAY, _rfbSend); } @@ -181,7 +181,7 @@ void _rfbSend(byte * code, int times) { rfb_message_t message; memcpy(message.code, code, RF_MESSAGE_SIZE); message.times = times; - _rfb_message_queue.push_back(message); + _rfb_message_queue.push(message); _rfbSend(); } @@ -359,6 +359,7 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa } _learnStatus = (char)payload[0] != '0'; _rfbLearn(); + return; }