expose internal charge/load current
This commit is contained in:
parent
892da9adbd
commit
d0801b2896
|
@ -8,6 +8,8 @@ pub struct Pli {
|
|||
port: Box<dyn SerialPort>,
|
||||
voltage_gauge: Gauge,
|
||||
duty_cycle: Gauge,
|
||||
internal_charge_current: Gauge,
|
||||
internal_load_current: Gauge,
|
||||
}
|
||||
|
||||
impl Pli {
|
||||
|
@ -20,21 +22,35 @@ impl Pli {
|
|||
let voltage_gauge = gauge!("pl_battery_voltage");
|
||||
describe_gauge!("pl_duty_cycle", "Duty cycle");
|
||||
let duty_cycle = gauge!("pl_duty_cycle");
|
||||
describe_gauge!("pl_internal_charge_current", "Internal charge current");
|
||||
let internal_charge_current = gauge!("pl_internal_charge_current");
|
||||
describe_gauge!("pl_internal_load_current", "Internal load current");
|
||||
let internal_load_current = gauge!("pl_internal_load_current");
|
||||
|
||||
Ok(Self {
|
||||
port,
|
||||
voltage_gauge,
|
||||
duty_cycle,
|
||||
internal_charge_current,
|
||||
internal_load_current,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self) {
|
||||
if let Ok(batv) = self.read_ram(50) {
|
||||
if let Ok(batv) = self.read_ram(PlRamAddress::Batv) {
|
||||
self.voltage_gauge.set((batv as f64) * (4. / 10.));
|
||||
}
|
||||
if let Ok(duty_cycle) = self.read_ram(39) {
|
||||
if let Ok(duty_cycle) = self.read_ram(PlRamAddress::Dutycyc) {
|
||||
self.duty_cycle.set((duty_cycle as f64) / 255.);
|
||||
}
|
||||
if let Ok(internal_charge_current) = self.read_ram(PlRamAddress::Cint) {
|
||||
self.internal_charge_current
|
||||
.set((internal_charge_current as f64) * (4. / 10.));
|
||||
}
|
||||
if let Ok(internal_load_current) = self.read_ram(PlRamAddress::Lint) {
|
||||
self.internal_load_current
|
||||
.set((internal_load_current as f64) * (4. / 10.));
|
||||
}
|
||||
}
|
||||
|
||||
fn send_command(&mut self, req: [u8; 4]) {
|
||||
|
@ -52,14 +68,32 @@ impl Pli {
|
|||
buf
|
||||
}
|
||||
|
||||
fn read_ram(&mut self, address: u8) -> anyhow::Result<u8> {
|
||||
self.send_command(command(20, address, 0));
|
||||
fn read_ram(&mut self, address: PlRamAddress) -> anyhow::Result<u8> {
|
||||
self.send_command(command(20, address.into(), 0));
|
||||
let buf: [u8; 2] = self.receive();
|
||||
if buf[0] == 200 { Some(buf[1]) } else { None }
|
||||
.context(format!("Error from PLI: {}", buf[0]))
|
||||
}
|
||||
}
|
||||
|
||||
enum PlRamAddress {
|
||||
Dutycyc,
|
||||
Batv,
|
||||
Cint,
|
||||
Lint,
|
||||
}
|
||||
|
||||
impl From<PlRamAddress> for u8 {
|
||||
fn from(value: PlRamAddress) -> Self {
|
||||
match value {
|
||||
PlRamAddress::Dutycyc => 39,
|
||||
PlRamAddress::Batv => 50,
|
||||
PlRamAddress::Cint => 213,
|
||||
PlRamAddress::Lint => 217,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn command(operation: u8, address: u8, data: u8) -> [u8; 4] {
|
||||
[operation, address, data, !operation]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue