forked from cc90202/knx-pico
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpt5.rs
More file actions
407 lines (350 loc) · 11.3 KB
/
dpt5.rs
File metadata and controls
407 lines (350 loc) · 11.3 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! DPT 5.xxx - 8-bit Unsigned Value (1 byte)
//!
//! 8-bit unsigned datapoint types represent values from 0 to 255
//! with different scaling and interpretations.
//!
//! ## Format
//!
//! - 8 bits: unsigned value (0-255)
//!
//! ## Common Subtypes
//!
//! - **5.001** - Percentage (0-100%)
//! - **5.003** - Angle (0-360°)
//! - **5.004** - Percentage 0-255 (0-255)
//! - **5.005** - Ratio (0-255)
//! - **5.006** - Tariff (0-254)
//! - **5.010** - Counter pulses (0-255)
//!
//! ## Example
//!
//! ```rust,no_run
//! use knx_pico::dpt::{Dpt5, DptEncode, DptDecode};
//!
//! let mut buf = [0u8; 1];
//!
//! // Encode percentage (0-100%)
//! let len = Dpt5::Percentage.encode(75, &mut buf)?; // len = 1, buf = [0xBF] = 191
//!
//! // Decode
//! let value = Dpt5::Percentage.decode(&buf[..len])?; // 75
//!
//! // Angle (0-360°)
//! let len = Dpt5::Angle.encode(180, &mut buf)?; // len = 1, buf = [0x80] = 128
//! let angle = Dpt5::Angle.decode(&buf[..len])?; // 180
//! ```
use crate::error::{KnxError, Result};
use crate::dpt::{DptEncode, DptDecode};
/// DPT 5.xxx 8-bit unsigned types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dpt5 {
/// DPT 5.001 - Percentage (0-100%)
Percentage,
/// DPT 5.003 - Angle (0-360°)
Angle,
/// DPT 5.004 - Percentage 0-255 (0-255)
PercentU8,
/// DPT 5.005 - Ratio (0-255)
Ratio,
/// DPT 5.006 - Tariff (0-254)
Tariff,
/// DPT 5.010 - Counter pulses (0-255)
Counter,
}
impl Dpt5 {
/// Get the DPT identifier string (e.g., "5.001")
pub const fn identifier(&self) -> &'static str {
match self {
Dpt5::Percentage => "5.001",
Dpt5::Angle => "5.003",
Dpt5::PercentU8 => "5.004",
Dpt5::Ratio => "5.005",
Dpt5::Tariff => "5.006",
Dpt5::Counter => "5.010",
}
}
/// Get the unit string for this DPT
pub const fn unit(&self) -> &'static str {
match self {
Dpt5::Percentage => "%",
Dpt5::Angle => "°",
Dpt5::PercentU8 => "",
Dpt5::Ratio => "",
Dpt5::Tariff => "",
Dpt5::Counter => "pulses",
}
}
/// Get the valid range for this DPT (min, max)
pub const fn range(&self) -> (u16, u16) {
match self {
Dpt5::Percentage => (0, 100),
Dpt5::Angle => (0, 360),
Dpt5::PercentU8 => (0, 255),
Dpt5::Ratio => (0, 255),
Dpt5::Tariff => (0, 254),
Dpt5::Counter => (0, 255),
}
}
/// Encode a value to raw byte
///
/// For scaled types (Percentage, Angle), this performs the scaling.
#[inline]
fn encode_scaled(&self, value: u16) -> Result<u8> {
let (min, max) = self.range();
if value > max {
return Err(KnxError::dpt_value_out_of_range());
}
let scaled = match self {
Dpt5::Percentage => {
// 0-100% -> 0-255
// scaled = value * 255 / 100
((u32::from(value) * 255) / 100) as u8
}
Dpt5::Angle => {
// 0-360° -> 0-255
// scaled = value * 255 / 360
((u32::from(value) * 255) / 360) as u8
}
// Direct mapping for others
Dpt5::PercentU8 | Dpt5::Ratio | Dpt5::Counter | Dpt5::Tariff => {
if value < min || value > max {
return Err(KnxError::dpt_value_out_of_range());
}
value as u8
}
};
Ok(scaled)
}
/// Decode raw byte to value
///
/// For scaled types (Percentage, Angle), this performs the inverse scaling.
#[inline]
fn decode_scaled(&self, raw: u8) -> Result<u16> {
let value = match self {
Dpt5::Percentage => {
// 0-255 -> 0-100%
// value = raw * 100 / 255
((u32::from(raw) * 100) / 255) as u16
}
Dpt5::Angle => {
// 0-255 -> 0-360°
// value = raw * 360 / 255
((u32::from(raw) * 360) / 255) as u16
}
Dpt5::Tariff => {
// Tariff has max 254
if raw > 254 {
return Err(KnxError::dpt_value_out_of_range());
}
u16::from(raw)
}
// Direct mapping for others
Dpt5::PercentU8 | Dpt5::Ratio | Dpt5::Counter => u16::from(raw),
};
Ok(value)
}
}
impl DptEncode<u16> for Dpt5 {
fn encode(&self, value: u16, buf: &mut [u8]) -> Result<usize> {
if buf.is_empty() {
return Err(KnxError::buffer_too_small());
}
buf[0] = self.encode_scaled(value)?;
Ok(1)
}
}
impl DptDecode<u16> for Dpt5 {
fn decode(&self, data: &[u8]) -> Result<u16> {
if data.is_empty() {
return Err(KnxError::invalid_dpt_data());
}
self.decode_scaled(data[0])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_percentage_encode() {
let mut buf = [0u8; 1];
// 0% -> 0
let len = Dpt5::Percentage.encode(0, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0x00);
// 50% -> 127 (128 would be closer but integer division)
let len = Dpt5::Percentage.encode(50, &mut buf).unwrap();
assert_eq!(len, 1);
assert!(buf[0] >= 127 && buf[0] <= 128);
// 100% -> 255
let len = Dpt5::Percentage.encode(100, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0xFF);
// 75% -> 191
let len = Dpt5::Percentage.encode(75, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0xBF);
}
#[test]
fn test_percentage_decode() {
// 0 -> 0%
assert_eq!(Dpt5::Percentage.decode(&[0x00]).unwrap(), 0);
// 255 -> 100%
assert_eq!(Dpt5::Percentage.decode(&[0xFF]).unwrap(), 100);
// 127 -> ~49%
let val = Dpt5::Percentage.decode(&[127]).unwrap();
assert!(val >= 49 && val <= 50);
// 191 -> ~75%
let val = Dpt5::Percentage.decode(&[0xBF]).unwrap();
assert!(val >= 74 && val <= 75);
}
#[test]
fn test_percentage_out_of_range() {
let mut buf = [0u8; 1];
let result = Dpt5::Percentage.encode(101, &mut buf);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), KnxError::Dpt(_)));
}
#[test]
fn test_angle_encode() {
let mut buf = [0u8; 1];
// 0° -> 0
let len = Dpt5::Angle.encode(0, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0x00);
// 180° -> 127
let len = Dpt5::Angle.encode(180, &mut buf).unwrap();
assert_eq!(len, 1);
assert!(buf[0] >= 127 && buf[0] <= 128);
// 360° -> 255
let len = Dpt5::Angle.encode(360, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0xFF);
}
#[test]
fn test_angle_decode() {
// 0 -> 0°
assert_eq!(Dpt5::Angle.decode(&[0x00]).unwrap(), 0);
// 255 -> 360°
assert_eq!(Dpt5::Angle.decode(&[0xFF]).unwrap(), 360);
// 127 -> ~179°
let val = Dpt5::Angle.decode(&[127]).unwrap();
assert!(val >= 178 && val <= 180);
}
#[test]
fn test_angle_out_of_range() {
let mut buf = [0u8; 1];
let result = Dpt5::Angle.encode(361, &mut buf);
assert!(result.is_err());
}
#[test]
fn test_percent_u8_encode() {
let mut buf = [0u8; 1];
let len = Dpt5::PercentU8.encode(0, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0);
let len = Dpt5::PercentU8.encode(128, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 128);
let len = Dpt5::PercentU8.encode(255, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 255);
}
#[test]
fn test_percent_u8_decode() {
assert_eq!(Dpt5::PercentU8.decode(&[0]).unwrap(), 0);
assert_eq!(Dpt5::PercentU8.decode(&[128]).unwrap(), 128);
assert_eq!(Dpt5::PercentU8.decode(&[255]).unwrap(), 255);
}
#[test]
fn test_tariff_encode() {
let mut buf = [0u8; 1];
let len = Dpt5::Tariff.encode(0, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0);
let len = Dpt5::Tariff.encode(100, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 100);
let len = Dpt5::Tariff.encode(254, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 254);
}
#[test]
fn test_tariff_out_of_range() {
let mut buf = [0u8; 1];
// Tariff max is 254
let result = Dpt5::Tariff.encode(255, &mut buf);
assert!(result.is_err());
}
#[test]
fn test_tariff_decode_invalid() {
// Tariff max is 254
let result = Dpt5::Tariff.decode(&[255]);
assert!(result.is_err());
}
#[test]
fn test_counter_encode() {
let mut buf = [0u8; 1];
let len = Dpt5::Counter.encode(0, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 0);
let len = Dpt5::Counter.encode(42, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 42);
let len = Dpt5::Counter.encode(255, &mut buf).unwrap();
assert_eq!(len, 1);
assert_eq!(buf[0], 255);
}
#[test]
fn test_counter_decode() {
assert_eq!(Dpt5::Counter.decode(&[0]).unwrap(), 0);
assert_eq!(Dpt5::Counter.decode(&[42]).unwrap(), 42);
assert_eq!(Dpt5::Counter.decode(&[255]).unwrap(), 255);
}
#[test]
fn test_decode_empty_data() {
let result = Dpt5::Percentage.decode(&[]);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), KnxError::Dpt(_)));
}
#[test]
fn test_round_trip_percentage() {
let mut buf = [0u8; 1];
for value in [0, 25, 50, 75, 100] {
let len = Dpt5::Percentage.encode(value, &mut buf).unwrap();
assert_eq!(len, 1);
let decoded = Dpt5::Percentage.decode(&buf[..len]).unwrap();
// Allow ±1 error due to rounding
assert!((decoded as i16 - value as i16).abs() <= 1);
}
}
#[test]
fn test_round_trip_angle() {
let mut buf = [0u8; 1];
for value in [0, 90, 180, 270, 360] {
let len = Dpt5::Angle.encode(value, &mut buf).unwrap();
assert_eq!(len, 1);
let decoded = Dpt5::Angle.decode(&buf[..len]).unwrap();
// Allow ±2 error due to rounding
assert!((decoded as i16 - value as i16).abs() <= 2);
}
}
#[test]
fn test_identifier() {
assert_eq!(Dpt5::Percentage.identifier(), "5.001");
assert_eq!(Dpt5::Angle.identifier(), "5.003");
assert_eq!(Dpt5::Counter.identifier(), "5.010");
}
#[test]
fn test_unit() {
assert_eq!(Dpt5::Percentage.unit(), "%");
assert_eq!(Dpt5::Angle.unit(), "°");
assert_eq!(Dpt5::Counter.unit(), "pulses");
}
#[test]
fn test_range() {
assert_eq!(Dpt5::Percentage.range(), (0, 100));
assert_eq!(Dpt5::Angle.range(), (0, 360));
assert_eq!(Dpt5::Tariff.range(), (0, 254));
assert_eq!(Dpt5::Counter.range(), (0, 255));
}
}