Skip to content

Commit

Permalink
remove std use in sleep.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindnetland committed Aug 14, 2023
1 parent 0e4faf6 commit 90c0b7c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/deep_sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use esp_idf_hal::sleep::DeepSleep;
use esp_idf_hal::sleep::Sleep;
use esp_idf_hal::sleep::WakeupSource;
use std::thread;
use std::time::Duration;
use core::time::Duration;

fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
Expand Down
2 changes: 1 addition & 1 deletion examples/light_sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use esp_idf_hal::sleep::LightSleep;
use esp_idf_hal::sleep::Sleep;
use esp_idf_hal::sleep::WakeupSource;
use std::thread;
use std::time::Duration;
use core::time::Duration;
use std::time::Instant;

fn print_wakeup_result(time_before: Instant) {
Expand Down
31 changes: 24 additions & 7 deletions src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
//! Currently implemented Timer and UART wakeup sources.
use esp_idf_sys::*;
use std::time::Duration;
use core::time::Duration;

#[derive(Debug)]
const MAX_WAKEUP_SOURCES: usize = 10;

#[derive(Default, Debug)]
pub enum WakeupSource {
#[default]
None,
Timer { dur: Duration },
Uart { uart_num: i32, threshold: i32 },
// TODO: add more sources
Expand All @@ -15,6 +19,7 @@ pub enum WakeupSource {
impl WakeupSource {
pub fn apply(&self) -> Result<(), EspError> {
match *self {
WakeupSource::None => {}
WakeupSource::Timer { dur } => {
esp!(unsafe { esp_sleep_enable_timer_wakeup(dur.as_micros() as u64) })?;
}
Expand Down Expand Up @@ -48,7 +53,8 @@ pub trait Sleep {

#[derive(Default, Debug)]
pub struct LightSleep {
wakeup_sources: Vec<WakeupSource>,
wakeup_sources: [WakeupSource; MAX_WAKEUP_SOURCES],
n_wakeup_sources: usize,
}

impl LightSleep {
Expand All @@ -63,7 +69,11 @@ impl LightSleep {

impl Sleep for LightSleep {
fn add_wakeup_source(&mut self, wakeup: WakeupSource) -> Result<(), EspError> {
self.wakeup_sources.push(wakeup);
if self.n_wakeup_sources >= MAX_WAKEUP_SOURCES {
return Err(EspError::from_infallible::<ESP_FAIL>())
}
self.wakeup_sources[self.n_wakeup_sources] = wakeup;
self.n_wakeup_sources += 1;
Ok(())
}

Expand All @@ -77,7 +87,8 @@ impl Sleep for LightSleep {

#[derive(Default, Debug)]
pub struct DeepSleep {
wakeup_sources: Vec<WakeupSource>,
wakeup_sources: [WakeupSource; MAX_WAKEUP_SOURCES],
n_wakeup_sources: usize,
}

impl DeepSleep {
Expand All @@ -95,12 +106,18 @@ impl Sleep for DeepSleep {
if matches!(wakeup, WakeupSource::Uart { .. }) {
return Err(EspError::from_infallible::<ESP_ERR_INVALID_ARG>());
}
self.wakeup_sources.push(wakeup);

if self.n_wakeup_sources >= MAX_WAKEUP_SOURCES {
return Err(EspError::from_infallible::<ESP_FAIL>())
}
self.wakeup_sources[self.n_wakeup_sources] = wakeup;
self.n_wakeup_sources += 1;

Ok(())
}

fn sleep(&self) -> Result<(), EspError> {
self.apply_wakeup_sources(&self.wakeup_sources)?;
self.apply_wakeup_sources(&self.wakeup_sources[0..self.n_wakeup_sources])?;

unsafe { esp_deep_sleep_start() };
#[allow(unreachable_code)]
Expand Down

0 comments on commit 90c0b7c

Please sign in to comment.