]>
Commit | Line | Data |
---|---|---|
eed3dec8 MG |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
6 | * Copyright (C) 2015 Matthieu Gaillet <matthieu@gaillet.be> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
2ea1fdf1 | 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
eed3dec8 MG |
20 | */ |
21 | ||
22 | /* | |
23 | * Dream Tech International DTM0660 protocol parser. | |
24 | * | |
25 | * 6000 counts (5 5/6 digits) | |
26 | * | |
27 | * - Package: QFP-64 | |
28 | * - Communication parameters: Unidirectional, 2400/8n1 | |
29 | * - The protocol is similar to FS9721 but with 15 bytes and reversed nibbles. | |
30 | */ | |
31 | ||
32 | #include <config.h> | |
33 | #include <string.h> | |
34 | #include <ctype.h> | |
35 | #include <math.h> | |
36 | #include <glib.h> | |
37 | #include <libsigrok/libsigrok.h> | |
38 | #include "libsigrok-internal.h" | |
39 | ||
40 | #define LOG_PREFIX "dtm0660" | |
41 | ||
42 | static int parse_digit(uint8_t b) | |
43 | { | |
44 | switch (b) { | |
45 | case 0xeb: | |
46 | return 0; | |
47 | case 0x0a: | |
48 | return 1; | |
49 | case 0xad: | |
50 | return 2; | |
51 | case 0x8f: | |
52 | return 3; | |
53 | case 0x4e: | |
54 | return 4; | |
55 | case 0xc7: | |
56 | return 5; | |
57 | case 0xe7: | |
58 | return 6; | |
59 | case 0x8a: | |
60 | return 7; | |
61 | case 0xef: | |
62 | return 8; | |
63 | case 0xcf: | |
64 | return 9; | |
65 | default: | |
66 | sr_dbg("Invalid digit byte: 0x%02x.", b); | |
67 | return -1; | |
68 | } | |
69 | } | |
70 | ||
71 | static gboolean sync_nibbles_valid(const uint8_t *buf) | |
72 | { | |
73 | int i; | |
74 | ||
75 | /* Check the synchronization nibbles, and make sure they all match. */ | |
76 | for (i = 0; i < DTM0660_PACKET_SIZE; i++) { | |
77 | if (((buf[i] >> 4) & 0x0f) != (i + 1)) { | |
78 | sr_dbg("Sync nibble in byte %d (0x%02x) is invalid.", | |
79 | i, buf[i]); | |
80 | return FALSE; | |
81 | } | |
82 | } | |
83 | ||
84 | return TRUE; | |
85 | } | |
86 | ||
87 | static gboolean flags_valid(const struct dtm0660_info *info) | |
88 | { | |
89 | int count; | |
90 | ||
91 | /* Does the packet have more than one multiplier? */ | |
92 | count = 0; | |
93 | count += (info->is_nano) ? 1 : 0; | |
94 | count += (info->is_micro) ? 1 : 0; | |
95 | count += (info->is_milli) ? 1 : 0; | |
96 | count += (info->is_kilo) ? 1 : 0; | |
97 | count += (info->is_mega) ? 1 : 0; | |
98 | if (count > 1) { | |
99 | sr_dbg("More than one multiplier detected in packet."); | |
100 | return FALSE; | |
101 | } | |
102 | ||
103 | /* Does the packet "measure" more than one type of value? */ | |
104 | count = 0; | |
105 | count += (info->is_hz) ? 1 : 0; | |
106 | count += (info->is_ohm) ? 1 : 0; | |
107 | count += (info->is_farad) ? 1 : 0; | |
108 | count += (info->is_ampere) ? 1 : 0; | |
109 | count += (info->is_volt) ? 1 : 0; | |
110 | count += (info->is_percent) ? 1 : 0; | |
111 | if (count > 1) { | |
112 | sr_dbg("More than one measurement type detected in packet."); | |
113 | return FALSE; | |
114 | } | |
115 | ||
116 | /* Both AC and DC set? */ | |
117 | if (info->is_ac && info->is_dc) { | |
118 | sr_dbg("Both AC and DC flags detected in packet."); | |
119 | return FALSE; | |
120 | } | |
121 | ||
122 | /* RS232 flag not set? */ | |
123 | if (!info->is_rs232) { | |
124 | sr_dbg("No RS232 flag detected in packet."); | |
125 | return FALSE; | |
126 | } | |
127 | ||
128 | return TRUE; | |
129 | } | |
130 | ||
427015d7 | 131 | static int parse_value(const uint8_t *buf, float *result, int *exponent) |
eed3dec8 MG |
132 | { |
133 | int i, sign, intval = 0, digits[4]; | |
134 | uint8_t digit_bytes[4]; | |
135 | float floatval; | |
136 | ||
137 | /* Byte 1 contains sign in bit 0. */ | |
138 | sign = ((buf[1] & (1 << 0)) != 0) ? -1 : 1; | |
139 | ||
140 | /* | |
141 | * Bytes 1-8: Value (4 decimal digits, sign, decimal point) | |
142 | * | |
143 | * Over limit: "0L" (LCD), 0x00 0xeb 0x61 0x00 (digit bytes). | |
144 | */ | |
145 | ||
146 | /* Merge the two nibbles for a digit into one byte. */ | |
147 | for (i = 0; i < 4; i++) { | |
148 | digit_bytes[i] = ((buf[1 + (i * 2)] & 0x0f) << 4); | |
149 | digit_bytes[i] |= (buf[1 + (i * 2) + 1] & 0x0f); | |
150 | ||
151 | /* Bit 4 in the byte is not part of the digit. */ | |
152 | digit_bytes[i] &= ~(1 << 4); | |
153 | } | |
154 | ||
155 | /* Check for "OL". */ | |
156 | if (digit_bytes[0] == 0x00 && digit_bytes[1] == 0xeb && | |
157 | digit_bytes[2] == 0x61 && digit_bytes[3] == 0x00) { | |
158 | sr_spew("Over limit."); | |
159 | *result = INFINITY; | |
160 | return SR_OK; | |
161 | } | |
162 | ||
163 | /* Parse the digits. */ | |
164 | for (i = 0; i < 4; i++) | |
165 | digits[i] = parse_digit(digit_bytes[i]); | |
166 | sr_spew("Digits: %02x %02x %02x %02x (%d%d%d%d).", | |
167 | digit_bytes[0], digit_bytes[1], digit_bytes[2], digit_bytes[3], | |
168 | digits[0], digits[1], digits[2], digits[3]); | |
169 | ||
170 | /* Merge all digits into an integer value. */ | |
171 | for (i = 0; i < 4; i++) { | |
172 | intval *= 10; | |
173 | intval += digits[i]; | |
174 | } | |
175 | ||
176 | floatval = (float)intval; | |
177 | ||
178 | /* Decimal point position. */ | |
179 | if ((buf[3] & 0x01) != 0) { | |
180 | floatval /= 1000; | |
427015d7 | 181 | *exponent = -3; |
eed3dec8 MG |
182 | sr_spew("Decimal point after first digit."); |
183 | } else if ((buf[5] & 0x01) != 0) { | |
184 | floatval /= 100; | |
427015d7 | 185 | *exponent = -2; |
eed3dec8 MG |
186 | sr_spew("Decimal point after second digit."); |
187 | } else if ((buf[7] & 0x01) != 0) { | |
188 | floatval /= 10; | |
427015d7 | 189 | *exponent = -1; |
eed3dec8 MG |
190 | sr_spew("Decimal point after third digit."); |
191 | } else { | |
427015d7 | 192 | *exponent = 0; |
eed3dec8 MG |
193 | sr_spew("No decimal point in the number."); |
194 | } | |
195 | ||
196 | /* Apply sign. */ | |
197 | floatval *= sign; | |
198 | ||
199 | sr_spew("The display value is %f.", floatval); | |
200 | ||
201 | *result = floatval; | |
202 | ||
203 | return SR_OK; | |
204 | } | |
205 | ||
206 | static void parse_flags(const uint8_t *buf, struct dtm0660_info *info) | |
207 | { | |
208 | /* Byte 0: LCD SEG1 */ | |
209 | info->is_ac = (buf[0] & (1 << 0)) != 0; | |
210 | info->is_dc = (buf[0] & (1 << 1)) != 0; | |
211 | info->is_auto = (buf[0] & (1 << 2)) != 0; | |
212 | info->is_rs232 = (buf[0] & (1 << 3)) != 0; | |
213 | ||
214 | /* Byte 1: LCD SEG2 */ | |
215 | info->is_sign = (buf[1] & (1 << 0)) != 0; | |
216 | ||
217 | /* Byte 9: LCD SEG10 */ | |
218 | info->is_micro = (buf[9] & (1 << 0)) != 0; | |
219 | info->is_nano = (buf[9] & (1 << 1)) != 0; | |
220 | info->is_kilo = (buf[9] & (1 << 2)) != 0; | |
221 | info->is_diode = (buf[9] & (1 << 3)) != 0; | |
222 | ||
223 | /* Byte 10: LCD SEG11 */ | |
224 | info->is_milli = (buf[10] & (1 << 0)) != 0; | |
225 | info->is_percent = (buf[10] & (1 << 1)) != 0; | |
226 | info->is_mega = (buf[10] & (1 << 2)) != 0; | |
227 | info->is_beep = (buf[10] & (1 << 3)) != 0; | |
228 | ||
229 | /* Byte 11: LCD SEG12 */ | |
230 | info->is_farad = (buf[11] & (1 << 0)) != 0; | |
231 | info->is_ohm = (buf[11] & (1 << 1)) != 0; | |
232 | info->is_rel = (buf[11] & (1 << 2)) != 0; | |
233 | info->is_hold = (buf[11] & (1 << 3)) != 0; | |
234 | ||
235 | /* Byte 12: LCD SEG13 */ | |
236 | info->is_ampere = (buf[12] & (1 << 0)) != 0; | |
237 | info->is_volt = (buf[12] & (1 << 1)) != 0; | |
238 | info->is_hz = (buf[12] & (1 << 2)) != 0; | |
239 | info->is_bat = (buf[12] & (1 << 3)) != 0; | |
240 | ||
241 | /* Byte 13: LCD SEG14 */ | |
242 | info->is_degf = (buf[13] & (1 << 0)) != 0; | |
243 | info->is_degc = (buf[13] & (1 << 1)) != 0; | |
244 | info->is_c2c1_00 = (buf[13] & (1 << 2)) != 0; | |
245 | info->is_c2c1_01 = (buf[13] & (1 << 3)) != 0; | |
246 | ||
247 | /* Byte 14: LCD SEG15 */ | |
248 | info->is_apo = (buf[14] & (1 << 0)) != 0; | |
249 | info->is_min = (buf[14] & (1 << 1)) != 0; | |
250 | info->is_minmax = (buf[14] & (1 << 2)) != 0; | |
251 | info->is_max = (buf[14] & (1 << 3)) != 0; | |
252 | } | |
253 | ||
b02bb45f | 254 | static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, |
427015d7 | 255 | int *exponent, const struct dtm0660_info *info) |
eed3dec8 MG |
256 | { |
257 | /* Factors */ | |
258 | if (info->is_nano) | |
427015d7 | 259 | *exponent -= 9; |
eed3dec8 | 260 | if (info->is_micro) |
427015d7 | 261 | *exponent -= 6; |
eed3dec8 | 262 | if (info->is_milli) |
427015d7 | 263 | *exponent -= 3; |
eed3dec8 | 264 | if (info->is_kilo) |
427015d7 | 265 | *exponent += 3; |
eed3dec8 | 266 | if (info->is_mega) |
427015d7 AJ |
267 | *exponent += 6; |
268 | *floatval *= powf(10, *exponent); | |
eed3dec8 MG |
269 | |
270 | /* Measurement modes */ | |
271 | if (info->is_volt) { | |
b02bb45f UH |
272 | analog->meaning->mq = SR_MQ_VOLTAGE; |
273 | analog->meaning->unit = SR_UNIT_VOLT; | |
eed3dec8 MG |
274 | } |
275 | if (info->is_ampere) { | |
b02bb45f UH |
276 | analog->meaning->mq = SR_MQ_CURRENT; |
277 | analog->meaning->unit = SR_UNIT_AMPERE; | |
eed3dec8 MG |
278 | } |
279 | if (info->is_ohm) { | |
b02bb45f UH |
280 | analog->meaning->mq = SR_MQ_RESISTANCE; |
281 | analog->meaning->unit = SR_UNIT_OHM; | |
eed3dec8 MG |
282 | } |
283 | if (info->is_hz) { | |
b02bb45f UH |
284 | analog->meaning->mq = SR_MQ_FREQUENCY; |
285 | analog->meaning->unit = SR_UNIT_HERTZ; | |
eed3dec8 MG |
286 | } |
287 | if (info->is_farad) { | |
b02bb45f UH |
288 | analog->meaning->mq = SR_MQ_CAPACITANCE; |
289 | analog->meaning->unit = SR_UNIT_FARAD; | |
eed3dec8 MG |
290 | } |
291 | if (info->is_beep) { | |
b02bb45f UH |
292 | analog->meaning->mq = SR_MQ_CONTINUITY; |
293 | analog->meaning->unit = SR_UNIT_BOOLEAN; | |
eed3dec8 MG |
294 | *floatval = (*floatval == INFINITY) ? 0.0 : 1.0; |
295 | } | |
296 | if (info->is_diode) { | |
b02bb45f UH |
297 | analog->meaning->mq = SR_MQ_VOLTAGE; |
298 | analog->meaning->unit = SR_UNIT_VOLT; | |
eed3dec8 MG |
299 | } |
300 | if (info->is_percent) { | |
b02bb45f UH |
301 | analog->meaning->mq = SR_MQ_DUTY_CYCLE; |
302 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
eed3dec8 MG |
303 | } |
304 | if (info->is_degc) { | |
b02bb45f UH |
305 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
306 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
eed3dec8 MG |
307 | } |
308 | if (info->is_degf) { | |
b02bb45f UH |
309 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
310 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
eed3dec8 MG |
311 | } |
312 | ||
313 | /* Measurement related flags */ | |
314 | if (info->is_ac) | |
b02bb45f | 315 | analog->meaning->mqflags |= SR_MQFLAG_AC; |
eed3dec8 | 316 | if (info->is_dc) |
b02bb45f | 317 | analog->meaning->mqflags |= SR_MQFLAG_DC; |
eed3dec8 | 318 | if (info->is_auto) |
b02bb45f | 319 | analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; |
eed3dec8 | 320 | if (info->is_diode) |
64aa214a | 321 | analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC; |
eed3dec8 | 322 | if (info->is_hold) |
b02bb45f | 323 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; |
eed3dec8 | 324 | if (info->is_rel) |
b02bb45f | 325 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; |
eed3dec8 | 326 | if (info->is_min) |
b02bb45f | 327 | analog->meaning->mqflags |= SR_MQFLAG_MIN; |
eed3dec8 | 328 | if (info->is_max) |
b02bb45f | 329 | analog->meaning->mqflags |= SR_MQFLAG_MAX; |
eed3dec8 MG |
330 | |
331 | /* Other flags */ | |
332 | if (info->is_rs232) | |
333 | sr_spew("RS232 enabled."); | |
334 | if (info->is_bat) | |
335 | sr_spew("Battery is low."); | |
336 | if (info->is_apo) | |
337 | sr_spew("Auto power-off mode is active."); | |
338 | if (info->is_minmax) | |
339 | sr_spew("Min/max mode active."); | |
340 | if (info->is_c2c1_00) | |
341 | sr_spew("User-defined LCD symbol 0 is active."); | |
342 | if (info->is_c2c1_01) | |
343 | sr_spew("User-defined LCD symbol 1 is active."); | |
344 | } | |
345 | ||
346 | SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf) | |
347 | { | |
348 | struct dtm0660_info info; | |
349 | ||
350 | parse_flags(buf, &info); | |
351 | ||
352 | return (sync_nibbles_valid(buf) && flags_valid(&info)); | |
353 | } | |
354 | ||
355 | /** | |
356 | * Parse a protocol packet. | |
357 | * | |
358 | * @param buf Buffer containing the 15-byte protocol packet. Must not be NULL. | |
359 | * @param floatval Pointer to a float variable. That variable will contain the | |
360 | * result value upon parsing success. Must not be NULL. | |
b02bb45f | 361 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be |
eed3dec8 MG |
362 | * filled with data according to the protocol packet. |
363 | * Must not be NULL. | |
364 | * @param info Pointer to a struct dtm0660_info. The struct will be filled | |
365 | * with data according to the protocol packet. Must not be NULL. | |
366 | * | |
367 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the | |
368 | * 'analog' variable contents are undefined and should not be used. | |
369 | */ | |
370 | SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval, | |
b02bb45f | 371 | struct sr_datafeed_analog *analog, void *info) |
eed3dec8 | 372 | { |
427015d7 | 373 | int ret, exponent = 0; |
eed3dec8 MG |
374 | struct dtm0660_info *info_local; |
375 | ||
94b1d506 | 376 | info_local = info; |
eed3dec8 | 377 | |
427015d7 | 378 | if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) { |
eed3dec8 MG |
379 | sr_dbg("Error parsing value: %d.", ret); |
380 | return ret; | |
381 | } | |
382 | ||
383 | parse_flags(buf, info_local); | |
427015d7 AJ |
384 | handle_flags(analog, floatval, &exponent, info_local); |
385 | ||
d9251a2c | 386 | analog->encoding->digits = -exponent; |
427015d7 | 387 | analog->spec->spec_digits = -exponent; |
eed3dec8 MG |
388 | |
389 | return SR_OK; | |
390 | } |