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