Skip to content

Commit 60483ec

Browse files
committed
During sleep: controller off and ignore commands
Should save some power Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 60f5613 commit 60483ec

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/main.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use cortex_m::delay::Delay;
99
use defmt_rtt as _;
1010
use embedded_hal::digital::v2::{InputPin, OutputPin};
1111

12+
use rp2040_hal::gpio::bank0::Gpio29;
1213
//#[cfg(debug_assertions)]
1314
//use panic_probe as _;
1415
use rp2040_panic_usb_boot as _;
@@ -78,7 +79,7 @@ use lotus_led_hal as bsp;
7879

7980
use bsp::hal::{
8081
clocks::{init_clocks_and_plls, Clock},
81-
pac,
82+
gpio, pac,
8283
sio::Sio,
8384
usb,
8485
watchdog::Watchdog,
@@ -217,8 +218,8 @@ fn main() -> ! {
217218

218219
let i2c = bsp::hal::I2C::i2c1(
219220
pac.I2C1,
220-
pins.gpio26.into_mode::<bsp::hal::gpio::FunctionI2C>(),
221-
pins.gpio27.into_mode::<bsp::hal::gpio::FunctionI2C>(),
221+
pins.gpio26.into_mode::<gpio::FunctionI2C>(),
222+
pins.gpio27.into_mode::<gpio::FunctionI2C>(),
222223
1000.kHz(),
223224
&mut pac.RESETS,
224225
&clocks.peripheral_clock,
@@ -293,10 +294,17 @@ fn main() -> ! {
293294
}
294295
Ok(count) => {
295296
if let Some(command) = parse_command(count, &buf) {
296-
handle_command(&command, &mut state, &mut matrix);
297-
298297
if let Command::Sleep(go_sleeping) = command {
299-
handle_sleep(go_sleeping, &mut state, &mut matrix, &mut delay);
298+
handle_sleep(
299+
go_sleeping,
300+
&mut state,
301+
&mut matrix,
302+
&mut delay,
303+
&mut led_enable,
304+
);
305+
} else if let SleepState::Awake = state.sleeping {
306+
// While sleeping no command is handled, except waking up
307+
handle_command(&command, &mut state, &mut matrix);
300308
}
301309

302310
fill_grid_pixels(&state.grid, &mut matrix);
@@ -307,7 +315,13 @@ fn main() -> ! {
307315
}
308316
}
309317

310-
fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &mut Delay) {
318+
fn handle_sleep(
319+
go_sleeping: bool,
320+
state: &mut State,
321+
matrix: &mut Foo,
322+
delay: &mut Delay,
323+
led_enable: &mut gpio::Pin<Gpio29, gpio::Output<gpio::PushPull>>,
324+
) {
311325
match (state.sleeping.clone(), go_sleeping) {
312326
(SleepState::Awake, false) => (),
313327
(SleepState::Awake, true) => {
@@ -329,6 +343,9 @@ fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &
329343
}
330344
}
331345

346+
// Turn LED controller off to save power
347+
led_enable.set_low().unwrap();
348+
332349
// TODO: Set up SLEEP# pin as interrupt and wfi
333350
//cortex_m::asm::wfi();
334351
}
@@ -339,6 +356,9 @@ fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &
339356
state.grid = old_grid;
340357
fill_grid_pixels(&state.grid, matrix);
341358

359+
// Power LED controller back on
360+
led_enable.set_high().unwrap();
361+
342362
// Slowly increase brightness
343363
delay.delay_ms(1000);
344364
let mut brightness = 0;

0 commit comments

Comments
 (0)