From 4c0bc407dcdc40580845ea393d651bd31a4e128f Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:16:25 +0300 Subject: [PATCH 1/9] event: [#46] prepare event to deprecate map usage --- source/shoh/src/threads/common/Event.h | 35 +++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/source/shoh/src/threads/common/Event.h b/source/shoh/src/threads/common/Event.h index 34a242a..35b347d 100644 --- a/source/shoh/src/threads/common/Event.h +++ b/source/shoh/src/threads/common/Event.h @@ -12,7 +12,7 @@ #include typedef short int EventRawData; -const EventRawData ERROR_RETURN = -999; +const EventRawData ERROR_RETURN = -999; // Soon to be depercated class Event { @@ -31,14 +31,14 @@ public: { EventRawData rd; EventType et; - } EventPair; + } EventPair; // Soon to be depercated - Event(Event::EventType type, EventRawData data) + Event(Event::EventType type, EventRawData data) // Soon to be depercated { events.insert({type, data}); } - void inline addData(Event::EventType type, EventRawData data) + void inline addData(Event::EventType type, EventRawData data) // Soon to be depercated { const auto pos = events.find(type); // No duplicates @@ -46,7 +46,7 @@ public: events.insert({type, data}); } - EventRawData getDataOf(Event::EventType e) const + EventRawData getDataOf(Event::EventType e) const // Soon to be depercated { const auto pos = events.find(e); if (pos == events.end()) @@ -54,13 +54,36 @@ public: return pos->second; } - void inline setDataOf(Event::EventType e, EventRawData data) + void inline setDataOf(Event::EventType e, EventRawData data) // Soon to be depercated { events[e] = data; } +/* Event(Event::EventType type, EventRawData data) + { + setEvent(type, data); + } */ + + void setEvent(Event::EventType type, EventRawData data) + { + _type = type; + _data = data; + } + + Event::EventType inline getType() const + { + return _type; + } + + EventRawData inline getData() const + { + return _data; + } + private: std::map events; + Event::EventType _type; + EventRawData _data; }; From 2f6bce5d9a418c58a231db926f6233853521469d Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:24:08 +0300 Subject: [PATCH 2/9] threads: manager: [#46] use new Event --- source/shoh/src/threads/manager/Manager.cpp | 22 +-------------------- source/shoh/src/threads/manager/Manager.h | 1 - source/shoh/src/threads/manager/Menu.cpp | 10 +++++----- source/shoh/src/threads/manager/Menu.h | 2 +- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/source/shoh/src/threads/manager/Manager.cpp b/source/shoh/src/threads/manager/Manager.cpp index fafac81..507a9a6 100644 --- a/source/shoh/src/threads/manager/Manager.cpp +++ b/source/shoh/src/threads/manager/Manager.cpp @@ -20,33 +20,13 @@ Manager::~Manager() LOG_ERROR("Deleting Manager"); } -Event::EventPair Manager::parseEvent(Event* e) -{ - EventRawData raw_data; - for(Event::EventType i : - {Event::Rotary, Event::InternalTemp, - Event::ExternalTemp}) - { - raw_data = e->getDataOf(i); - if(raw_data != ERROR_RETURN) - { - Event::EventPair p = {raw_data, i}; - return p; - } - } - LOG_WARNING("Event is empty"); - return {ERROR_RETURN, Event::Null}; -} - void Manager::taskFunction() { Event data(Event::Null, 0); - Event::EventPair event_pair = {0, Event::EventType::Null}; for(;;) { _qm->receive(ThreadCommon::QueueManager::manager_event_master, &data, portMAX_DELAY); - event_pair= this->parseEvent(&data); - _menu.HandleEventPair(&event_pair); + _menu.HandleEventPair(&data); } } diff --git a/source/shoh/src/threads/manager/Manager.h b/source/shoh/src/threads/manager/Manager.h index 7a8e844..23e2c99 100644 --- a/source/shoh/src/threads/manager/Manager.h +++ b/source/shoh/src/threads/manager/Manager.h @@ -20,7 +20,6 @@ public: virtual ~Manager(); void taskFunction(); private: - Event::EventPair parseEvent(Event* e); ThreadCommon::QueueManager* _qm; Menu _menu; diff --git a/source/shoh/src/threads/manager/Menu.cpp b/source/shoh/src/threads/manager/Menu.cpp index 19d8593..cd31c4d 100644 --- a/source/shoh/src/threads/manager/Menu.cpp +++ b/source/shoh/src/threads/manager/Menu.cpp @@ -38,14 +38,14 @@ Menu::readSetPointFromEEPROM (void) } } -void Menu::HandleEventPair (Event::EventPair *ep) +void Menu::HandleEventPair (Event *ep) { - switch(ep->et/*EventType*/) + switch(ep->getType()/*EventType*/) { case Event::Rotary: // can be wrapped in a function, but this was found to be more clear solution, // since we are still handling eventpair here, although nested - switch(static_cast(ep->rd)/*RawData*/) + switch(static_cast(ep->getData())/*RawData*/) { case ThreadCommon::RotaryAction::Right: this->HandleObj(MenuObjEvent (MenuObjEvent::eRollClockWise)); @@ -68,7 +68,7 @@ void Menu::HandleEventPair (Event::EventPair *ep) break; case Event::ExternalTemp: //Change ExternalTemp value. -99 <= ext_temp <= 99 - this->ext_temp.setCurrent(ep->rd); + this->ext_temp.setCurrent(ep->getData()); //Refresh the menu screen. this->HandleObj(MenuObjEvent (MenuObjEvent::eRefresh)); break; @@ -179,7 +179,7 @@ void Menu::sSetPointMod(const MenuObjEvent &e) // Write to EEPROM eeprom.write_to(EEPROM_START_ADDR, (void*)&sp, sizeof(EventRawData)); - event_sp.setDataOf(Event::EventType::SetPoint, sp); + event_sp.setEvent(Event::EventType::SetPoint, sp); _qm->send(ThreadCommon::QueueManager::master_event_all, &event_sp, 1); this->SetState(&Menu::sMainView); diff --git a/source/shoh/src/threads/manager/Menu.h b/source/shoh/src/threads/manager/Menu.h index 76b1a22..e7fdadb 100644 --- a/source/shoh/src/threads/manager/Menu.h +++ b/source/shoh/src/threads/manager/Menu.h @@ -22,7 +22,7 @@ class Menu public: Menu (ThreadCommon::QueueManager* qm); virtual ~Menu (); - void HandleEventPair (Event::EventPair *ep); + void HandleEventPair (Event *ep); private: /* Variables and objects */ From 23dda8ba740aeb95fa10b879ea2ccce9cb29a13a Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:28:51 +0300 Subject: [PATCH 3/9] threads: master: [#46] use new Event --- source/shoh/src/threads/master/Master.cpp | 31 +++++++++-------------- source/shoh/src/threads/master/Master.h | 2 +- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index fc43545..3a0ad0d 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -36,9 +36,10 @@ Master::~Master() LOG_ERROR("Master was deleted"); } -void Master::HandleEventType(Event* e, Event::EventType type) +void Master::HandleEventType(Event* e) { - switch (type) + EventRawData rd = e->getData(); + switch (e->getType()) { case Event::Null: break; @@ -46,7 +47,7 @@ void Master::HandleEventType(Event* e, Event::EventType type) //Comes from rotary, goes to manager _qm->send(ThreadCommon::QueueManager::manager_event_master, e, 0); //LOG_WARNING("Timestamp: %zus, Clock: %zu, Chip freq: %zu", LPC_SCT1->COUNT_U / Chip_Clock_GetMainClockRate(), LPC_SCT1->COUNT_U, Chip_Clock_GetMainClockRate()); - LOG_DEBUG("Rotary: %s has been forwarded to manager", rotary_direction[e->getDataOf(type)]); + LOG_DEBUG("Rotary: %s has been forwarded to manager", rotary_direction[rd]); break; case Event::InternalTemp: // TODO remove (deprecated) @@ -55,14 +56,15 @@ void Master::HandleEventType(Event* e, Event::EventType type) //Comes from sensors, goes to relay & manager _qm->send(ThreadCommon::QueueManager::relay_event_master, e, 0); _qm->send(ThreadCommon::QueueManager::manager_event_master, e, 0); - LOG_DEBUG("ExtTemp: %d has been forwarded to manager and relay", e->getDataOf(type)); + LOG_DEBUG("ExtTemp: %d has been forwarded to manager and relay", rd); break; case Event::SetPoint: //Comes from manager, goes to relay _qm->send(ThreadCommon::QueueManager::relay_event_master, e, 0); - LOG_DEBUG("SetPoint: %d has been forwarded to relay", e->getDataOf(type)); + LOG_DEBUG("SetPoint: %d has been forwarded to relay", rd); break; default: + LOG_ERROR("Unknown EventType"); assert(0); break; } @@ -70,22 +72,13 @@ void Master::HandleEventType(Event* e, Event::EventType type) void Master::taskFunction() { Event data(Event::Null, 0); - for (;;) { + for (;;) + { if(!_qm->receive(ThreadCommon::QueueManager::master_event_all, &data, 10000)) data.setDataOf(Event::Rotary, ThreadCommon::RotaryAction::Idle); - - for(Event::EventType i : - {Event::Null, - Event::Rotary, - Event::InternalTemp, - Event::ExternalTemp, - Event::SetPoint}) - { - if (data.getDataOf(i) != ERROR_RETURN) - { - HandleEventType(&data, i); - } - } + + HandleEventType(&data); + global_clock->updateClock(); } } diff --git a/source/shoh/src/threads/master/Master.h b/source/shoh/src/threads/master/Master.h index 8585c6b..30efd46 100644 --- a/source/shoh/src/threads/master/Master.h +++ b/source/shoh/src/threads/master/Master.h @@ -26,7 +26,7 @@ public: private: Event* message; ThreadCommon::QueueManager* _qm; - void HandleEventType(Event* e, Event::EventType type); + void HandleEventType(Event* e); }; void thread_master(void* pvParams); From c5692fd5ba1934e97704f5e013ed50a0ccd29c0b Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:32:27 +0300 Subject: [PATCH 4/9] threads: relay: [#46] use new Event --- source/shoh/src/threads/relay/Relay.cpp | 36 ++++++++++--------------- source/shoh/src/threads/relay/Relay.h | 2 +- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/source/shoh/src/threads/relay/Relay.cpp b/source/shoh/src/threads/relay/Relay.cpp index a8733b0..12a4640 100644 --- a/source/shoh/src/threads/relay/Relay.cpp +++ b/source/shoh/src/threads/relay/Relay.cpp @@ -87,29 +87,21 @@ void Relay::taskFunction() } } -void Relay::parseEvent(Event* d) +void Relay::parseEvent(Event* e) { - for (uint8_t i = Event::ExternalTemp; i <= Event::SetPoint; i++) - { - EventRawData rd = d->getDataOf(static_cast(i)); - if(rd == ERROR_RETURN) - { - continue; - } - switch(i /* EventType */) - { - case Event::ExternalTemp: - ext_temp = rd; - break; - case Event::SetPoint: - setpoint = rd; - break; - default: - assert(0); - break; - } - - } + EventRawData rd = e->getData(); + switch(e->getType() /* EventType */) + { + case Event::ExternalTemp: + ext_temp = rd; + break; + case Event::SetPoint: + setpoint = rd; + break; + default: + assert(0); + break; + } } void Relay::utilizeEventData() diff --git a/source/shoh/src/threads/relay/Relay.h b/source/shoh/src/threads/relay/Relay.h index 5ee61d4..c626ff7 100644 --- a/source/shoh/src/threads/relay/Relay.h +++ b/source/shoh/src/threads/relay/Relay.h @@ -51,7 +51,7 @@ private: RelayDevice relays [2] = {{0, 24, 0}, {0, 26, 1}}; - void parseEvent(Event * d); + void parseEvent(Event * e); int8_t setpoint, ext_temp; }; From 6d80c979d82e68817dbe24abab3cf63679164b49 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:34:48 +0300 Subject: [PATCH 5/9] threads: rotary: [#46] use new Event --- source/shoh/src/threads/rotary/Rotary.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/shoh/src/threads/rotary/Rotary.cpp b/source/shoh/src/threads/rotary/Rotary.cpp index a161c5b..7baa5f3 100644 --- a/source/shoh/src/threads/rotary/Rotary.cpp +++ b/source/shoh/src/threads/rotary/Rotary.cpp @@ -70,13 +70,13 @@ Rotary::~Rotary() void Rotary::taskFunction() { auto action_from_rotary_isr = ThreadCommon::RotaryAction::Idle; - Event * p_e= new Event(Event::EventType::Rotary, action_from_rotary_isr); + Event data(Event::EventType::Rotary, action_from_rotary_isr); for (;;) { xQueueReceive(*p_rotary_isr_q, &action_from_rotary_isr, portMAX_DELAY); - p_e->setDataOf(Event::EventType::Rotary, action_from_rotary_isr); - _qm->send(ThreadCommon::QueueManager::master_event_all, p_e, 10); + data.setEvent(Event::EventType::Rotary, action_from_rotary_isr); + _qm->send(ThreadCommon::QueueManager::master_event_all, &data, 10); } } From 7c7e69ffa189a73d1052ea26ab0d5d9fa691f585 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:36:47 +0300 Subject: [PATCH 6/9] threads: manager: [#46] renmae event handling method --- source/shoh/src/threads/manager/Manager.cpp | 2 +- source/shoh/src/threads/manager/Menu.cpp | 2 +- source/shoh/src/threads/manager/Menu.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/shoh/src/threads/manager/Manager.cpp b/source/shoh/src/threads/manager/Manager.cpp index 507a9a6..a450a3f 100644 --- a/source/shoh/src/threads/manager/Manager.cpp +++ b/source/shoh/src/threads/manager/Manager.cpp @@ -26,7 +26,7 @@ void Manager::taskFunction() for(;;) { _qm->receive(ThreadCommon::QueueManager::manager_event_master, &data, portMAX_DELAY); - _menu.HandleEventPair(&data); + _menu.parseEvent(&data); } } diff --git a/source/shoh/src/threads/manager/Menu.cpp b/source/shoh/src/threads/manager/Menu.cpp index cd31c4d..f2086f4 100644 --- a/source/shoh/src/threads/manager/Menu.cpp +++ b/source/shoh/src/threads/manager/Menu.cpp @@ -38,7 +38,7 @@ Menu::readSetPointFromEEPROM (void) } } -void Menu::HandleEventPair (Event *ep) +void Menu::parseEvent (Event *ep) { switch(ep->getType()/*EventType*/) { diff --git a/source/shoh/src/threads/manager/Menu.h b/source/shoh/src/threads/manager/Menu.h index e7fdadb..10a19b7 100644 --- a/source/shoh/src/threads/manager/Menu.h +++ b/source/shoh/src/threads/manager/Menu.h @@ -22,7 +22,7 @@ class Menu public: Menu (ThreadCommon::QueueManager* qm); virtual ~Menu (); - void HandleEventPair (Event *ep); + void parseEvent (Event *ep); private: /* Variables and objects */ From 5441e3a9ca3dacb47e6f124e05f9cefe3572fdb8 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:42:10 +0300 Subject: [PATCH 7/9] threads: master: [#46] correct if statement --- source/shoh/src/threads/master/Master.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index 3a0ad0d..643480e 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -75,8 +75,10 @@ void Master::taskFunction() { for (;;) { if(!_qm->receive(ThreadCommon::QueueManager::master_event_all, &data, 10000)) - data.setDataOf(Event::Rotary, ThreadCommon::RotaryAction::Idle); - + { + data.setEvent(Event::Rotary, ThreadCommon::RotaryAction::Idle); + } + HandleEventType(&data); global_clock->updateClock(); From 90e4e0e43d73324b104ba9dadeea967ac204e361 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:44:38 +0300 Subject: [PATCH 8/9] threads: master: [#46] add error on Evemt::Null --- source/shoh/src/threads/master/Master.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index 643480e..a431323 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -42,6 +42,7 @@ void Master::HandleEventType(Event* e) switch (e->getType()) { case Event::Null: + LOG_ERROR("Master recieved Event::Null with data: %d", rd); break; case Event::Rotary: //Comes from rotary, goes to manager @@ -78,7 +79,7 @@ void Master::taskFunction() { { data.setEvent(Event::Rotary, ThreadCommon::RotaryAction::Idle); } - + HandleEventType(&data); global_clock->updateClock(); From 2ccce7a6b28f7f3c2da7d9390cfcee62a40067e4 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 23:46:05 +0300 Subject: [PATCH 9/9] event: [#46] remove deprecated methods --- source/shoh/src/threads/common/Event.h | 40 ++------------------------ 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/source/shoh/src/threads/common/Event.h b/source/shoh/src/threads/common/Event.h index 35b347d..3186d3c 100644 --- a/source/shoh/src/threads/common/Event.h +++ b/source/shoh/src/threads/common/Event.h @@ -8,11 +8,7 @@ #ifndef THREADS_COMMON_EVENT_H_ #define THREADS_COMMON_EVENT_H_ -#include -#include - typedef short int EventRawData; -const EventRawData ERROR_RETURN = -999; // Soon to be depercated class Event { @@ -27,42 +23,11 @@ public: NotifyUI }; - typedef struct _EventPair - { - EventRawData rd; - EventType et; - } EventPair; // Soon to be depercated - Event(Event::EventType type, EventRawData data) // Soon to be depercated - { - events.insert({type, data}); - } - - void inline addData(Event::EventType type, EventRawData data) // Soon to be depercated - { - const auto pos = events.find(type); - // No duplicates - if (pos == events.end()) - events.insert({type, data}); - } - - EventRawData getDataOf(Event::EventType e) const // Soon to be depercated - { - const auto pos = events.find(e); - if (pos == events.end()) - return ERROR_RETURN; - return pos->second; - } - - void inline setDataOf(Event::EventType e, EventRawData data) // Soon to be depercated - { - events[e] = data; - } - -/* Event(Event::EventType type, EventRawData data) + Event(Event::EventType type, EventRawData data) { setEvent(type, data); - } */ + } void setEvent(Event::EventType type, EventRawData data) { @@ -81,7 +46,6 @@ public: } private: - std::map events; Event::EventType _type; EventRawData _data; };