1use 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#[derive(Debug)]
25pub struct TmcmModule<'a, IF: Interface + 'a, Cell: InteriorMut<'a, IF>, T: Deref<Target=Cell> + 'a> {
26 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 pub fn new(interface: T, address: u8) -> Self {
36 TmcmModule{
37 address,
38 interface,
39 pd1: PhantomData{},
40 pd2: PhantomData{},
41 }
42 }
43
44 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
57pub trait TmcmInstruction: Instruction {}
59
60
61pub trait TmcmAxisParameter: AxisParameter {}
63
64pub trait ReadableTmcmAxisParameter: ReadableAxisParameter {}
66
67pub trait WriteableTmcmAxisParameter: WriteableAxisParameter {}