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