dynamixel/protocol1/
control_table.rs

1macro_rules! rw_reg1{
2    ($name:ident, $type:ident, $address:expr) => {
3        register_impl1!($name, $type, $address);
4
5        impl $name {
6            pub fn new(v: $type) -> Self {
7                $name(v)
8            }
9        }
10        
11        impl From<$name> for $type {
12            fn from(v: $name) -> $type {
13                v.0
14            }
15        }
16
17        read_register_impl1!($name, $type);
18        write_register_impl1!($name, $type);
19    };
20}
21
22macro_rules! r_reg1{
23    ($name:ident, $type:ident, $address:expr) => {
24        register_impl1!($name, $type, $address);
25
26        impl From<$name> for $type {
27            fn from(v: $name) -> $type {
28                v.0
29            }
30        }
31        
32        read_register_impl1!($name, $type);
33    };
34}
35
36macro_rules! register_impl1{
37    ($name:ident, bool, $address:expr) => {
38        #[derive(Debug, PartialEq, Eq, Clone, Copy)]
39        pub struct $name(bool);
40
41        impl ::protocol1::Register for $name {
42            const SIZE: u8 = 1;
43            const ADDRESS: u8 = $address;
44        }
45    };
46    ($name:ident, u8, $address:expr) => {
47        #[derive(Debug, PartialEq, Eq, Clone, Copy)]
48        pub struct $name(u8);
49        
50        impl ::protocol1::Register for $name {
51            const SIZE: u8 = 1;
52            const ADDRESS: u8 = $address;
53        }
54    };
55    ($name:ident, i16, $address:expr) => {
56        #[derive(Debug, PartialEq, Eq, Clone, Copy)]
57        pub struct $name(i16);
58        
59        impl ::protocol1::Register for $name {
60            const SIZE: u8 = 2;
61            const ADDRESS: u8 = $address;
62        }
63    };
64    ($name:ident, u16, $address:expr) => {
65        #[derive(Debug, PartialEq, Eq, Clone, Copy)]
66        pub struct $name(u16);
67        
68        impl ::protocol1::Register for $name {
69            const SIZE: u8 = 2;
70            const ADDRESS: u8 = $address;
71        }
72    };
73    ($name:ident, i32, $address:expr) => {
74        #[derive(Debug, PartialEq, Eq, Clone, Copy)]
75        pub struct $name(i32);
76        
77        impl ::protocol1::Register for $name {
78            const SIZE: u8 = 4;
79            const ADDRESS: u8 = $address;
80        }
81    };
82
83}
84
85macro_rules! read_register_impl1{
86    ($name:ident, bool) => {
87        impl ReadRegister for $name {}
88        
89        impl ::protocol1::ReadRegister for $name {
90            fn deserialize(data: &[u8]) -> Self {
91                assert_eq!(data.len(), 1);
92                $name(data[0]&1 == 1)
93            }
94        }
95    };
96    ($name:ident, u8) => {
97        impl ReadRegister for $name {}
98        
99        impl ::protocol1::ReadRegister for $name {
100            fn deserialize(data: &[u8]) -> Self {
101                assert_eq!(data.len(), 1);
102                $name(data[0])
103            }
104        }
105    };
106    ($name:ident, i8) => {
107        impl ReadRegister for $name {}
108        
109        impl ::protocol1::ReadRegister for $name {
110            fn deserialize(data: &[u8]) -> Self {
111                assert_eq!(data.len(), 1);
112                $name(data[0] as i8)
113            }
114        }
115    };
116    ($name:ident, i16) => {
117        impl ReadRegister for $name {}
118        
119        impl ::protocol1::ReadRegister for $name {
120            fn deserialize(data: &[u8]) -> Self {
121                assert_eq!(data.len(), 2);
122                $name(data[0] as i16 | ((data[1] as u16) << 8) as i16)
123            }
124        }
125    };
126    ($name:ident, u16) => {
127        impl ReadRegister for $name {}
128        
129        impl ::protocol1::ReadRegister for $name {
130            fn deserialize(data: &[u8]) -> Self {
131                assert_eq!(data.len(), 2);
132                $name(data[0] as u16 | ((data[1] as u16) << 8))
133            }
134        }
135    };
136    ($name:ident, u32) => {
137        impl ReadRegister for $name {}
138
139        impl ::protocol1::ReadRegister for $name {
140            fn deserialize(data: &[u8]) -> Self {
141                assert_eq!(data.len(), 4);
142                $name((data[0] as u32 | (data[1] as u32) << 8 | (data[2] as u32) << 16 | (data[3] as u32) << 24))
143            }
144        }
145    };
146    ($name:ident, i32) => {
147        impl ReadRegister for $name {}
148        
149        impl ::protocol1::ReadRegister for $name {
150            fn deserialize(data: &[u8]) -> Self {
151                assert_eq!(data.len(), 4);
152                $name((data[0] as u32 | (data[1] as u32) << 8 | (data[2] as u32) << 16 | (data[3] as u32) << 24) as i32)
153            }
154        }
155    };
156}
157
158macro_rules! write_register_impl1{
159    ($name:ident, bool) => {
160        impl WriteRegister for $name {}
161        
162        impl ::protocol1::WriteRegister for $name {
163            fn serialize(&self) -> [u8; 4] {
164                [self.0 as u8, 0, 0, 0]
165            }    
166        }
167    };
168    ($name:ident, u8) => {
169        impl WriteRegister for $name {}
170        
171        impl ::protocol1::WriteRegister for $name {
172            fn serialize(&self) -> [u8; 4] {
173                [self.0, 0, 0, 0]
174            }    
175        }
176    };
177    ($name:ident, i8) => {
178        impl WriteRegister for $name {}
179        
180        impl ::protocol1::WriteRegister for $name {
181            fn serialize(&self) -> [u8; 4] {
182                [self.0 as u8, 0, 0, 0]
183            }    
184        }
185    };
186    ($name:ident, i16) => {
187        impl WriteRegister for $name {}
188        
189        impl ::protocol1::WriteRegister for $name {
190            fn serialize(&self) -> [u8; 4] {
191                [self.0 as u8, (self.0 >> 8) as u8, 0, 0]
192            }    
193        }
194    };
195    ($name:ident, u16) => {
196        impl WriteRegister for $name {}
197        
198        impl ::protocol1::WriteRegister for $name {
199            fn serialize(&self) -> [u8; 4] {
200                [self.0 as u8, (self.0 >> 8) as u8, 0, 0]
201            }    
202        }
203    };
204    ($name:ident, u32) => {
205        impl WriteRegister for $name {}
206        
207        impl ::protocol1::WriteRegister for $name {
208            fn serialize(&self) -> [u8; 4] {
209                [self.0 as u8, (self.0 >> 8) as u8, (self.0 >> 16) as u8, (self.0 >> 24) as u8]
210            }    
211        }
212    };
213    ($name:ident, i32) => {
214        impl WriteRegister for $name {}
215        
216        impl ::protocol1::WriteRegister for $name {
217            fn serialize(&self) -> [u8; 4] {
218                [self.0 as u8, (self.0 >> 8) as u8, (self.0 >> 16) as u8, (self.0 >> 24) as u8]
219            }    
220        }
221    };
222}