ev3dev_lang_rust/
power_supply.rs1use std::{fs, path::Path};
5
6use crate::driver::DRIVER_PATH;
7use crate::utils::OrErr;
8use crate::{Attribute, Device, Driver, Ev3Error, Ev3Result};
9
10#[derive(Debug, Clone, Device)]
13pub struct PowerSupply {
14 driver: Driver,
15}
16
17impl PowerSupply {
18 pub fn new() -> Ev3Result<PowerSupply> {
20 let paths = fs::read_dir(Path::new(DRIVER_PATH).join("power_supply"))?;
21
22 for path in paths {
23 let file_name = path?.file_name();
24 let name = file_name.to_str().or_err()?;
25
26 if name.contains("ev3-battery") {
27 return Ok(PowerSupply {
28 driver: Driver::new("power_supply", name),
29 });
30 }
31 }
32
33 Err(Ev3Error::NotConnected {
34 device: "power_supply".to_owned(),
35 port: None,
36 })
37 }
38
39 pub fn get_current_now(&self) -> Ev3Result<i32> {
41 self.get_attribute("current_now").get()
42 }
43
44 pub fn get_scope(&self) -> Ev3Result<String> {
46 self.get_attribute("zscope").get()
47 }
48
49 pub fn get_technology(&self) -> Ev3Result<String> {
51 self.get_attribute("technology").get()
52 }
53
54 pub fn get_type(&self) -> Ev3Result<String> {
56 self.get_attribute("type").get()
57 }
58
59 pub fn get_voltage_max_design(&self) -> Ev3Result<i32> {
61 self.get_attribute("voltage_max_design").get()
62 }
63
64 pub fn get_voltage_min_design(&self) -> Ev3Result<i32> {
66 self.get_attribute("voltage_min_design").get()
67 }
68
69 pub fn get_voltage_now(&self) -> Ev3Result<i32> {
71 self.get_attribute("voltage_now").get()
72 }
73}