MCPcopy Index your code
hub / github.com/FrameworkComputer/inputmodule-rs / handle_sleep

Function handle_sleep

ledmatrix/src/main.rs:606–680  ·  view source on GitHub ↗

Will do nothing if already in the right state

(
    sleep_reason: Option<SleepReason>,
    state: &mut LedmatrixState,
    matrix: &mut Foo,
    delay: &mut Delay,
    led_enable: &mut gpio::Pin<Gpio29, gpio::Output<gpio::PushPull>>,
)

Source from the content-addressed store, hash-verified

604
605// Will do nothing if already in the right state
606fn handle_sleep(
607 sleep_reason: Option<SleepReason>,
608 state: &mut LedmatrixState,
609 matrix: &mut Foo,
610 delay: &mut Delay,
611 led_enable: &mut gpio::Pin<Gpio29, gpio::Output<gpio::PushPull>>,
612) {
613 match (state.sleeping.clone(), sleep_reason) {
614 // Awake and staying awake
615 (SleepState::Awake, None) => (),
616 (SleepState::Awake, Some(sleep_reason)) => {
617 state.sleeping = SleepState::Sleeping((state.grid.clone(), state.brightness));
618 // Slowly decrease brightness
619 if dyn_sleep_mode(state) == SleepMode::Fading {
620 let mut brightness = state.brightness;
621 loop {
622 delay.delay_ms(100);
623 brightness = if brightness <= 5 { 0 } else { brightness - 5 };
624 set_brightness(state, brightness, matrix);
625 if brightness == 0 {
626 break;
627 }
628 }
629 }
630
631 if debug_mode(state) {
632 state.grid = display_sleep_reason(sleep_reason);
633 fill_grid_pixels(state, matrix);
634 } else {
635 // Turn LED controller off to save power
636 led_enable.set_low().unwrap();
637 }
638
639 // TODO: Set up SLEEP# pin as interrupt and wfi
640 //cortex_m::asm::wfi();
641 }
642 // Already sleeping and new sleep reason => just keep sleeping
643 (SleepState::Sleeping(_), Some(sleep_reason)) => {
644 // If debug mode is enabled, then make sure the latest sleep reason is displayed
645 if debug_mode(state) {
646 state.grid = display_sleep_reason(sleep_reason);
647 fill_grid_pixels(state, matrix);
648 }
649 }
650 // Sleeping and need to wake up
651 (SleepState::Sleeping((old_grid, old_brightness)), None) => {
652 // Restore back grid before sleeping
653 state.sleeping = SleepState::Awake;
654 state.grid = old_grid;
655 fill_grid_pixels(state, matrix);
656
657 // Power LED controller back on
658 if !debug_mode(state) {
659 led_enable.set_high().unwrap();
660 }
661
662 // Slowly increase brightness
663 if dyn_sleep_mode(state) == SleepMode::Fading {

Callers 1

mainFunction · 0.70

Calls 6

dyn_sleep_modeFunction · 0.85
set_brightnessFunction · 0.85
debug_modeFunction · 0.85
display_sleep_reasonFunction · 0.85
fill_grid_pixelsFunction · 0.85
unwrapMethod · 0.80

Tested by

no test coverage detected