From 8b0919a9b4cd7429ab1a31faacb81773f516f7ef Mon Sep 17 00:00:00 2001 From: Tamas Domok Date: Mon, 5 Jan 2026 20:12:35 +0100 Subject: [PATCH] project: added battery.rs --- pico/app/src/main.rs | 15 ++++++- pico/pico-lib/src/battery.rs | 76 ++++++++++++++++++++++++++++++++++++ pico/pico-lib/src/lib.rs | 1 + 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 pico/pico-lib/src/battery.rs diff --git a/pico/app/src/main.rs b/pico/app/src/main.rs index ec84ae9..32735e5 100644 --- a/pico/app/src/main.rs +++ b/pico/app/src/main.rs @@ -1,7 +1,8 @@ #![no_std] #![no_main] -use atat::asynch::Client; +use alloc::string::ToString; +use atat::asynch::{AtatClient, Client}; use atat::{AtatIngress, DefaultDigester, Ingress, ResponseSlot, UrcChannel}; use core::ptr::addr_of_mut; use embassy_executor::Spawner; @@ -22,7 +23,8 @@ use {defmt_rtt as _, panic_probe as _}; use pico_lib::at::PicoHW; use pico_lib::poro; use pico_lib::urc; -use pico_lib::{at, call, network, sms}; +use pico_lib::utils::LogBE; +use pico_lib::{at, battery, call, network, sms}; extern crate alloc; @@ -141,6 +143,15 @@ async fn main(spawner: Spawner) { Timer::after(Duration::from_millis(100)).await; } + { + let _l = LogBE::new("AtBatteryChargeExecute".to_string()); + let r = client.send(&battery::AtBatteryChargeExecute).await; + match r { + Ok(b) => log::info!(" OK {:?}", b), + Err(e) => log::info!(" ERROR: {:?}", e), + } + } + call::call_number( &mut client, &mut pico, diff --git a/pico/pico-lib/src/battery.rs b/pico/pico-lib/src/battery.rs new file mode 100644 index 0000000..b998cc7 --- /dev/null +++ b/pico/pico-lib/src/battery.rs @@ -0,0 +1,76 @@ +use atat::atat_derive::AtatCmd; +use atat::atat_derive::AtatEnum; +use atat::atat_derive::AtatResp; + +// 3.2.52 AT+CBC Battery Charge +// AT+CBC +#[derive(Clone, Debug, AtatCmd)] +#[at_cmd("+CBC", BatteryChargeResponse)] +pub struct AtBatteryChargeExecute; + +// +CBC: ,, +#[derive(Debug, Clone, AtatResp, PartialEq)] +pub struct BatteryChargeResponse { + #[at_arg(position = 0)] + pub bcs: BatteryStatus, + #[at_arg(position = 1)] + pub bcl: u8, + #[at_arg(position = 2)] + pub voltage: u32, +} + +#[derive(Debug, Clone, PartialEq, AtatEnum)] +pub enum BatteryStatus { + NotCharging = 0, + Charging = 1, + ChargingFinished = 2, +} + +#[cfg(test)] +mod tests { + use crate::cmd_serialization_tests; + + use super::*; + use atat::AtatCmd; + use atat::heapless::String; + + cmd_serialization_tests! { + test_at_battery_charge_execute: ( + AtBatteryChargeExecute, + 7, + "AT+CBC\r", + ), + } + + #[test] + fn test_network_registration_responses() { + let cmd = AtBatteryChargeExecute; + + assert_eq!( + BatteryChargeResponse { + bcs: BatteryStatus::NotCharging, + bcl: 50, + voltage: 300, + }, + cmd.parse(Ok(b"+CBC: 0,50,300\r\n")).unwrap() + ); + + assert_eq!( + BatteryChargeResponse { + bcs: BatteryStatus::Charging, + bcl: 75, + voltage: 450, + }, + cmd.parse(Ok(b"+CBC: 1,75,450\r\n")).unwrap() + ); + + assert_eq!( + BatteryChargeResponse { + bcs: BatteryStatus::ChargingFinished, + bcl: 100, + voltage: 600, + }, + cmd.parse(Ok(b"+CBC: 2,100,600\r\n")).unwrap() + ); + } +} diff --git a/pico/pico-lib/src/lib.rs b/pico/pico-lib/src/lib.rs index 658a84f..78ffc53 100644 --- a/pico/pico-lib/src/lib.rs +++ b/pico/pico-lib/src/lib.rs @@ -3,6 +3,7 @@ extern crate alloc; pub mod at; +pub mod battery; pub mod call; pub mod network; pub mod poro;