embedded_types/can/
mod.rs

1
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub struct BaseID(u16);
4
5impl BaseID {
6    pub fn new(id: u16) -> Self {
7        assert_eq!(id & 0xf800, 0);
8        BaseID(id)
9    }
10}
11
12impl From<BaseID> for u16 {
13    fn from(id: BaseID) -> Self {
14        id.0
15    }
16}
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct ExtendedID(u32);
20
21impl ExtendedID {
22    pub fn new(id: u32) -> Self {
23        assert_eq!(id & 0xe000_0000, 0);
24        ExtendedID(id)
25    }
26}
27
28impl From<ExtendedID> for u32 {
29    fn from(id: ExtendedID) -> Self {
30        id.0
31    }
32}
33
34/// A can ID, can either be Extended (29bit CAN2.0B) or Base (normal 11bit CAN2.0A)
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub enum ID{
37    /// ID for CAN2.0A data frames
38    BaseID(BaseID),
39    
40    /// ID for CAN2.0B data frames
41    ExtendedID(ExtendedID),
42}
43
44impl From<ID> for u32 {
45    fn from(id: ID) -> Self {
46        match id {
47            ID::BaseID(x) => u16::from(x) as u32,
48            ID::ExtendedID(x) => u32::from(x),
49        }
50    }    
51}
52
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub struct BaseDataFrame {
55    id: BaseID,
56    dlc: u8,
57    data: [u8; 8],
58}
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub struct ExtendedDataFrame {
62    id: ExtendedID,
63    dlc: u8,
64    data: [u8; 8],
65}
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
68pub enum DataFrame {
69    /// A CAN2.0A data frame
70    BaseDataFrame(BaseDataFrame),
71    
72    /// A CAN2.0B data frame
73    ExtendedDataFrame(ExtendedDataFrame),
74}    
75
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
77pub struct BaseRemoteFrame {
78    id: BaseID,
79    dlc: u8,
80}
81
82#[derive(Clone, Copy, Debug, PartialEq, Eq)]
83pub struct ExtendedRemoteFrame {
84    id: ExtendedID,
85    dlc: u8,
86}
87
88pub enum RemoteFrame {
89    /// A CAN2.0A remote frame
90    BaseRemoteFrame(BaseRemoteFrame),
91    
92    /// A CAN2.0B remote frame
93    ExtendedRemoteFrame(ExtendedRemoteFrame),
94}
95
96
97impl ExtendedDataFrame {
98    pub fn new(id: ExtendedID) -> Self {
99        Self{id: id, dlc: 0, data: [0; 8]}
100    }
101    
102    pub fn set_data_length(&mut self, length: usize) {
103        assert!(length <= 8);
104        self.dlc = length as u8;
105    }
106    
107    pub fn data(&self) -> &[u8] {
108        &self.data[0..(self.dlc as usize)]
109    }
110    
111    pub fn data_as_mut(&mut self) -> &mut[u8] {
112        &mut self.data[0..(self.dlc as usize)]
113    }
114    
115    pub fn id(&self) -> ExtendedID {
116        self.id 
117    }
118}
119
120impl BaseDataFrame {
121    pub fn new(id: BaseID) -> Self {
122        Self{id: id, dlc: 0, data: [0; 8]}
123    }
124    
125    pub fn set_data_length(&mut self, length: usize) {
126        assert!(length <= 8);
127        self.dlc = length as u8;
128    }
129    
130    pub fn data(&self) -> &[u8] {
131        &self.data[0..(self.dlc as usize)]
132    }
133    
134    pub fn data_as_mut(&mut self) -> &mut[u8] {
135        &mut self.data[0..(self.dlc as usize)]
136    }
137    
138    pub fn id(&self) -> BaseID {
139        self.id 
140    }
141}
142
143impl DataFrame {
144    pub fn new(id: ID) -> Self {
145        match id {
146            ID::BaseID(id) => DataFrame::BaseDataFrame(BaseDataFrame::new(id)),
147            ID::ExtendedID(id) => DataFrame::ExtendedDataFrame(ExtendedDataFrame::new(id)),
148        }
149    }
150    
151    pub fn set_data_length(&mut self, length: usize) {
152        match *self {
153            DataFrame::BaseDataFrame(ref mut f) => f.set_data_length(length),
154            DataFrame::ExtendedDataFrame(ref mut f) => f.set_data_length(length),
155        }
156    }
157    
158    pub fn data(&self) -> &[u8] {
159        match *self {
160            DataFrame::BaseDataFrame(ref f) => f.data(),
161            DataFrame::ExtendedDataFrame(ref f) => f.data(),
162        }
163    }
164    
165    pub fn data_as_mut(&mut self) -> &mut[u8] {
166        match *self {
167            DataFrame::BaseDataFrame(ref mut f) => f.data_as_mut(),
168            DataFrame::ExtendedDataFrame(ref mut f) => f.data_as_mut(),
169        }
170    }
171     
172    pub fn id(&self) -> ID {
173        match *self {
174            DataFrame::BaseDataFrame(f) => ID::BaseID(f.id()),
175            DataFrame::ExtendedDataFrame(f) => ID::ExtendedID(f.id()),
176        }
177    }
178}
179
180impl BaseRemoteFrame {
181    pub fn new(id: BaseID) -> Self {
182        Self{id: id, dlc: 0}
183    }
184    
185    pub fn set_data_length(&mut self, length: usize) {
186        assert!(length <= 8);
187        self.dlc = length as u8;
188    }
189        
190    pub fn id(&self) -> BaseID {
191        self.id 
192    }
193}
194
195impl ExtendedRemoteFrame {
196    pub fn new(id: ExtendedID) -> Self {
197        Self{id: id, dlc: 0}
198    }
199    
200    pub fn set_data_length(&mut self, length: usize) {
201        assert!(length <= 8);
202        self.dlc = length as u8;
203    }
204        
205    pub fn id(&self) -> ExtendedID {
206        self.id 
207    }
208}
209
210impl RemoteFrame {
211    pub fn new(id: ID) -> Self {
212        match id {
213            ID::BaseID(id) => RemoteFrame::BaseRemoteFrame(BaseRemoteFrame::new(id)),
214            ID::ExtendedID(id) => RemoteFrame::ExtendedRemoteFrame(ExtendedRemoteFrame::new(id)),
215        }
216    }
217    
218    pub fn id(&self) -> ID {
219        match *self {
220            RemoteFrame::BaseRemoteFrame(f) => ID::BaseID(f.id()),
221            RemoteFrame::ExtendedRemoteFrame(f) => ID::ExtendedID(f.id()),
222        }
223    }
224}
225
226pub enum CanFrame {
227    DataFrame(DataFrame),
228    RemoteFrame(RemoteFrame),
229}
230
231impl CanFrame {
232    pub fn id(&self) -> ID {
233        match *self {
234            CanFrame::DataFrame(ref f) => f.id(),
235            CanFrame::RemoteFrame(ref f) => f.id(),
236        }
237    }
238}
239
240
241// Conversion between these types
242
243impl From<DataFrame> for CanFrame {
244    fn from(f: DataFrame) -> CanFrame {
245        CanFrame::DataFrame(f)
246    }
247}
248
249impl From<RemoteFrame> for CanFrame {
250    fn from(f: RemoteFrame) -> CanFrame {
251        CanFrame::RemoteFrame(f)
252    }
253}
254
255impl From<BaseDataFrame> for DataFrame {
256    fn from(f: BaseDataFrame) -> DataFrame {
257        DataFrame::BaseDataFrame(f)
258    }
259}
260
261impl From<ExtendedDataFrame> for DataFrame {
262    fn from(f: ExtendedDataFrame) -> DataFrame {
263        DataFrame::ExtendedDataFrame(f)
264    }
265}
266
267impl From<BaseRemoteFrame> for RemoteFrame {
268    fn from(f: BaseRemoteFrame) -> RemoteFrame {
269        RemoteFrame::BaseRemoteFrame(f)
270    }
271}
272
273impl From<ExtendedRemoteFrame> for RemoteFrame {
274    fn from(f: ExtendedRemoteFrame) -> RemoteFrame {
275        RemoteFrame::ExtendedRemoteFrame(f)
276    }
277}
278
279impl From<BaseDataFrame> for CanFrame {
280    fn from(f: BaseDataFrame) -> CanFrame {
281        CanFrame::from(DataFrame::from(f))
282    }
283}
284
285impl From<ExtendedDataFrame> for CanFrame {
286    fn from(f: ExtendedDataFrame) -> CanFrame {
287        CanFrame::from(DataFrame::from(f))
288    }
289}
290
291impl From<BaseRemoteFrame> for CanFrame {
292    fn from(f: BaseRemoteFrame) -> CanFrame {
293        CanFrame::from(RemoteFrame::from(f))
294    }
295}
296
297impl From<ExtendedRemoteFrame> for CanFrame {
298    fn from(f: ExtendedRemoteFrame) -> CanFrame {
299        CanFrame::from(RemoteFrame::from(f))
300    }
301}