From 34769049f935038fec96035bc9753adaa44f28e7 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Wed, 1 Apr 2026 14:48:54 +0200 Subject: [PATCH 1/2] Add write que to microcontroller to avoid overringing the mcu buffer while not stalling the applicaiton --- src/microcontroller.cpp | 43 +++++++++++++++++++++++++++-------------- src/microcontroller.h | 10 +++++----- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/microcontroller.cpp b/src/microcontroller.cpp index 340bc6e..0bf8690 100644 --- a/src/microcontroller.cpp +++ b/src/microcontroller.cpp @@ -1,8 +1,5 @@ #include "microcontroller.h" -#include -#include - void Microcontroller::relayToggle(int state, int relay) { char buffer[8]; @@ -49,22 +46,34 @@ void Microcontroller::setAuxPwm(int duty) void Microcontroller::write(const QByteArray& buffer) { - if(_port != nullptr) + writeQue.enqueue(buffer); + if(!writeTimer.isActive()) { - _port->write(buffer); - _port->waitForBytesWritten(1000); + writeTimer.setInterval(0); + writeTimer.start(); } - std::this_thread::sleep_for(std::chrono::milliseconds(40)); } void Microcontroller::write(char* buffer, const size_t length) { - if(_port != nullptr) + write(QByteArray(buffer, length)); +} + +void Microcontroller::onWriteTimerTimeout() +{ + writeTimer.setInterval(50); + if(connected()) { - _port->write(buffer, length); - _port->waitForBytesWritten(1000); + if(!writeQue.empty()) + { + QByteArray data = writeQue.dequeue(); + _port->write(data); + } + else + { + writeTimer.stop(); + } } - std::this_thread::sleep_for(std::chrono::milliseconds(40)); } void Microcontroller::setPattern(int pattern) @@ -82,8 +91,10 @@ void Microcontroller::startSunrise() bool Microcontroller::connected() { - if(_port != nullptr) return _port->isOpen(); - else return false; + if(_port != nullptr) + return _port->isOpen(); + else + return false; } void Microcontroller::refresh() @@ -93,13 +104,17 @@ void Microcontroller::refresh() //housekeeping -Microcontroller::Microcontroller(QIODevice* port) +Microcontroller::Microcontroller(QIODevice* port): Microcontroller() { setIODevice(port); } Microcontroller::Microcontroller() { + writeTimer.setInterval(50); + writeTimer.setSingleShot(false); + connect(&writeTimer, &QTimer::timeout, this, &Microcontroller::onWriteTimerTimeout); + qDebug()<<__func__< #include #include -#include #include -#include +#include +#include #include #include #include "items/item.h" @@ -32,13 +32,12 @@ private: bool listMode = false; - //uint8_t _auxState = 0; - QIODevice* _port = nullptr; + QQueue writeQue; + QTimer writeTimer; std::vector< std::shared_ptr > relayList; - QScopedPointer loop; QString _buffer; void processMicroReturn(); @@ -75,6 +74,7 @@ public slots: private slots: void isReadyRead(); + void onWriteTimerTimeout(); signals: void textRecived(const QString string); From 9648c7c040a388d891639d3a5cc46ad522821771 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Wed, 1 Apr 2026 14:49:30 +0200 Subject: [PATCH 2/2] Fix item updates in various scenarious --- src/items/item.cpp | 25 +++++++++++++++++-------- src/items/item.h | 5 +++-- src/items/itemstore.cpp | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/items/item.cpp b/src/items/item.cpp index 6f88544..7f350a2 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -161,26 +161,34 @@ Item& Item::operator=(const ItemData& other) void Item::requestUpdate(ItemUpdateRequest update) { - if(!hasChanged(update.payload)) + assert(update.type != ITEM_UPDATE_INVALID); + if(update.type != ITEM_UPDATE_LOADED && value_ == update.payload.getValue()) return; if(update.type == ITEM_UPDATE_ACTOR && override_) return; - if(update.type != ITEM_UPDATE_LOADED - && (programMode == PROGRAM_MODE_PRIMARY || programMode == PROGRAM_MODE_HEADLESS_PRIMARY)) + qDebug()<<"Item Update Request for"<& actor : update.newActors) addActor(actor); } - + update.payload = *this; updated(update); } @@ -286,6 +294,7 @@ ItemUpdateRequest Item::createValueUpdateRequest(uint8_t value, bool withActors) { ItemUpdateRequest update; + update.type = type; update.payload = *this; update.payload.setValueData(value); if(withActors) diff --git a/src/items/item.h b/src/items/item.h index f88b8cf..d461006 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -19,7 +19,8 @@ typedef enum { ITEM_UPDATE_ACTOR, ITEM_UPDATE_REMOTE, ITEM_UPDATE_LOADED, - ITEM_UPDATE_BACKEND + ITEM_UPDATE_BACKEND, + ITEM_UPDATE_INVALID } item_update_type_t; class ItemData @@ -67,7 +68,7 @@ public: struct ItemUpdateRequest { - item_update_type_t type; + item_update_type_t type = ITEM_UPDATE_INVALID; ItemData payload; std::vector > newActors; }; diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index a14ae6f..232d319 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -29,6 +29,7 @@ void ItemStore::addItem(const std::shared_ptr& item, item_update_type_t up ItemUpdateRequest request = item->createValueUpdateRequest(item->getValue(), updateType, updateType == ITEM_UPDATE_LOADED); + request.newActors = item->getActors(); updateItem(request); } } @@ -56,6 +57,7 @@ void ItemStore::removeItem(const ItemData& item) void ItemStore::replaceItems(const std::vector>& items) { + qDebug()<<__func__; addItems(items, ITEM_UPDATE_LOADED); std::vector deletedItems; for(std::shared_ptr item : items_) @@ -86,7 +88,6 @@ void ItemStore::updateItem(const ItemUpdateRequest& update) if(items_[i]->operator==(update.payload)) { items_[i]->requestUpdate(update); - qDebug() << "Item" << items_[i]->getName() << "updated"; itemUpdated(update); } } @@ -106,7 +107,6 @@ void ItemStore::store(QJsonObject& json) void ItemStore::itemUpdateSlot(ItemUpdateRequest update) { - qDebug() << "Item" << update.payload.getName() << "updated from update slot"; itemUpdated(update); }