SharedDeviceState

Struct SharedDeviceState 

Source
pub struct SharedDeviceState { /* private fields */ }
Expand description

Thread-safe device state container.

This struct provides thread-safe access to device state and allows for state change notifications.

Implementations§

Source§

impl SharedDeviceState

Source

pub fn new(device_info: DeviceInfo, device_data: DeviceData) -> Self

Create a new shared device state.

§Parameters
  • device_info - The device information.
  • device_data - The device data.
§Returns

A new shared device state.

Source

pub fn setup_notifications(&self) -> Receiver<StateChangeType>

Set up state change notifications.

§Returns

A receiver for state change notifications.

Source

pub fn status(&self) -> ThreadStatus

Get the current thread status.

§Returns

The current thread status.

Source

pub fn set_running(&self, running: bool)

Set the thread as running.

§Parameters
  • running - Whether the thread is running.
Source

pub fn set_paused(&self, paused: bool)

Set the thread as paused.

§Parameters
  • paused - Whether the thread is paused.
Source

pub fn update_from_device_with_notifications(&self, device: &PoKeysDevice)

Update the device state from a PoKeys device and detect changes.

§Parameters
  • device - The PoKeys device to update from.
Source

pub fn update(&self, update_fn: impl FnOnce(&mut DeviceState))

§Parameters
  • update_fn - A function that updates the device state.
Source

pub fn read<T>(&self, read_fn: impl FnOnce(&DeviceState) -> T) -> T

Read the device state.

§Parameters
  • read_fn - A function that reads the device state.
§Returns

The result of the read function.

Source

pub fn last_update(&self) -> u64

Get the last update timestamp.

§Returns

The last update timestamp.

Source

pub fn get_digital_input(&self, pin: u32) -> Option<bool>

Get a digital input value.

§Parameters
  • pin - The pin number to read.
§Returns

The value of the digital input (true for high, false for low), or None if the pin is invalid.

Source

pub fn get_analog_input(&self, pin: u32) -> Option<u32>

Get an analog input value.

§Parameters
  • pin - The pin number to read.
§Returns

The value of the analog input (0-4095 for 12-bit ADC), or None if the pin is invalid.

Source

pub fn get_encoder_value(&self, encoder_index: u32) -> Option<i32>

Get an encoder value.

§Parameters
  • encoder_index - The encoder index to read.
§Returns

The value of the encoder, or None if the encoder index is invalid.

Source

pub fn get_pwm_duty_cycle(&self, channel: usize) -> Option<u32>

Get a PWM duty cycle.

§Parameters
  • channel - The PWM channel to read.
§Returns

The duty cycle of the PWM channel (0-4095 for 12-bit PWM), or None if the channel is invalid.

Source

pub fn set_digital_output(&self, pin: u32, value: bool)

Set a digital output value.

§Parameters
  • pin - The pin number to set.
  • value - The value to set (true for high, false for low).
Source

pub fn set_analog_output(&self, pin: u32, value: u32)

Set an analog output value.

§Parameters
  • pin - The pin number to set.
  • value - The value to set (0-4095 for 12-bit DAC).
Source

pub fn set_pwm_duty_cycle(&self, channel: usize, duty: u32)

Set a PWM duty cycle.

§Parameters
  • channel - The PWM channel to set.
  • duty - The duty cycle to set (0-4095 for 12-bit PWM).
Source

pub fn set_custom_value(&self, key: &str, value: &str)

Set a custom value.

§Parameters
  • key - The key of the custom value.
  • value - The value to set.
Source

pub fn get_custom_value(&self, key: &str) -> Option<String>

Get a custom value.

§Parameters
  • key - The key of the custom value.
§Returns

The custom value, or None if the key is not found.

Source

pub fn set_error(&self, error: Option<String>)

Set an error message.

§Parameters
  • error - The error message, or None to clear the error.
Source

pub fn get_error(&self) -> Option<String>

Get the error message.

§Returns

The error message, or None if there is no error.

Source

pub fn with_state<F, T>(&self, f: F) -> T
where F: FnOnce(&DeviceState) -> T,

Access the device state with a function.

§Parameters
  • f - A function that takes a reference to the device state and returns a value.
§Returns

The result of the function.

Auto Trait Implementations§

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, 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.