From 389cb2d653591ed1d34029216172d591dc59ee60 Mon Sep 17 00:00:00 2001 From: vegano1 Date: Thu, 18 Jan 2024 11:30:02 -0500 Subject: [PATCH 1/3] feat(hepa-uv): add a task to handle the UV Ballast --- hepa-uv/core/tasks.cpp | 13 ++++- include/hepa-uv/core/tasks.hpp | 11 +++- include/hepa-uv/core/uv_task.hpp | 90 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 include/hepa-uv/core/uv_task.hpp diff --git a/hepa-uv/core/tasks.cpp b/hepa-uv/core/tasks.cpp index 2ac648d19..5ff086ffa 100644 --- a/hepa-uv/core/tasks.cpp +++ b/hepa-uv/core/tasks.cpp @@ -17,6 +17,9 @@ static auto queues = hepauv_tasks::QueueClient{can::ids::NodeId::hepa_uv}; static auto hepa_task_builder = freertos_task::TaskStarter<512, hepa_task::HepaTask>{}; +static auto uv_task_builder = + freertos_task::TaskStarter<512, uv_task::UVTask>{}; + /** * Start hepa_uv tasks. */ @@ -27,22 +30,30 @@ void hepauv_tasks::start_tasks( can_task::start_reader(can_bus); auto& hepa_task = hepa_task_builder.start(5, "hepa_fan", gpio_drive_pins); + auto& uv_task = uv_task_builder.start(5, "uv_ballast", gpio_drive_pins); tasks.hepa_task_handler = &hepa_task; + tasks.uv_task_handler = &uv_task; tasks.can_writer = &can_writer; queues.set_queue(&can_writer.get_queue()); queues.hepa_queue = &hepa_task.get_queue(); + queues.uv_queue = &uv_task.get_queue(); } hepauv_tasks::QueueClient::QueueClient(can::ids::NodeId this_fw) : can::message_writer::MessageWriter{this_fw} {} -void hepauv_tasks::QueueClient::send_interrupt_message( +void hepauv_tasks::QueueClient::send_hepa_message( const hepa_task::TaskMessage& m) { hepa_queue->try_write(m); } +void hepauv_tasks::QueueClient::send_uv_message( + const uv_task::TaskMessage& m) { + uv_queue->try_write(m); +} + /** * Access to the tasks singleton * @return diff --git a/include/hepa-uv/core/tasks.hpp b/include/hepa-uv/core/tasks.hpp index d9f633cd4..8c3001b37 100644 --- a/include/hepa-uv/core/tasks.hpp +++ b/include/hepa-uv/core/tasks.hpp @@ -2,6 +2,7 @@ #include "can/core/message_writer.hpp" #include "common/core/freertos_timer.hpp" #include "hepa-uv/core/hepa_task.hpp" +#include "hepa-uv/core/uv_task.hpp" #include "hepa-uv/firmware/gpio_drive_hardware.hpp" namespace hepauv_tasks { @@ -18,10 +19,14 @@ void start_tasks(can::bus::CanBus& can_bus, struct QueueClient : can::message_writer::MessageWriter { QueueClient(can::ids::NodeId this_fw); - void send_interrupt_message(const hepa_task::TaskMessage& m); + void send_hepa_message(const hepa_task::TaskMessage& m); + void send_uv_message(const hepa_task::TaskMessage& m); freertos_message_queue::FreeRTOSMessageQueue* hepa_queue{nullptr}; + + freertos_message_queue::FreeRTOSMessageQueue* + uv_queue{nullptr}; }; /** @@ -33,6 +38,10 @@ struct AllTask { hepa_task::HepaTask* hepa_task_handler{nullptr}; + + uv_task::UVTask* + uv_task_handler{nullptr}; + }; /** diff --git a/include/hepa-uv/core/uv_task.hpp b/include/hepa-uv/core/uv_task.hpp new file mode 100644 index 000000000..30915ee53 --- /dev/null +++ b/include/hepa-uv/core/uv_task.hpp @@ -0,0 +1,90 @@ +#pragma once + +#include "can/core/ids.hpp" +#include "common/core/bit_utils.hpp" +#include "common/core/logging.h" +#include "common/core/message_queue.hpp" +#include "hepa-uv/firmware/gpio_drive_hardware.hpp" +#include "messages.hpp" + +namespace uv_task { + +using TaskMessage = interrupt_task_messages::TaskMessage; + +class UVMessageHandler { + public: + explicit UVMessageHandler(gpio_drive_hardware::GpioDrivePins &drive_pins) + : drive_pins{drive_pins} {} + UVMessageHandler(const UVMessageHandler &) = delete; + UVMessageHandler(const UVMessageHandler &&) = delete; + auto operator=(const UVMessageHandler &) -> UVMessageHandler & = delete; + auto operator=(const UVMessageHandler &&) + -> UVMessageHandler && = delete; + ~UVMessageHandler() {} + + void handle_message(const TaskMessage &m) { + std::visit([this](auto o) { this->visit(o); }, m); + } + + private: + void visit(const std::monostate &) {} + + // Handle GPIO EXTI Interrupts here + void visit(const interrupt_task_messages::GPIOInterruptChanged &m) { + if (m.pin == drive_pins.uv_push_button.pin) { + uv_push_button = !uv_push_button; + // handle state changes here + if (uv_push_button) { + gpio::set(drive_pins.uv_on_off); + } else { + gpio::reset(drive_pins.uv_on_off); + } + } + + // TODO: send CAN message to host + } + + // state tracking variables + // TODO: add reed, door_open, etc + bool uv_push_button = false; + bool uv_fan_on = false; + + gpio_drive_hardware::GpioDrivePins &drive_pins; +}; + +/** + * The task type. + */ +template