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 = 8,
135 VisibleString = 9,
137 OctetString = 0xa,
139 UnicodeString = 0xb,
141 TimeOfDay = 0xc,
143 TimeDifference = 0xd,
145 Domain = 0xf,
148 Real64 = 0x11,
150 Int64 = 0x15,
152 UInt64 = 0x1b,
154 Other(u16),
156}
157
158impl From<u16> for DataType {
159 fn from(value: u16) -> Self {
160 use DataType::*;
161 match value {
162 1 => Boolean,
163 2 => Int8,
164 3 => Int16,
165 4 => Int32,
166 5 => UInt8,
167 6 => UInt16,
168 7 => UInt32,
169 8 => Real32,
170 9 => VisibleString,
171 0xa => OctetString,
172 0xb => UnicodeString,
173 0xf => Domain,
174 _ => Other(value),
175 }
176 }
177}
178
179impl DataType {
180 pub fn is_str(&self) -> bool {
182 matches!(
183 self,
184 Self::VisibleString | Self::OctetString | Self::UnicodeString
185 )
186 }
187}
188
189#[derive(Clone, Copy, Debug, Default, PartialEq)]
191pub struct SubInfo {
192 pub size: usize,
194 pub data_type: DataType,
196 pub access_type: AccessType,
198 pub pdo_mapping: PdoMappable,
200 pub persist: bool,
202}
203
204impl SubInfo {
205 pub const MAX_SUB_NUMBER: SubInfo = SubInfo {
207 size: 1,
208 data_type: DataType::UInt8,
209 access_type: AccessType::Const,
210 pdo_mapping: PdoMappable::None,
211 persist: false,
212 };
213
214 pub const fn new_u32() -> Self {
216 Self {
217 size: 4,
218 data_type: DataType::UInt32,
219 access_type: AccessType::Ro,
220 pdo_mapping: PdoMappable::None,
221 persist: false,
222 }
223 }
224
225 pub const fn new_u16() -> Self {
227 Self {
228 size: 2,
229 data_type: DataType::UInt16,
230 access_type: AccessType::Ro,
231 pdo_mapping: PdoMappable::None,
232 persist: false,
233 }
234 }
235
236 pub const fn new_u8() -> Self {
238 Self {
239 size: 1,
240 data_type: DataType::UInt8,
241 access_type: AccessType::Ro,
242 pdo_mapping: PdoMappable::None,
243 persist: false,
244 }
245 }
246
247 pub const fn new_i32() -> Self {
249 Self {
250 size: 4,
251 data_type: DataType::Int32,
252 access_type: AccessType::Ro,
253 pdo_mapping: PdoMappable::None,
254 persist: false,
255 }
256 }
257
258 pub const fn new_i16() -> Self {
260 Self {
261 size: 2,
262 data_type: DataType::Int16,
263 access_type: AccessType::Ro,
264 pdo_mapping: PdoMappable::None,
265 persist: false,
266 }
267 }
268
269 pub const fn new_i8() -> Self {
271 Self {
272 size: 1,
273 data_type: DataType::Int8,
274 access_type: AccessType::Ro,
275 pdo_mapping: PdoMappable::None,
276 persist: false,
277 }
278 }
279
280 pub const fn new_f32() -> Self {
282 Self {
283 size: 4,
284 data_type: DataType::Real32,
285 access_type: AccessType::Ro,
286 pdo_mapping: PdoMappable::None,
287 persist: false,
288 }
289 }
290
291 pub const fn new_visibile_str(size: usize) -> Self {
293 Self {
294 size,
295 data_type: DataType::VisibleString,
296 access_type: AccessType::Ro,
297 pdo_mapping: PdoMappable::None,
298 persist: false,
299 }
300 }
301
302 pub const fn ro_access(mut self) -> Self {
304 self.access_type = AccessType::Ro;
305 self
306 }
307
308 pub const fn rw_access(mut self) -> Self {
310 self.access_type = AccessType::Rw;
311 self
312 }
313
314 pub const fn const_access(mut self) -> Self {
316 self.access_type = AccessType::Const;
317 self
318 }
319
320 pub const fn wo_access(mut self) -> Self {
322 self.access_type = AccessType::Wo;
323 self
324 }
325
326 pub const fn persist(mut self, value: bool) -> Self {
328 self.persist = value;
329 self
330 }
331}