Skip to content

Commit 7a1eb40

Browse files
committed
Change blink task so it only runs when DC power is off.
1 parent 2bc4ceb commit 7a1eb40

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

src/main.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ const APP: () = {
7373
///
7474
/// Sets up the hardware and spawns the regular tasks.
7575
///
76-
/// * Task `led_status_blink` - blinks the LED
76+
/// * Task `led_power_blink` - blinks the LED
7777
/// * Task `button_poll` - checks the power and reset buttons
78-
#[init(spawn = [led_status_blink, button_poll])]
78+
#[init(spawn = [led_power_blink, button_poll])]
7979
fn init(ctx: init::Context) -> init::LateResources {
8080
defmt::info!("Neotron BMC version {:?} booting", VERSION);
8181

@@ -126,7 +126,7 @@ const APP: () = {
126126

127127
serial.listen(serial::Event::Rxne);
128128

129-
ctx.spawn.led_status_blink().unwrap();
129+
ctx.spawn.led_power_blink().unwrap();
130130

131131
ctx.spawn.button_poll().unwrap();
132132

@@ -182,29 +182,34 @@ const APP: () = {
182182
///
183183
/// This task is called periodically. We check whether the status LED is currently on or off,
184184
/// and set it to the opposite. This makes the LED blink.
185-
#[task(schedule = [led_status_blink], resources = [led_status])]
186-
fn led_status_blink(ctx: led_status_blink::Context) {
185+
#[task(schedule = [led_power_blink], resources = [led_power, dc_power_enabled])]
186+
fn led_power_blink(ctx: led_power_blink::Context) {
187187
// Use the safe local `static mut` of RTIC
188188
static mut LED_STATE: bool = false;
189189

190-
defmt::trace!("blink time {}", ctx.scheduled.counts());
191-
192-
if *LED_STATE {
193-
ctx.resources.led_status.set_low().unwrap();
194-
*LED_STATE = false;
195-
} else {
196-
ctx.resources.led_status.set_high().unwrap();
197-
*LED_STATE = true;
190+
if *ctx.resources.dc_power_enabled == DcPowerState::Off {
191+
defmt::trace!("blink time {}", ctx.scheduled.counts());
192+
if *LED_STATE {
193+
ctx.resources.led_power.set_low().unwrap();
194+
*LED_STATE = false;
195+
} else {
196+
ctx.resources.led_power.set_high().unwrap();
197+
*LED_STATE = true;
198+
}
199+
let next = ctx.scheduled + LED_PERIOD_MS.millis();
200+
defmt::trace!("Next blink at {}", next.counts());
201+
ctx.schedule.led_power_blink(next).unwrap();
198202
}
199-
let next = ctx.scheduled + LED_PERIOD_MS.millis();
200-
defmt::trace!("Next blink at {}", next.counts());
201-
ctx.schedule.led_status_blink(next).unwrap();
202203
}
203204

204205
/// This task polls our power and reset buttons.
205206
///
206207
/// We poll them rather than setting up an interrupt as we need to debounce them, which involves waiting a short period and checking them again. Given that we have to do that, we might as well not bother with the interrupt.
207-
#[task(schedule = [button_poll], resources = [led_power, button_power, button_power_short_press, button_power_long_press, dc_power_enabled])]
208+
#[task(
209+
spawn = [led_power_blink],
210+
schedule = [button_poll],
211+
resources = [led_power, button_power, button_power_short_press, button_power_long_press, dc_power_enabled]
212+
)]
208213
fn button_poll(ctx: button_poll::Context) {
209214
// Poll button
210215
let pressed: bool = ctx.resources.button_power.is_low().unwrap();
@@ -256,6 +261,7 @@ const APP: () = {
256261
defmt::info!("Power off!");
257262
// TODO: Put system in reset here
258263
// TODO: Disable DC PSU here
264+
ctx.spawn.led_power_blink().unwrap();
259265
}
260266
}
261267

0 commit comments

Comments
 (0)