Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dbus/interface/source/iio_imu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl SourceIioImuInterface {
pub async fn listen_on_dbus(
conn: Connection,
device: UdevDevice,
) -> Result<(), Box<dyn Error>> {
) -> Result<(), Box<dyn Error + Send + Sync>> {
let iface = SourceIioImuInterface::new(device);
let Ok(id) = iface.id() else {
return Ok(());
Expand Down
25 changes: 18 additions & 7 deletions src/drivers/iio_imu/driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{collections::HashMap, error::Error, time::Duration};
use std::{
collections::HashMap,
error::Error,
sync::{Arc, Mutex},
time::Duration,
};

use industrial_io::{Channel, ChannelType, Device, Direction};

Expand All @@ -12,9 +17,9 @@ use super::{
/// Driver for reading IIO IMU data
pub struct Driver {
mount_matrix: MountMatrix,
accel: HashMap<String, Channel>,
accel: HashMap<String, Arc<Mutex<Channel>>>,
accel_info: HashMap<String, AxisInfo>,
gyro: HashMap<String, Channel>,
gyro: HashMap<String, Arc<Mutex<Channel>>>,
gyro_info: HashMap<String, AxisInfo>,
pub sample_delay: Duration,
}
Expand Down Expand Up @@ -116,7 +121,7 @@ impl Driver {
let Some(info) = self.accel_info.get(id) else {
continue;
};
let data = channel.attr_read_int("raw")?;
let data = channel.lock().unwrap().attr_read_int("raw")?;

// processed_value = (raw + offset) * scale
let value = (data + info.offset) as f64 * info.scale;
Expand Down Expand Up @@ -144,7 +149,7 @@ impl Driver {
let Some(info) = self.gyro_info.get(id) else {
continue;
};
let data = channel.attr_read_int("raw")?;
let data = channel.lock().unwrap().attr_read_int("raw")?;

// processed_value = (raw + offset) * scale
let value = (data + info.offset) as f64 * info.scale;
Expand Down Expand Up @@ -194,7 +199,10 @@ impl Driver {
fn get_channels_with_type(
device: &Device,
channel_type: ChannelType,
) -> (HashMap<String, Channel>, HashMap<String, AxisInfo>) {
) -> (
HashMap<String, Arc<Mutex<Channel>>>,
HashMap<String, AxisInfo>,
) {
let mut channels = HashMap::new();
let mut channel_info = HashMap::new();
device
Expand Down Expand Up @@ -276,8 +284,11 @@ fn get_channels_with_type(
scales_avail,
};
channel_info.insert(id.clone(), info);
channels.insert(id, channel);
channels.insert(id, Arc::new(Mutex::new(channel)));
});

(channels, channel_info)
}

// A mutex is used to access channels
unsafe impl Send for Driver {}
Loading