-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(hepa-uv): Add a task to handle the UV Ballast state based on external input #744
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#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" | ||
#include "ot_utils/freertos/freertos_timer.hpp" | ||
|
||
namespace uv_task { | ||
|
||
// How long to keep the UV light on in ms. | ||
static constexpr uint32_t DELAY_MS = 1000 * 60 * 15; // 15 minutes | ||
|
||
using TaskMessage = interrupt_task_messages::TaskMessage; | ||
|
||
class UVMessageHandler { | ||
public: | ||
explicit UVMessageHandler(gpio_drive_hardware::GpioDrivePins &drive_pins) | ||
: drive_pins{drive_pins}, | ||
_timer( | ||
"UVTask", [ThisPtr = this] { ThisPtr->timer_callback(); }, | ||
DELAY_MS) { | ||
// get current state | ||
uv_push_button = gpio::is_set(drive_pins.uv_push_button); | ||
door_closed = gpio::is_set(drive_pins.door_open); | ||
reed_switch_set = gpio::is_set(drive_pins.reed_switch); | ||
// turn off UV Ballast | ||
gpio::reset(drive_pins.uv_on_off); | ||
} | ||
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: | ||
// call back to turn off the UV ballast | ||
auto timer_callback() -> void { | ||
gpio::reset(drive_pins.uv_on_off); | ||
uv_push_button = false; | ||
uv_fan_on = false; | ||
} | ||
|
||
void visit(const std::monostate &) {} | ||
|
||
// Handle GPIO EXTI Interrupts here | ||
void visit(const interrupt_task_messages::GPIOInterruptChanged &m) { | ||
if (m.pin == drive_pins.hepa_push_button.pin) { | ||
// ignore hepa push button presses | ||
return; | ||
} | ||
|
||
// update states | ||
if (m.pin == drive_pins.uv_push_button.pin) { | ||
uv_push_button = !uv_push_button; | ||
} else if (m.pin == drive_pins.door_open.pin) { | ||
door_closed = gpio::is_set(drive_pins.door_open); | ||
} else if (m.pin == drive_pins.reed_switch.pin) { | ||
reed_switch_set = gpio::is_set(drive_pins.reed_switch); | ||
} | ||
|
||
// reset push button state if the door is opened or the reed switch is | ||
// not set | ||
if (!door_closed || !reed_switch_set) uv_push_button = false; | ||
|
||
// set the UV Ballast | ||
if (door_closed && reed_switch_set && uv_push_button) { | ||
gpio::set(drive_pins.uv_on_off); | ||
// start the turn off timer | ||
if (_timer.is_running()) _timer.stop(); | ||
_timer.start(); | ||
uv_fan_on = true; | ||
} else { | ||
gpio::reset(drive_pins.uv_on_off); | ||
if (_timer.is_running()) _timer.stop(); | ||
uv_fan_on = false; | ||
} | ||
|
||
// TODO: send CAN message to host | ||
} | ||
|
||
// state tracking variables | ||
bool door_closed = false; | ||
bool reed_switch_set = false; | ||
bool uv_push_button = false; | ||
bool uv_fan_on = false; | ||
|
||
gpio_drive_hardware::GpioDrivePins &drive_pins; | ||
ot_utils::freertos_timer::FreeRTOSTimer _timer; | ||
}; | ||
|
||
/** | ||
* The task type. | ||
*/ | ||
template <template <class> class QueueImpl> | ||
requires MessageQueue<QueueImpl<TaskMessage>, TaskMessage> | ||
class UVTask { | ||
public: | ||
using Messages = TaskMessage; | ||
using QueueType = QueueImpl<TaskMessage>; | ||
UVTask(QueueType &queue) : queue{queue} {} | ||
UVTask(const UVTask &c) = delete; | ||
UVTask(const UVTask &&c) = delete; | ||
auto operator=(const UVTask &c) = delete; | ||
auto operator=(const UVTask &&c) = delete; | ||
~UVTask() = default; | ||
|
||
/** | ||
* Task entry point. | ||
*/ | ||
[[noreturn]] void operator()( | ||
gpio_drive_hardware::GpioDrivePins *drive_pins) { | ||
auto handler = UVMessageHandler{*drive_pins}; | ||
TaskMessage message{}; | ||
for (;;) { | ||
if (queue.try_read(&message, queue.max_delay)) { | ||
handler.handle_message(message); | ||
} | ||
} | ||
} | ||
|
||
[[nodiscard]] auto get_queue() const -> QueueType & { return queue; } | ||
|
||
private: | ||
QueueType &queue; | ||
}; | ||
}; // namespace uv_task |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if i push the button twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We toggle the uv_push_button every time it's pressed on line 61.
Assuming the ballast is on and the timer is running, pressing the button again would turn it off and cancel the timer.