diff --git a/source/shoh/src/threads/common/Event.h b/source/shoh/src/threads/common/Event.h index 34a242a..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; class Event { @@ -27,40 +23,31 @@ public: NotifyUI }; - typedef struct _EventPair - { - EventRawData rd; - EventType et; - } EventPair; Event(Event::EventType type, EventRawData data) { - events.insert({type, data}); + setEvent(type, data); + } + + void setEvent(Event::EventType type, EventRawData data) + { + _type = type; + _data = data; } - void inline addData(Event::EventType type, EventRawData data) + Event::EventType inline getType() const { - const auto pos = events.find(type); - // No duplicates - if (pos == events.end()) - events.insert({type, data}); + return _type; } - EventRawData getDataOf(Event::EventType e) const + EventRawData inline getData() const { - const auto pos = events.find(e); - if (pos == events.end()) - return ERROR_RETURN; - return pos->second; - } - - void inline setDataOf(Event::EventType e, EventRawData data) - { - events[e] = data; + return _data; } private: - std::map events; + Event::EventType _type; + EventRawData _data; }; diff --git a/source/shoh/src/threads/manager/Manager.cpp b/source/shoh/src/threads/manager/Manager.cpp index fafac81..a450a3f 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.parseEvent(&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..f2086f4 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::parseEvent (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..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::EventPair *ep); + void parseEvent (Event *ep); private: /* Variables and objects */ diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index fc43545..a431323 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -36,17 +36,19 @@ 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: + LOG_ERROR("Master recieved Event::Null with data: %d", rd); break; case Event::Rotary: //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 +57,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 +73,15 @@ 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); - } - } + { + data.setEvent(Event::Rotary, ThreadCommon::RotaryAction::Idle); + } + + 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); 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; }; 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); } }