tmcl/modules/tmcm/
mod.rs

1//! A `TMCM` type useable with TMCM modules other than TMCM-100 and Monopack 2.
2
3use lib::ops::Deref;
4use lib::marker::PhantomData;
5
6pub mod instructions;
7pub mod axis_parameters;
8
9use interior_mut::InteriorMut;
10
11use Error;
12use Instruction;
13use instructions::DirectInstruction;
14use Interface;
15use Return;
16use Status;
17use Command;
18use AxisParameter;
19use ReadableAxisParameter;
20use WriteableAxisParameter;
21
22
23/// This type represennts a TMCM module other than TMCM-100 and Monopack 2.
24#[derive(Debug)]
25pub struct TmcmModule<'a, IF: Interface + 'a, Cell: InteriorMut<'a, IF>, T: Deref<Target=Cell> + 'a> {
26    /// The module address
27    address: u8,
28    interface: T,
29    pd1: PhantomData<&'a IF>,
30    pd2: PhantomData<&'a T>,
31}
32
33impl<'a, IF: Interface, Cell: InteriorMut<'a, IF>, T: Deref<Target=Cell>> TmcmModule<'a, IF, Cell, T> {
34    /// Create a new module
35    pub fn new(interface: T, address: u8) -> Self {
36        TmcmModule{
37            address,
38            interface,
39            pd1: PhantomData{},
40            pd2: PhantomData{},
41        }
42    }
43
44    /// Synchronously write a command and wait for the Reply
45    pub fn write_command<Instruction: TmcmInstruction + DirectInstruction>(&'a self, instruction: Instruction) -> Result<Instruction::Return, Error<IF::Error>> {
46        let mut interface = self.interface.borrow_int_mut().or(Err(Error::InterfaceUnavailable))?;
47        interface.transmit_command(&Command::new(self.address, instruction)).map_err(|e| Error::InterfaceError(e))?;
48        let reply = interface.receive_reply().map_err(|e| Error::InterfaceError(e))?;
49        match reply.status() {
50            Status::Ok(_) => Ok(<Instruction::Return as Return>::from_operand(reply.operand())),
51            Status::Err(e) => Err(e.into()),
52        }
53    }
54}
55
56
57/// An `AxisParameter` useable with all TMCM modules other than TMCM-100 and Monopack 2.
58pub trait TmcmInstruction: Instruction {}
59
60
61/// An `AxisParameter` useable with all TMCM modules other than TMCM-100 and Monopack 2.
62pub trait TmcmAxisParameter: AxisParameter {}
63
64/// A `ReadableAxisParameter` useable with all TMCM modules other than TMCM-100 and Monopack 2.
65pub trait ReadableTmcmAxisParameter: ReadableAxisParameter {}
66
67/// A `WriteableAxisParamtere` useable with all TMCM modules other than TMCM-100 and Monopack 2.
68pub trait WriteableTmcmAxisParameter: WriteableAxisParameter {}