Skip to content

Commit babe7c3

Browse files
add battery reading
1 parent fca55c4 commit babe7c3

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

examples/pico/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ fn main() -> ! {
115115
.send_and_wait_reply(&at_command::power_saving_mode::GetPowerSavingMode {})
116116
.unwrap();
117117
info!("power_saving_mode: {}", power_saving_mode);
118+
let battery_charge = modem
119+
.send_and_wait_reply(&at_command::battery::BatteryCharge {})
120+
.unwrap();
121+
info!("battery_charge: {}", battery_charge);
118122
let gprs_status = modem
119123
.send_and_wait_reply(&at_command::at_cgatt::GPRSServiceStatus {})
120124
.unwrap();

src/at_command/battery.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::at_command::{AtRequest, AtResponse, BufferType};
2+
use crate::AtError;
3+
use at_commands::parser::CommandParser;
4+
5+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6+
pub struct BatteryChargeStatus{
7+
capacity_percent: i32,
8+
voltage_millivolt: i32,
9+
}
10+
11+
12+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
13+
pub struct BatteryCharge;
14+
15+
impl AtRequest for BatteryCharge {
16+
type Response = Result<(), AtError>;
17+
18+
fn get_command<'a>(&'a self, buffer: &'a mut BufferType) -> Result<&'a [u8], usize> {
19+
at_commands::builder::CommandBuilder::create_execute(buffer, true)
20+
.named("+CBC")
21+
.finish()
22+
}
23+
24+
fn parse_response(&self, data: &[u8]) -> Result<AtResponse, AtError> {
25+
let (capacity_percent, voltage_millivolt) = CommandParser::parse(data)
26+
.expect_identifier(b"\r\n+CBC: ")
27+
.expect_int_parameter()
28+
.expect_int_parameter()
29+
.expect_identifier(b"\r\n\r\nOK")
30+
.finish()?;
31+
let status = BatteryChargeStatus{capacity_percent, voltage_millivolt};
32+
Ok(AtResponse::BatteryCharge(status))
33+
}
34+
}

src/at_command/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::at_command::sleep_indication::SleepIndication;
1010
use crate::{AtError, BUFFER_SIZE};
1111
#[cfg(feature = "defmt")]
1212
use defmt::debug;
13+
use crate::at_command::battery::BatteryChargeStatus;
1314

1415
pub mod at;
1516
pub mod at_cgatt;
@@ -33,6 +34,7 @@ pub mod ntp;
3334
pub mod pdp_context;
3435
pub mod power_saving_mode;
3536
pub mod sleep_indication;
37+
pub mod battery;
3638

3739
type BufferType = [u8; BUFFER_SIZE];
3840

@@ -55,6 +57,7 @@ pub enum AtResponse {
5557
PDPContext(Option<(PDPState, i32)>),
5658
SleepIndication(SleepIndication),
5759
PowerSavingMode(PowerSavingModeState),
60+
BatteryCharge(BatteryChargeStatus),
5861
}
5962

6063
pub trait AtRequest {

0 commit comments

Comments
 (0)