diff --git a/source/shoh/.cproject b/source/shoh/.cproject index 9a99684..1f9b41f 100644 --- a/source/shoh/.cproject +++ b/source/shoh/.cproject @@ -43,6 +43,7 @@ + diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index dbc1774..23d6536 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -9,6 +9,7 @@ #include "Log.h" #include "ThreadCommon.h" #include "Rotary.h" +#include "Relay.h" #include "Manager.h" #include "Logging.h" #include "UserInterface.h" @@ -108,6 +109,9 @@ void thread_master(void* pvParams) { manager->qm->createQueue(20, sizeof(UserInterface::InterfaceWithData), ThreadCommon::QueueManager::ui_event_manager); + manager->qm->createQueue(10, + sizeof(Event), + ThreadCommon::QueueManager::relay_event_master); LOG_INFO("Master created queues"); @@ -121,6 +125,9 @@ void thread_master(void* pvParams) { manager->tm->createTask(thread_user_interface, "user_interface", configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL, static_cast(manager)); + manager->tm->createTask(thread_relay, "relay", + configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL, + static_cast(manager)); LOG_INFO("Master created tasks"); m.taskFunction(); } diff --git a/source/shoh/src/threads/relay/Relay.cpp b/source/shoh/src/threads/relay/Relay.cpp new file mode 100644 index 0000000..a8733b0 --- /dev/null +++ b/source/shoh/src/threads/relay/Relay.cpp @@ -0,0 +1,151 @@ +/* + * Relay.cpp + * + * Created on: 12 May 2023 + * Author: tylen + */ + +#include "Relay.h" +#include "Event.h" +#include "Log.h" + +RelayDevice::RelayDevice(uint8_t pha_pin, + uint8_t pha_port, + uint8_t relay_device_index, + bool heat_output_1 + ) : + heat_output_one(heat_output_1) +{ + pha = new DigitalIoPin(pha_pin, pha_port, false); + LOG_DEBUG("Creating RelayDevice with output %d", + heat_output_one ? 1 : 2); + pha->write(heat_output_one); +} + +RelayDevice::~RelayDevice() +{ + LOG_ERROR("Deleting RelayDevice"); +} + +void inline RelayDevice::RelayOn() +{ + pha->write(!heat_output_one); +} + +void inline RelayDevice::RelayOff() +{ + pha->write(heat_output_one); +} + + +Relay::Relay(ThreadCommon::QueueManager* qm): _qm(qm) +{ + LOG_DEBUG("Creating Relay"); +} + +Relay::~Relay() +{ + LOG_ERROR("Deleting Relay"); +} + +void Relay::setPowerMode(PowerMode pm) +{ + switch (pm) + { + case PowerMode::POWER_0: + relays[INF_RELAY].RelayOff(); + relays[SUP_RELAY].RelayOff(); + LOG_INFO("Heater is turned OFF"); + case PowerMode::POWER_1: + relays[INF_RELAY].RelayOn(); + relays[SUP_RELAY].RelayOff(); + LOG_INFO("Heater using power mode 1"); + break; + case PowerMode::POWER_2: + relays[INF_RELAY].RelayOff(); + relays[SUP_RELAY].RelayOn(); + LOG_INFO("Heater using power mode 2"); + break; + case PowerMode::POWER_3: + relays[INF_RELAY].RelayOn(); + relays[SUP_RELAY].RelayOn(); + LOG_INFO("Heater using power mode 3"); + break; + default: + break; + } +} + +void Relay::taskFunction() +{ + Event data(Event::Null, 0); + for(;;) + { + _qm->receive(ThreadCommon::QueueManager::relay_event_master, &data, portMAX_DELAY); + parseEvent(&data); + utilizeEventData(); + } +} + +void Relay::parseEvent(Event* d) +{ + 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; + } + + } +} + +void Relay::utilizeEventData() +{ + PowerMode pm = POWER_0; + /* If setpoint is lower than ext_temp, + * none of below checks will pass and function + * shall turn OFF the heater. + * When setpoint is always higher than ext_temp, + * we can use up to three stages determine how + * powerful the output of the signal should be. + */ + int8_t diff = setpoint - ext_temp; + + if (diff >= 10) + { + pm = POWER_3; + } + + else if (diff >= 5) + { + pm = POWER_2; + } + + else if (diff >= 1) + { + pm = POWER_1; + } + + setPowerMode(pm); + return; +} + +void thread_relay(void * pvParams) +{ + ThreadCommon::CommonManagers * manager = static_cast(pvParams); + Relay r(manager->qm); + r.taskFunction(); +} diff --git a/source/shoh/src/threads/relay/Relay.h b/source/shoh/src/threads/relay/Relay.h new file mode 100644 index 0000000..5ee61d4 --- /dev/null +++ b/source/shoh/src/threads/relay/Relay.h @@ -0,0 +1,60 @@ +/* + * Relay.h + * + * Created on: 12 May 2023 + * Author: tylen + */ + +#ifndef THREADS_RELAY_RELAY_H_ +#define THREADS_RELAY_RELAY_H_ + +#include "ThreadCommon.h" +#include "DigitalIoPin.h" +#include "Counter.h" +#include "Event.h" + +class RelayDevice { + public: + RelayDevice(uint8_t pha_pin, + uint8_t pha_port, + uint8_t relay_device_index, + bool heat_output_1 = true); + virtual ~RelayDevice(); + void RelayOn(); + void RelayOff(); + private: + DigitalIoPin * pha; + bool heat_output_one; +}; + +class Relay { + enum PowerMode + { + POWER_0, + POWER_1, + POWER_2, + POWER_3 + }; + enum RelayType + { + INF_RELAY, + SUP_RELAY + }; +public: + Relay(ThreadCommon::QueueManager* qm); + virtual ~Relay(); + void taskFunction(); + void setPowerMode(PowerMode pm); + void utilizeEventData(); +private: + ThreadCommon::QueueManager* _qm; + RelayDevice relays [2] = {{0, 24, 0}, + {0, 26, 1}}; + + void parseEvent(Event * d); + int8_t setpoint, ext_temp; +}; + +void thread_relay(void * pvParams); + +#endif /* THREADS_RELAY_RELAY_H_ */