1#[derive(Clone, Copy, Debug, PartialEq)]
6pub struct ObjectId {
7 pub index: u16,
9 pub sub: u8,
11}
12
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17#[repr(u8)]
18pub enum ObjectCode {
19 Null = 0,
23 Domain = 2,
27 DefType = 5,
29 DefStruct = 6,
31 #[default]
33 Var = 7,
34 Array = 8,
36 Record = 9,
38}
39
40impl TryFrom<u8> for ObjectCode {
41 type Error = ();
42
43 fn try_from(value: u8) -> Result<Self, Self::Error> {
44 match value {
45 0 => Ok(ObjectCode::Null),
46 2 => Ok(ObjectCode::Domain),
47 5 => Ok(ObjectCode::DefType),
48 6 => Ok(ObjectCode::DefStruct),
49 7 => Ok(ObjectCode::Var),
50 8 => Ok(ObjectCode::Array),
51 9 => Ok(ObjectCode::Record),
52 _ => Err(()),
53 }
54 }
55}
56
57#[derive(Copy, Clone, Debug, Default, PartialEq)]
59pub enum AccessType {
60 #[default]
62 Ro,
63 Wo,
65 Rw,
67 Const,
69}
70
71impl AccessType {
72 pub fn is_readable(&self) -> bool {
74 matches!(self, AccessType::Ro | AccessType::Rw | AccessType::Const)
75 }
76
77 pub fn is_writable(&self) -> bool {
79 matches!(self, AccessType::Rw | AccessType::Wo)
80 }
81}
82
83#[derive(Copy, Clone, Debug, Default, PartialEq)]
85#[cfg_attr(
86 feature = "std",
87 derive(serde::Deserialize),
88 serde(rename_all = "lowercase")
89)]
90pub enum PdoMappable {
91 #[default]
93 None,
94 Rpdo,
96 Tpdo,
98 Both,
100}
101
102impl PdoMappable {
103 pub fn supports_tpdo(&self) -> bool {
105 matches!(self, PdoMappable::Tpdo | PdoMappable::Both)
106 }
107
108 pub fn supports_rpdo(&self) -> bool {
110 matches!(self, PdoMappable::Rpdo | PdoMappable::Both)
111 }
112}
113
114#[derive(Copy, Clone, Debug, Default, PartialEq)]
116#[repr(u16)]
117pub enum DataType {
118 Boolean = 1,
120 #[default]
121 Int8 = 2,
123 Int16 = 3,
125 Int32 = 4,
127 UInt8 = 5,
129 UInt16 = 6,
131 UInt32 = 7,
133 Real32 = 0x8,
135 VisibleString = 0x9,
137 OctetString = 0xa,
139 UnicodeString = 0xb,
141 TimeOfDay = 0xc,
143 TimeDifference = 0xd,
145 Domain = 0xf,
148 Int24 = 0x10,
150 Real64 = 0x11,
152 Int64 = 0x15,
154 UInt24 = 0x16,
156 UInt64 = 0x1b,
158 Other(u16),
160}
161
162impl From<u16> for DataType {
163 fn from(value: u16) -> Self {
164 use DataType::*;
165 match value {
166 1 => Boolean,
167 2 => Int8,
168 3 => Int16,
169 4 => Int32,
170 5 => UInt8,
171 6 => UInt16,
172 7 => UInt32,
173 8 => Real32,
174 9 => VisibleString,
175 0xa => OctetString,
176 0xb => UnicodeString,
177 0xf => Domain,
178 0x10 => Int24,
179 0x11 => Real64,
180 0x15 => Int64,
181 0x16 => UInt24,
182 0x1b => UInt64,
183 _ => Other(value),
184 }
185 }
186}
187
188impl DataType {
189 pub fn is_str(&self) -> bool {
191 matches!(
192 self,
193 Self::VisibleString | Self::OctetString | Self::UnicodeString
194 )
195 }
196}
197
198#[derive(Clone, Copy, Debug, Default, PartialEq)]
200pub struct SubInfo {
201 pub size: usize,
203 pub data_type: DataType,
205 pub access_type: AccessType,
207 pub pdo_mapping: PdoMappable,
209 pub persist: bool,
211}
212
213impl SubInfo {
214 pub const MAX_SUB_NUMBER: SubInfo = SubInfo {
216 size: 1,
217 data_type: DataType::UInt8,
218 access_type: AccessType::Const,
219 pdo_mapping: PdoMappable::None,
220 persist: false,
221 };
222
223 pub const fn new_u32() -> Self {
225 Self {
226 size: 4,
227 data_type: DataType::UInt32,
228 access_type: AccessType::Ro,
229 pdo_mapping: PdoMappable::None,
230 persist: false,
231 }
232 }
233
234 pub const fn new_u24() -> Self {
236 Self {
237 size: 3,
238 data_type: DataType::UInt24,
239 access_type: AccessType::Ro,
240 pdo_mapping: PdoMappable::None,
241 persist: false,
242 }
243 }
244
245 pub const fn new_u16() -> Self {
247 Self {
248 size: 2,
249 data_type: DataType::UInt16,
250 access_type: AccessType::Ro,
251 pdo_mapping: PdoMappable::None,
252 persist: false,
253 }
254 }
255
256 pub const fn new_u8() -> Self {
258 Self {
259 size: 1,
260 data_type: DataType::UInt8,
261 access_type: AccessType::Ro,
262 pdo_mapping: PdoMappable::None,
263 persist: false,
264 }
265 }
266
267 pub const fn new_i32() -> Self {
269 Self {
270 size: 4,
271 data_type: DataType::Int32,
272 access_type: AccessType::Ro,
273 pdo_mapping: PdoMappable::None,
274 persist: false,
275 }
276 }
277
278 pub const fn new_i24() -> Self {
280 Self {
281 size: 3,
282 data_type: DataType::Int24,
283 access_type: AccessType::Ro,
284 pdo_mapping: PdoMappable::None,
285 persist: false,
286 }
287 }
288
289 pub const fn new_i16() -> Self {
291 Self {
292 size: 2,
293 data_type: DataType::Int16,
294 access_type: AccessType::Ro,
295 pdo_mapping: PdoMappable::None,
296 persist: false,
297 }
298 }
299
300 pub const fn new_i8() -> Self {
302 Self {
303 size: 1,
304 data_type: DataType::Int8,
305 access_type: AccessType::Ro,
306 pdo_mapping: PdoMappable::None,
307 persist: false,
308 }
309 }
310
311 pub const fn new_f32() -> Self {
313 Self {
314 size: 4,
315 data_type: DataType::Real32,
316 access_type: AccessType::Ro,
317 pdo_mapping: PdoMappable::None,
318 persist: false,
319 }
320 }
321
322 pub const fn new_visibile_str(size: usize) -> Self {
324 Self {
325 size,
326 data_type: DataType::VisibleString,
327 access_type: AccessType::Ro,
328 pdo_mapping: PdoMappable::None,
329 persist: false,
330 }
331 }
332
333 pub const fn ro_access(mut self) -> Self {
335 self.access_type = AccessType::Ro;
336 self
337 }
338
339 pub const fn rw_access(mut self) -> Self {
341 self.access_type = AccessType::Rw;
342 self
343 }
344
345 pub const fn const_access(mut self) -> Self {
347 self.access_type = AccessType::Const;
348 self
349 }
350
351 pub const fn wo_access(mut self) -> Self {
353 self.access_type = AccessType::Wo;
354 self
355 }
356
357 pub const fn persist(mut self, value: bool) -> Self {
359 self.persist = value;
360 self
361 }
362}