use embassy_time::Duration;
use crate::at::command;
use crate::at::processor::AtProcessor;
use crate::at::AtResponse;
use crate::bus::SpiTransport;
use crate::error::{Error, Result};
use crate::sync::TmMutex;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SleepMode {
NoSleep,
LightSleep,
ModemSleep,
DeepSleep,
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PowerConfig {
pub sleep_mode: SleepMode,
pub dtim_period: u8,
pub listen_interval: u8,
}
impl Default for PowerConfig {
fn default() -> Self {
Self {
sleep_mode: SleepMode::NoSleep,
dtim_period: 1,
listen_interval: 3,
}
}
}
pub struct PowerManager {
processor: &'static AtProcessor,
timeout: Duration,
}
impl PowerManager {
pub const fn new(processor: &'static AtProcessor, timeout: Duration) -> Self {
Self { processor, timeout }
}
pub async fn deep_sleep<SPI, CS>(
&self,
spi: &'static TmMutex<SpiTransport<SPI, CS>>,
duration_ms: u32,
) -> Result<()>
where
SPI: embedded_hal_async::spi::SpiDevice,
CS: embedded_hal::digital::OutputPin,
{
let cmd = command::system::deep_sleep(duration_ms)?;
let response = self
.processor
.send_command(spi, cmd.as_bytes(), self.timeout)
.await?;
if response == AtResponse::Ok {
Ok(())
} else {
Err(Error::AtCommandFailed)
}
}
pub async fn configure_power_saving<SPI, CS>(
&self,
_spi: &'static TmMutex<SpiTransport<SPI, CS>>,
_config: &PowerConfig,
) -> Result<()>
where
SPI: embedded_hal_async::spi::SpiDevice,
CS: embedded_hal::digital::OutputPin,
{
Err(Error::NotSupported)
}
pub async fn wake<SPI, CS>(&self, _spi: &'static TmMutex<SpiTransport<SPI, CS>>) -> Result<()>
where
SPI: embedded_hal_async::spi::SpiDevice,
CS: embedded_hal::digital::OutputPin,
{
Ok(())
}
}