Skip to content

Commit

Permalink
added led controls to the hepa and uv tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
vegano1 committed Jan 20, 2024
1 parent 868ac67 commit 796a647
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
7 changes: 4 additions & 3 deletions hepa-uv/core/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ static auto light_control_task_builder =
void hepauv_tasks::start_tasks(
can::bus::CanBus& can_bus,
gpio_drive_hardware::GpioDrivePins& gpio_drive_pins,
light_control_task::LightControlInterface& led_hardware) {
light_control_hardware::LightControlHardware& led_hardware) {
auto& can_writer = can_task::start_writer(can_bus);
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);
// TODO: including led_hardware for testing, this should be a AssesorClient
auto& hepa_task = hepa_task_builder.start(5, "hepa_fan", gpio_drive_pins, led_hardware);
auto& uv_task = uv_task_builder.start(5, "uv_ballast", gpio_drive_pins, led_hardware);
auto& light_control_task = light_control_task_builder.start(5, "push_button_leds", led_hardware);

tasks.hepa_task_handler = &hepa_task;
Expand Down
4 changes: 2 additions & 2 deletions hepa-uv/firmware/led_hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ void button_led_hw_update_pwm(uint32_t duty_cycle, LED_TYPE led, PUSH_BUTTON_TYP
} else if (button == UV_BUTTON) {
switch(led) {
case RED_LED:
htim8.Instance->CCR2 = duty_cycle;
htim8.Instance->CCR3 = duty_cycle;
break;
case GREEN_LED:
htim8.Instance->CCR3 = duty_cycle;
htim8.Instance->CCR2 = duty_cycle;
break;
case BLUE_LED:
htim1.Instance->CCR4 = duty_cycle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ auto LightControlHardware::initialize() -> void { button_led_hw_initialize_leds(

void LightControlHardware::set_led_power(uint8_t button, uint8_t led, uint32_t duty_cycle) {
button_led_hw_update_pwm(duty_cycle, static_cast<LED_TYPE>(led), static_cast<PUSH_BUTTON_TYPE>(button));
}

void LightControlHardware::set_button_led_power(uint8_t button, uint32_t r, uint32_t g, uint32_t b, uint32_t w) {
set_button_led_pwm(static_cast<PUSH_BUTTON_TYPE>(button), r, g, b, w);
}
19 changes: 14 additions & 5 deletions include/hepa-uv/core/hepa_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
#include "common/core/logging.h"
#include "common/core/message_queue.hpp"
#include "common/firmware/gpio.hpp"
#include "hepa-uv/core/messages.hpp"
#include "hepa-uv/firmware/gpio_drive_hardware.hpp"
#include "messages.hpp"
#include "hepa-uv/firmware/light_control_hardware.hpp"
#include "hepa-uv/core/constants.h"

namespace hepa_task {

using TaskMessage = interrupt_task_messages::TaskMessage;

class HepaMessageHandler {
public:
explicit HepaMessageHandler(gpio_drive_hardware::GpioDrivePins &drive_pins)
: drive_pins{drive_pins} {
explicit HepaMessageHandler(
gpio_drive_hardware::GpioDrivePins &drive_pins,
light_control_hardware::LightControlHardware &led_hardware
)
: drive_pins{drive_pins}, led_hardware{led_hardware} {
// get current state
hepa_push_button = gpio::is_set(drive_pins.hepa_push_button);
// turn off the HEPA fan
Expand All @@ -42,8 +47,10 @@ class HepaMessageHandler {
// handle state changes here
if (hepa_push_button) {
gpio::set(drive_pins.hepa_on_off);
led_hardware.set_button_led_power(HEPA_BUTTON, 0, 50, 0, 0);
} else {
gpio::reset(drive_pins.hepa_on_off);
led_hardware.set_button_led_power(HEPA_BUTTON, 0, 0, 0, 50);
}
}

Expand All @@ -55,6 +62,7 @@ class HepaMessageHandler {
bool hepa_fan_on = false;

gpio_drive_hardware::GpioDrivePins &drive_pins;
light_control_hardware::LightControlHardware &led_hardware;
};

/**
Expand All @@ -77,8 +85,9 @@ class HepaTask {
* Task entry point.
*/
[[noreturn]] void operator()(
gpio_drive_hardware::GpioDrivePins *drive_pins) {
auto handler = HepaMessageHandler{*drive_pins};
gpio_drive_hardware::GpioDrivePins *drive_pins,
light_control_hardware::LightControlHardware *led_hardware) {
auto handler = HepaMessageHandler{*drive_pins, *led_hardware};
TaskMessage message{};
for (;;) {
if (queue.try_read(&message, queue.max_delay)) {
Expand Down
2 changes: 2 additions & 0 deletions include/hepa-uv/core/light_control_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LightControlInterface {
virtual ~LightControlInterface() = default;

virtual auto set_led_power(uint8_t button, uint8_t led, uint32_t duty_cycle) -> void = 0;

virtual auto set_button_led_power(uint8_t button, uint32_t r, uint32_t g, uint32_t b, uint32_t w) -> void = 0;
};

class LightControlMessageHandler {
Expand Down
3 changes: 2 additions & 1 deletion include/hepa-uv/core/tasks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "hepa-uv/core/hepa_task.hpp"
#include "hepa-uv/core/uv_task.hpp"
#include "hepa-uv/core/light_control_task.hpp"
#include "hepa-uv/firmware/light_control_hardware.hpp"
#include "hepa-uv/firmware/gpio_drive_hardware.hpp"

namespace hepauv_tasks {
Expand All @@ -13,7 +14,7 @@ namespace hepauv_tasks {
*/
void start_tasks(can::bus::CanBus& can_bus,
gpio_drive_hardware::GpioDrivePins& gpio_drive_pins,
light_control_task::LightControlInterface& led_hardware);
light_control_hardware::LightControlHardware& led_hardware);

/**
* Access to all the message queues in the system.
Expand Down
25 changes: 18 additions & 7 deletions include/hepa-uv/core/uv_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
#include "common/core/bit_utils.hpp"
#include "common/core/logging.h"
#include "common/core/message_queue.hpp"
#include "hepa-uv/core/constants.h"
#include "hepa-uv/core/messages.hpp"
#include "hepa-uv/firmware/gpio_drive_hardware.hpp"
#include "messages.hpp"
#include "hepa-uv/firmware/light_control_hardware.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
// static constexpr uint32_t DELAY_MS = 1000 * 60 * 15; // 15 minutes
static constexpr uint32_t DELAY_MS = 5000; // FOR TESTING

using TaskMessage = interrupt_task_messages::TaskMessage;

class UVMessageHandler {
public:
explicit UVMessageHandler(gpio_drive_hardware::GpioDrivePins &drive_pins)
explicit UVMessageHandler(
gpio_drive_hardware::GpioDrivePins &drive_pins,
light_control_hardware::LightControlHardware &led_hardware)
: drive_pins{drive_pins},
led_hardware{led_hardware},
_timer(
"UVTask", [ThisPtr = this] { ThisPtr->timer_callback(); },
DELAY_MS) {
Expand All @@ -43,6 +49,7 @@ class UVMessageHandler {
// call back to turn off the UV ballast
auto timer_callback() -> void {
gpio::reset(drive_pins.uv_on_off);
led_hardware.set_button_led_power(UV_BUTTON, 0, 0, 0, 50);
uv_push_button = false;
uv_fan_on = false;
}
Expand All @@ -67,17 +74,19 @@ class UVMessageHandler {

// 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;
// if (!door_closed || !reed_switch_set) uv_push_button = false;

// set the UV Ballast
if (door_closed && reed_switch_set && uv_push_button) {
if (uv_push_button) {
gpio::set(drive_pins.uv_on_off);
led_hardware.set_button_led_power(UV_BUTTON, 0, 50, 0, 0);
// 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);
led_hardware.set_button_led_power(UV_BUTTON, 0, 0, 0, 50);
if (_timer.is_running()) _timer.stop();
uv_fan_on = false;
}
Expand All @@ -92,6 +101,7 @@ class UVMessageHandler {
bool uv_fan_on = false;

gpio_drive_hardware::GpioDrivePins &drive_pins;
light_control_hardware::LightControlHardware &led_hardware;
ot_utils::freertos_timer::FreeRTOSTimer _timer;
};

Expand All @@ -115,8 +125,9 @@ class UVTask {
* Task entry point.
*/
[[noreturn]] void operator()(
gpio_drive_hardware::GpioDrivePins *drive_pins) {
auto handler = UVMessageHandler{*drive_pins};
gpio_drive_hardware::GpioDrivePins *drive_pins,
light_control_hardware::LightControlHardware *led_hardware) {
auto handler = UVMessageHandler{*drive_pins, *led_hardware};
TaskMessage message{};
for (;;) {
if (queue.try_read(&message, queue.max_delay)) {
Expand Down
1 change: 1 addition & 0 deletions include/hepa-uv/firmware/light_control_hardware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class LightControlHardware : public light_control_task::LightControlInterface {

auto initialize() -> void;
void set_led_power(uint8_t button, uint8_t led, uint32_t duty_cycle) final;
void set_button_led_power(uint8_t button, uint32_t r, uint32_t g, uint32_t b, uint32_t w) final;
};

} // namespace light_control_hardware

0 comments on commit 796a647

Please sign in to comment.