diff --git a/eepy-launcher/src/main.rs b/eepy-launcher/src/main.rs index 2db98d3..ad04642 100644 --- a/eepy-launcher/src/main.rs +++ b/eepy-launcher/src/main.rs @@ -6,13 +6,12 @@ mod ui; extern crate panic_halt; -use core::arch::asm; use core::mem::offset_of; use core::sync::atomic::Ordering; use usb_device::bus::UsbBusAllocator; use eepy_gui::draw_target::EpdDrawTarget; use eepy_gui::element::Gui; -use eepy_sys::input::{has_event, next_event, set_touch_enabled}; +use eepy_sys::input::{eep, next_event, set_touch_enabled}; use eepy_sys::misc::get_serial; use eepy_sys::usb::{self, UsbBus}; use usb_device::prelude::*; @@ -101,10 +100,8 @@ fn main() { } else if NEEDS_REFRESH.swap(false, Ordering::Relaxed) { gui.draw_init(&mut draw_target); draw_target.refresh(false); - } else if !has_event() { - // has_event() is a syscall. The SVCall exception is a WFE wakeup event, so we need two - // WFEs so we don't immediately wake up. - unsafe { asm!("wfe", "wfe") }; + } else { + eep(); } } } diff --git a/eepy-sys/src/input.rs b/eepy-sys/src/input.rs index f2436d7..ec32caa 100644 --- a/eepy-sys/src/input.rs +++ b/eepy-sys/src/input.rs @@ -1,3 +1,4 @@ +use core::arch::asm; use core::mem::MaybeUninit; use crate::{syscall, SafeOption}; use crate::input_common::Event; @@ -47,4 +48,17 @@ pub fn has_event() -> bool { } has_event != 0 +} + +/// If there are no events remaining in the event queue, allow the processor to enter a sleep state +/// until there is a new event ready. +#[inline(always)] +pub fn eep() { + if !has_event() { + // SAFETY: "wfe" only (maybe) puts the processor to sleep temporarily and doesn't access any + // memory so this specific instruction is safe + // has_event() is a syscall. The SVCall exception is a WFE wakeup event, so we need two + // WFEs so we don't immediately wake up. + unsafe { asm!("wfe", "wfe"); } + } } \ No newline at end of file