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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Object Definitions
//!
/// A container for the address of a subobject
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ObjectId {
/// Object index
pub index: u16,
/// Sub index
pub sub: u8,
}
/// Object Code value
///
/// Defines the type of an object or sub object
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
pub enum ObjectCode {
/// An empty object
///
/// Zencan does not support Null objects
Null = 0,
/// A large chunk of data
///
/// Zencan does not support Domain Object; it only supports domain sub-objects.
Domain = 2,
/// Unused
DefType = 5,
/// Unused
DefStruct = 6,
/// An object which has a single sub object
#[default]
Var = 7,
/// An array of sub-objects all with the same data type
Array = 8,
/// A collection of sub-objects with varying types
Record = 9,
}
impl TryFrom<u8> for ObjectCode {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(ObjectCode::Null),
2 => Ok(ObjectCode::Domain),
5 => Ok(ObjectCode::DefType),
6 => Ok(ObjectCode::DefStruct),
7 => Ok(ObjectCode::Var),
8 => Ok(ObjectCode::Array),
9 => Ok(ObjectCode::Record),
_ => Err(()),
}
}
}
/// Access type enum
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum AccessType {
/// Read-only
#[default]
Ro,
/// Write-only
Wo,
/// Read-write
Rw,
/// Read-only, and also will never be changed, even internally by the device
Const,
}
impl AccessType {
/// Returns true if an object with this access type can be read
pub fn is_readable(&self) -> bool {
matches!(self, AccessType::Ro | AccessType::Rw | AccessType::Const)
}
/// Returns true if an object with this access type can be written
pub fn is_writable(&self) -> bool {
matches!(self, AccessType::Rw | AccessType::Wo)
}
}
/// Possible PDO mapping values for an object
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[cfg_attr(
feature = "std",
derive(serde::Deserialize),
serde(rename_all = "lowercase")
)]
pub enum PdoMappable {
/// Object cannot be mapped to PDOs
#[default]
None,
/// Object can be mapped to RPDOs only
Rpdo,
/// Object can be mapped to TPDOs only
Tpdo,
/// Object can be mapped to both RPDOs and TPDOs
Both,
}
impl PdoMappable {
/// Can be mapped to a TPDO
pub fn supports_tpdo(&self) -> bool {
matches!(self, PdoMappable::Tpdo | PdoMappable::Both)
}
/// Can be mapped to an RPDO
pub fn supports_rpdo(&self) -> bool {
matches!(self, PdoMappable::Rpdo | PdoMappable::Both)
}
}
/// Indicate the type of data stored in an object
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[repr(u16)]
pub enum DataType {
/// A true false value, encoded as a single byte, with 0 for false and 1 for true
Boolean = 1,
#[default]
/// A signed 8-bit integer
Int8 = 2,
/// A signed 16-bit integer
Int16 = 3,
/// A signed 32-bit integer
Int32 = 4,
/// An unsigned 8-bit integer
UInt8 = 5,
/// An unsigned 16-bit integer
UInt16 = 6,
/// An unsigned 32-bit integer
UInt32 = 7,
/// A 32-bit floating point value
Real32 = 0x8,
/// An ASCII/utf-8 string
VisibleString = 0x9,
/// A byte string
OctetString = 0xa,
/// A unicode string
UnicodeString = 0xb,
/// Currently Unimplemented
TimeOfDay = 0xc,
/// Currently Unimplemented
TimeDifference = 0xd,
/// An arbitrary byte access type for e.g. data streams, or large chunks of
/// data. Size is typically not known at build time.
Domain = 0xf,
/// A signed 24-bit integer
Int24 = 0x10,
/// A 64-bit floating point value
Real64 = 0x11,
/// A signed 64-bit integer
Int64 = 0x15,
/// An unsigned 24-bit integer
UInt24 = 0x16,
/// An unsigned 64-bit integer
UInt64 = 0x1b,
/// A contained for an unrecognized data type value
Other(u16),
}
impl From<u16> for DataType {
fn from(value: u16) -> Self {
use DataType::*;
match value {
1 => Boolean,
2 => Int8,
3 => Int16,
4 => Int32,
5 => UInt8,
6 => UInt16,
7 => UInt32,
8 => Real32,
9 => VisibleString,
0xa => OctetString,
0xb => UnicodeString,
0xf => Domain,
0x10 => Int24,
0x11 => Real64,
0x15 => Int64,
0x16 => UInt24,
0x1b => UInt64,
_ => Other(value),
}
}
}
impl DataType {
/// Returns true if data type is one of the string types
pub fn is_str(&self) -> bool {
matches!(
self,
Self::VisibleString | Self::OctetString | Self::UnicodeString
)
}
}
/// Information about a sub object
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SubInfo {
/// The size (or max size) of this sub object, in bytes
pub size: usize,
/// The data type of this sub object
pub data_type: DataType,
/// Indicates what accesses (i.e. read/write) are allowed on this sub object
pub access_type: AccessType,
/// Indicates whether this sub may be mapped to PDOs
pub pdo_mapping: PdoMappable,
/// Indicates whether this sub should be persisted when data is saved
pub persist: bool,
}
impl SubInfo {
/// A shorthand value for sub0 on record and array objects
pub const MAX_SUB_NUMBER: SubInfo = SubInfo {
size: 1,
data_type: DataType::UInt8,
access_type: AccessType::Const,
pdo_mapping: PdoMappable::None,
persist: false,
};
/// Convenience function for creating a new sub-info by type
pub const fn new_u32() -> Self {
Self {
size: 4,
data_type: DataType::UInt32,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_u24() -> Self {
Self {
size: 3,
data_type: DataType::UInt24,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_u16() -> Self {
Self {
size: 2,
data_type: DataType::UInt16,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_u8() -> Self {
Self {
size: 1,
data_type: DataType::UInt8,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_i32() -> Self {
Self {
size: 4,
data_type: DataType::Int32,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_i24() -> Self {
Self {
size: 3,
data_type: DataType::Int24,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_i16() -> Self {
Self {
size: 2,
data_type: DataType::Int16,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_i8() -> Self {
Self {
size: 1,
data_type: DataType::Int8,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_f32() -> Self {
Self {
size: 4,
data_type: DataType::Real32,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function for creating a new sub-info by type
pub const fn new_visibile_str(size: usize) -> Self {
Self {
size,
data_type: DataType::VisibleString,
access_type: AccessType::Ro,
pdo_mapping: PdoMappable::None,
persist: false,
}
}
/// Convenience function to set the access_type to read-only
pub const fn ro_access(mut self) -> Self {
self.access_type = AccessType::Ro;
self
}
/// Convenience function to set the access_type to read-write
pub const fn rw_access(mut self) -> Self {
self.access_type = AccessType::Rw;
self
}
/// Convenience function to set the access_type to const
pub const fn const_access(mut self) -> Self {
self.access_type = AccessType::Const;
self
}
/// Convenience function to set the access_type to write-only
pub const fn wo_access(mut self) -> Self {
self.access_type = AccessType::Wo;
self
}
/// Convenience function to set the persist value
pub const fn persist(mut self, value: bool) -> Self {
self.persist = value;
self
}
}