Struct SupMCUMaster

Source
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 SupMCUModules 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 SupMCUModules available to control

Implementations§

Source§

impl<I> SupMCUMaster<I>
where I: I2CDevice + Send + Sync,

Source

pub fn discover_modules(&mut self) -> Result<(), SupMCUError>

Discover the definitions for each stored module

Source

pub fn discover_module( &mut self, module: &SupMCUModuleDefinition, ) -> Result<(), SupMCUError>

Discover an individual module’s definition

Source

pub fn get_definitions( &self, ) -> Result<Vec<SupMCUModuleDefinition>, SupMCUError>

Get module definitions of this SupMCUMaster

Source

pub fn get_all_telemetry( &mut self, ) -> Vec<Vec<Result<SupMCUTelemetry, SupMCUError>>>

Getting all the telemetry for each stored module

Source

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

Source

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

Source

pub fn send_command( &mut self, module: &SupMCUModuleDefinition, command: &str, ) -> Result<(), SupMCUError>

Sends a command to a module

Source

pub fn response_delay( &mut self, module: &SupMCUModuleDefinition, delay: f32, ) -> Result<(), SupMCUError>

Updates a module’s response delay

Source

pub fn for_each<'a, F, T, O>(&'a mut self, f: F) -> Vec<O>
where F: Fn(&'a mut SupMCUModule<I>) -> T, T: Future<Output = O> + Send, O: Send + 'static,

Runs an async function for each module and returns their results in a Vec

Source

pub fn load_def_file(&mut self, file: &Path) -> Result<(), SupMCUError>

Load a SupMCU master from a definition file instead of discovering modules.

Source

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>

Source

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

Source

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.

Source

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

Source

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

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.