Skip to content

Commit

Permalink
Add util function to fetch SDOs of a slave
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Oct 27, 2020
1 parent 2537066 commit 60e1622
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -6,6 +6,8 @@ use ethercat_sys as ec;
mod master;
mod types;

pub mod util;

pub use self::{
master::{Domain, Master, MasterAccess, SlaveConfig},
types::*,
Expand Down
26 changes: 26 additions & 0 deletions src/util.rs
@@ -0,0 +1,26 @@
use crate::{Error, Master, SdoEntryAddr, SdoEntryInfo, SdoIdx, SdoInfo, SdoPos, SlavePos, SubIdx};
use std::collections::HashMap;

type Result<T> = std::result::Result<T, Error>;

pub fn slave_sdos(
master: &mut Master,
slave_pos: SlavePos,
) -> Result<HashMap<SdoIdx, SdoEntryInfo>> {
let slave = master.get_slave_info(slave_pos)?;
let sdo_positions = (0..slave.sdo_count).into_iter().map(SdoPos::from);
let mut res = HashMap::new();
for sdo_pos in sdo_positions {
let SdoInfo {
idx, max_sub_idx, ..
} = master.get_sdo(slave_pos, sdo_pos)?;
let sdo_idxs = (0..u8::from(max_sub_idx))
.map(SubIdx::from)
.map(|sub_idx| SdoIdx { idx, sub_idx });
for sdo_idx in sdo_idxs {
let entry = master.get_sdo_entry(slave_pos, SdoEntryAddr::ByIdx(sdo_idx))?;
res.insert(sdo_idx, entry);
}
}
Ok(res)
}

0 comments on commit 60e1622

Please sign in to comment.