pub struct SupMCUMaster<I: I2CDevice + Send + Sync> {
pub modules: Vec<SupMCUModule<I>>,
/* private fields */
}
Expand description
A struct to represent an I2C bus of SupMCU modules
This basically just holds a vec of SupMCUModule
s and an async runtime.
The async runtime is used to run async functions like SupMCUModule.get_telemetry_by_def_async
from withing a sync context. This allows you to take advantage of the speedups
that come from accessing modules in parallel without having to deal with an entire
async application.
use supmcu_rs::supmcu::{
SupMCUMaster,
parsing::*
};
use std::{
time::Duration,
path::Path
};
// Initialize master from definition file
let mut master = SupMCUMaster::new("/dev/i2c-1", None)?;
master.load_def_file(Path::new("definition.json"))?;
// Get the first telemetry item (version string) from each module
let versions = master
.for_each(|module| module.get_telemetry_async(TelemetryType::SupMCU, 0))
.into_iter()
.collect::<Result<Vec<SupMCUTelemetry>, SupMCUError>>()?;
for version in versions {
// Prints the version string from each module in the definition.json file
println!("{}", &version.data[0]);
}
A SupMCUMaster is used to communicate with SupMCU modules over an I2C bus
Fields§
§modules: Vec<SupMCUModule<I>>
The SupMCUModule
s available to control
Implementations§
Source§impl<I> SupMCUMaster<I>
impl<I> SupMCUMaster<I>
Sourcepub fn discover_modules(&mut self) -> Result<(), SupMCUError>
pub fn discover_modules(&mut self) -> Result<(), SupMCUError>
Discover the definitions for each stored module
Sourcepub fn discover_module(
&mut self,
module: &SupMCUModuleDefinition,
) -> Result<(), SupMCUError>
pub fn discover_module( &mut self, module: &SupMCUModuleDefinition, ) -> Result<(), SupMCUError>
Discover an individual module’s definition
Sourcepub fn get_definitions(
&self,
) -> Result<Vec<SupMCUModuleDefinition>, SupMCUError>
pub fn get_definitions( &self, ) -> Result<Vec<SupMCUModuleDefinition>, SupMCUError>
Get module definitions of this SupMCUMaster
Sourcepub fn get_all_telemetry(
&mut self,
) -> Vec<Vec<Result<SupMCUTelemetry, SupMCUError>>>
pub fn get_all_telemetry( &mut self, ) -> Vec<Vec<Result<SupMCUTelemetry, SupMCUError>>>
Getting all the telemetry for each stored module
Sourcepub fn with_module<F: FnOnce(&SupMCUModule<I>) -> O, O: Send + 'static>(
&self,
module: &SupMCUModuleDefinition,
f: F,
) -> Result<O, SupMCUError>
pub fn with_module<F: FnOnce(&SupMCUModule<I>) -> O, O: Send + 'static>( &self, module: &SupMCUModuleDefinition, f: F, ) -> Result<O, SupMCUError>
Runs a closure for a specific module
Sourcepub fn with_module_mut<F: FnOnce(&mut SupMCUModule<I>) -> O, O: Send + 'static>(
&mut self,
module: &SupMCUModuleDefinition,
f: F,
) -> Result<O, SupMCUError>
pub fn with_module_mut<F: FnOnce(&mut SupMCUModule<I>) -> O, O: Send + 'static>( &mut self, module: &SupMCUModuleDefinition, f: F, ) -> Result<O, SupMCUError>
Runs a closure for a specific module, mutable
Sourcepub fn send_command(
&mut self,
module: &SupMCUModuleDefinition,
command: &str,
) -> Result<(), SupMCUError>
pub fn send_command( &mut self, module: &SupMCUModuleDefinition, command: &str, ) -> Result<(), SupMCUError>
Sends a command to a module
Sourcepub fn response_delay(
&mut self,
module: &SupMCUModuleDefinition,
delay: f32,
) -> Result<(), SupMCUError>
pub fn response_delay( &mut self, module: &SupMCUModuleDefinition, delay: f32, ) -> Result<(), SupMCUError>
Updates a module’s response delay
Sourcepub fn for_each<'a, F, T, O>(&'a mut self, f: F) -> Vec<O>
pub fn for_each<'a, F, T, O>(&'a mut self, f: F) -> Vec<O>
Runs an async function for each module and returns their results in a Vec
Sourcepub fn load_def_file(&mut self, file: &Path) -> Result<(), SupMCUError>
pub fn load_def_file(&mut self, file: &Path) -> Result<(), SupMCUError>
Load a SupMCU master from a definition file instead of discovering modules.
Sourcepub fn save_def_file<P: AsRef<Path>>(&self, file: P) -> Result<(), SupMCUError>
pub fn save_def_file<P: AsRef<Path>>(&self, file: P) -> Result<(), SupMCUError>
Save the modules definitions to a definition file
Source§impl SupMCUMaster<LinuxI2CDevice>
impl SupMCUMaster<LinuxI2CDevice>
Sourcepub fn scan_bus(
device: &str,
blacklist: Option<Vec<u16>>,
) -> Result<Vec<u16>, SupMCUError>
pub fn scan_bus( device: &str, blacklist: Option<Vec<u16>>, ) -> Result<Vec<u16>, SupMCUError>
Uses single byte reads to determine what addresses on the bus are populated.
Checks addresses between 0x03 and 0x77, inclusive.a
Sourcepub fn new<S: AsRef<str>>(
device: S,
blacklist: Option<Vec<u16>>,
) -> Result<Self, SupMCUError>
pub fn new<S: AsRef<str>>( device: S, blacklist: Option<Vec<u16>>, ) -> Result<Self, SupMCUError>
Initialize a SupMCUMaster with empty SupMCUModules, usually followed by discovery.
Sourcepub fn new_with_addrs<S: AsRef<str>>(
device: S,
addresses: Vec<u16>,
) -> Result<Self, SupMCUError>
pub fn new_with_addrs<S: AsRef<str>>( device: S, addresses: Vec<u16>, ) -> Result<Self, SupMCUError>
Initialize a SupMCUMaster, specifying addresses of modules to interact with
Sourcepub fn new_from_file<S: AsRef<str>, P: AsRef<Path>>(
device: S,
file: P,
) -> Result<Self, SupMCUError>
pub fn new_from_file<S: AsRef<str>, P: AsRef<Path>>( device: S, file: P, ) -> Result<Self, SupMCUError>
Initialize a SupMCUMaster with modules definitions that have been saved to disk
Sourcepub fn new_no_retries<S: AsRef<str>>(device: S) -> Result<Self, SupMCUError>
pub fn new_no_retries<S: AsRef<str>>(device: S) -> Result<Self, SupMCUError>
Initialize a SupMCUMaster without allowing any attempts to retry telemetry requests that return non-ready responses.
Auto Trait Implementations§
impl<I> !Freeze for SupMCUMaster<I>
impl<I> RefUnwindSafe for SupMCUMaster<I>where
I: RefUnwindSafe,
impl<I> Send for SupMCUMaster<I>
impl<I> Sync for SupMCUMaster<I>
impl<I> Unpin for SupMCUMaster<I>
impl<I> UnwindSafe for SupMCUMaster<I>where
I: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more