From 924bad62ed02efefa08a83a27a56e51f4b90ab5c Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 15 Sep 2024 20:46:19 +0200 Subject: [PATCH] Use deque for pending commands --- tw7100.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tw7100.cpp b/tw7100.cpp index 5465a02..7066c0f 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -8,7 +8,7 @@ namespace esphome { namespace tw7100 { bool waiting_for_answer = false; -std::vector cmd_queue; +std::deque cmd_queue; std::vector pwr_off_query_cmds{ "PWR?", "LAMP?" }; std::vector pwr_on_query_cmds{ @@ -96,7 +96,7 @@ void tw7100Component::setup() { void tw7100Component::update() { static const char *const TAG = "update()"; - if (cmd_queue.size() == 0) { + if (cmd_queue.empty()) { for (auto &cmd : pwr_off_query_cmds) { cmd_queue.push_back(cmd); } @@ -120,10 +120,10 @@ void tw7100Component::loop() { std::string response = readStringUntil(':'); if (response == "") { // no response on bus, we can use our time to do something else - if (cmd_queue.size() > 0 && !waiting_for_answer) { + if (!cmd_queue.empty() && !waiting_for_answer) { waiting_for_answer = true; - std::string cmd = cmd_queue[0]; - cmd_queue.erase(cmd_queue.begin()); + std::string cmd = cmd_queue.front(); + cmd_queue.pop_front(); ESP_LOGV(TAG, "sending out command: %s", cmd.c_str()); write_str((cmd + "\r\n").c_str()); flush();