From b51ffc29a62e4bb5046b48ffaedc3c0e335a3280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 21 Jan 2018 23:15:34 +0100 Subject: [PATCH] Improve queue in RFBridge messages --- code/espurna/rfbridge.ino | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index f90fb14f..596d51f2 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -48,7 +48,8 @@ typedef struct { byte times; } rfb_message_t; static std::queue _rfb_message_queue; -Ticker _rfbTicker; +Ticker _rfb_ticker; +bool _rfb_ticker_active = false; // ----------------------------------------------------------------------------- // PRIVATES @@ -166,29 +167,35 @@ void _rfbSend() { } // if there are still messages in the queue... - if (!_rfb_message_queue.empty()) { - _rfbTicker.once_ms(RF_SEND_DELAY, _rfbSend); + if (_rfb_message_queue.empty()) { + _rfb_ticker.detach(); + _rfb_ticker_active = false; } } -void _rfbSend(byte * code, int times) { +void _rfbSend(byte * code, unsigned char times) { char buffer[RF_MESSAGE_SIZE]; _rfbToChar(code, buffer); - DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending MESSAGE '%s' %d time(s)\n"), buffer, times); + DEBUG_MSG_P(PSTR("[RFBRIDGE] Enqueuing MESSAGE '%s' %d time(s)\n"), buffer, times); rfb_message_t message; memcpy(message.code, code, RF_MESSAGE_SIZE); message.times = times; _rfb_message_queue.push(message); - _rfbSend(); + + // Enable the ticker if not running + if (!_rfb_ticker_active) { + _rfb_ticker_active = true; + _rfb_ticker.attach_ms(RF_SEND_DELAY, _rfbSend); + } } #if RF_RAW_SUPPORT -void _rfbSendRawOnce(byte *code, int length) { +void _rfbSendRawOnce(byte *code, unsigned char length) { char buffer[length*2]; _rfbToChar(code, buffer, length); DEBUG_MSG_P(PSTR("[RFBRIDGE] Sending RAW MESSAGE '%s'\n"), buffer);