Skip to content
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

automatically stop a running alarm after 5 minutes #56

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::task::alarm_settings::alarm_settings_handler;
use crate::task::buttons::{blue_button_handler, green_button_handler, yellow_button_handler};
use crate::task::display::display_handler;
use crate::task::orchestrate::{orchestrator, scheduler};
use crate::task::orchestrate::{alarm_expirer, orchestrator, scheduler};
use crate::task::power::{usb_power_detector, vsys_voltage_reader};
use crate::task::resources::*;
use crate::task::sound::sound_handler;
Expand Down Expand Up @@ -62,6 +62,7 @@ async fn main(_spawner: Spawner) {
if task_config.orchestrator {
spawner.spawn(orchestrator()).unwrap();
spawner.spawn(scheduler()).unwrap();
spawner.spawn(alarm_expirer()).unwrap();
}

// Low priority executor: runs in thread mode, using WFE/SEV
Expand Down
32 changes: 24 additions & 8 deletions src/task/orchestrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ pub async fn orchestrator() {
}
Events::Scheduler((hour, minute, second)) => {
info!("Scheduler event");
// we only update the light effects if the alarm is not enabled and the alarm state is None
// update the light effects if the alarm is not enabled and the alarm state is None
if state_manager.alarm_state == AlarmState::None
&& !state_manager.alarm_settings.get_enabled()
{
LIGHTFX_SIGNAL.signal(Commands::LightFXUpdate((hour, minute, second)));
}
// update the display
DISPLAY_SIGNAL.signal(Commands::DisplayUpdate);
}
Events::RtcUpdated => {
Expand Down Expand Up @@ -116,14 +117,17 @@ pub async fn orchestrator() {
state_manager.set_alarm_mode();
DISPLAY_SIGNAL.signal(Commands::DisplayUpdate);
LIGHTFX_SIGNAL.signal(Commands::LightFXUpdate((0, 0, 0)));
ALARM_EXPIRER_SIGNAL.signal(Commands::AlarmExpiry);
}
Events::AlarmStop => {
info!("Alarm stop event");
state_manager.set_normal_mode();
DISPLAY_SIGNAL.signal(Commands::DisplayUpdate);
LIGHTFX_STOP_SIGNAL.signal(Commands::LightFXUpdate((0, 0, 0)));
LIGHTFX_SIGNAL.signal(Commands::LightFXUpdate((0, 0, 0)));
SOUND_STOP_SIGNAL.signal(Commands::SoundUpdate);
if state_manager.alarm_state.is_active() {
state_manager.set_normal_mode();
DISPLAY_SIGNAL.signal(Commands::DisplayUpdate);
LIGHTFX_STOP_SIGNAL.signal(Commands::LightFXUpdate((0, 0, 0)));
LIGHTFX_SIGNAL.signal(Commands::LightFXUpdate((0, 0, 0)));
SOUND_STOP_SIGNAL.signal(Commands::SoundUpdate);
};
}
Events::SunriseEffectFinished => {
info!("Sunrise effect finished event");
Expand All @@ -132,8 +136,6 @@ pub async fn orchestrator() {
LIGHTFX_SIGNAL.signal(Commands::LightFXUpdate((0, 0, 0)));
}
}
// log the state of the system
info!("{}", state_manager);
drop(state_manager_guard);
}
}
Expand Down Expand Up @@ -244,3 +246,17 @@ pub async fn scheduler() {
select(downtime_timer, SCHEDULER_WAKE_SIGNAL.wait()).await;
}
}

/// This task handles the expiration of the alarm after 5 minutes.
#[embassy_executor::task]
pub async fn alarm_expirer() {
info!("Alarm expirer task started");
'_mainloop: loop {
// wait for the alarm expiry watcher signal
ALARM_EXPIRER_SIGNAL.wait().await;
// wait for 5 minutes
Timer::after(Duration::from_secs(300)).await;
// send the alarm stop event
EVENT_CHANNEL.sender().send(Events::AlarmStop).await;
}
}
8 changes: 7 additions & 1 deletion src/task/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl AlarmSettings {
/// The state of the alarm
#[derive(PartialEq, Debug, Format, Clone)]
pub enum AlarmState {
/// The alarm is not active, the alarm time has not been reached
/// The alarm is not active
None,
/// The alarm time has been reached, the alarm is active and the sunrise effect is displayed on the neopixel ring. The user
/// can stop the alarm by pressing the buttons in the correct sequence.
Expand All @@ -356,6 +356,12 @@ pub enum AlarmState {
Noise,
}

impl AlarmState {
pub fn is_active(&self) -> bool {
self != &AlarmState::None
}
}

/// The battery level of the system in steps of 20% from 0 to 100. One additional state is provided for charging.
#[derive(PartialEq, Debug, Format, Clone)]
pub enum BatteryLevel {
Expand Down
5 changes: 5 additions & 0 deletions src/task/task_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum Commands {
SchedulerWakeUp,
/// Wake the vsys_voltage_reader task early when awaiting the next iteration
VsysWakeUp,
/// Handle the expiry of a running alarm
AlarmExpiry,
}

/// For the events that we want the orchestrator to react to, all state events are of the type Enum Events.
Expand Down Expand Up @@ -100,3 +102,6 @@ pub static SOUND_STOP_SIGNAL: Signal<CriticalSectionRawMutex, Commands> = Signal

/// Signal for the wake command that we want the orchestrator to send to the vsys_voltage_reader task.
pub static VSYS_WAKE_SIGNAL: Signal<CriticalSectionRawMutex, Commands> = Signal::new();

/// Signal for the alarm expiry command that we want the orchestrator to send to the alarm expiration task.
pub static ALARM_EXPIRER_SIGNAL: Signal<CriticalSectionRawMutex, Commands> = Signal::new();
Loading