]> sigrok.org Git - libsigrok.git/blame - src/dmm/ms8250d.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / dmm / ms8250d.c
CommitLineData
67070942
M
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) 2018 Stefan Mandl
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, see <http://www.gnu.org/licenses/>.
20 */
21
22/*
23 * MASTECH MS8250D protocol parser.
24 *
25 * Sends 18 bytes.
26 * 40 02 32 75 53 33 35 5303 10 00 00 00 00 00 00 10 00
27 *
28 * - Communication parameters: Unidirectional, 2400/8n1
29 * - CP2102 USB to UART bridge controller
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 "ms8250d"
41
42/*
43 * Main display (7-segment LCD value): xxDGA xxEF xxxx xxCB
44 * https://en.wikipedia.org/wiki/Seven-segment_display
45 */
46static int parse_digit(uint16_t b)
47{
48 switch (b) {
49 case 0x0: /* 7-segment not active */
50 return 0;
51 case 0x430: /* Overflow */
52 return 0xF;
53 case 0x533:
54 return 0;
55 case 0x003:
56 return 1;
57 case 0x721:
58 return 2;
59 case 0x703:
60 return 3;
61 case 0x213:
62 return 4;
63 case 0x712:
64 return 5;
65 case 0x732:
66 return 6;
67 case 0x103:
68 return 7;
69 case 0x733:
70 return 8;
71 case 0x713:
72 return 9;
73 default:
8a14fc08 74 sr_dbg("Invalid digit word: 0x%04x.", b);
67070942
M
75 return -1;
76 }
77}
78
79/* Parse second display. */
80static int parse_digit2(uint16_t b)
81{
82 switch (b) {
83 case 0x00:
84 return 0;
85 case 0x7D:
86 return 0;
87 case 0x05:
88 return 1;
89 case 0x1B:
90 return 2;
91 case 0x1F:
92 return 3;
93 case 0x27:
94 return 4;
95 case 0x3E:
96 return 5;
97 case 0x7E:
98 return 6;
99 case 0x15:
100 return 7;
101 case 0x7F:
102 return 8;
103 case 0x3F:
104 return 9;
105 default:
8a14fc08 106 sr_dbg("Invalid second display digit word: 0x%04x.", b);
67070942
M
107 return -1;
108 }
109}
110
111static void parse_flags(const uint8_t *buf, struct ms8250d_info *info)
112{
113 info->is_volt = (buf[9] & (1 << 4)) ? 1 : 0;
114 info->is_ohm = (buf[9] & (1 << 6)) ? 1 : 0;
115 info->is_ampere = (buf[10] & (1 << 0)) ? 1 : 0;
116 info->is_hz = (buf[10] & (1 << 2)) ? 1 : 0;
117 info->is_farad = (buf[10] & (1 << 1)) ? 1 : 0;
118
119 /* Micro */
120 if (!info->is_farad)
121 info->is_micro = (buf[8] & (1 << 4)) ? 1 : 0;
122 else
123 info->is_micro = (buf[9] & (1 << 1)) ? 1 : 0; /* uF */
124
125 info->is_nano = (buf[8] & (1 << 5)) ? 1 : 0;
126 info->is_milli = (buf[9] & (1 << 0)) ? 1 : 0;
127 info->is_kilo = (buf[9] & (1 << 2)) ? 1 : 0;
128 info->is_mega = (buf[8] & (1 << 6)) ? 1 : 0;
129
130 info->is_autotimer = (buf[1] & (1 << 0)) ? 1 : 0; /* Auto off timer */
131 info->is_rs232 = (buf[1] & (1 << 1)) ? 1 : 0; /* RS232 via USB */
132 info->is_ac = (buf[1] & (1 << 4)) ? 1 : 0;
133 info->is_dc = (buf[2] & (1 << 1)) ? 1 : 0;
134 info->is_auto = (buf[16] & (1 << 4)) ? 1 : 0;
135 info->is_bat = (buf[1] & (1 << 5)) ? 1 : 0; /* Low battery */
136 info->is_min = (buf[16] & (1 << 2)) ? 1 : 0;
137 info->is_max = (buf[16] & (1 << 1)) ? 1 : 0;
138 info->is_rel = (buf[15] & (1 << 7)) ? 1 : 0;
139 info->is_hold = (buf[16] & (1 << 3)) ? 1 : 0;
140 info->is_diode = (buf[11] & (1 << 0)) ? 1 : 0;
141 info->is_beep = (buf[11] & (1 << 1)) ? 1 : 0;
142 info->is_ncv = (buf[0] & (1 << 0)) ? 1 : 0;
143}
144
145static gboolean flags_valid(const struct ms8250d_info *info)
146{
147 int count;
148
149 /* Does the packet have more than one multiplier? */
150 count = 0;
151 count += (info->is_nano) ? 1 : 0;
152 count += (info->is_micro) ? 1 : 0;
153 count += (info->is_milli) ? 1 : 0;
154 count += (info->is_kilo) ? 1 : 0;
155 count += (info->is_mega) ? 1 : 0;
156 if (count > 1) {
157 sr_dbg("More than one multiplier detected in packet.");
158 return FALSE;
159 }
160
161 /* Does the packet "measure" more than one type of value? */
162 count = 0;
163 count += (info->is_hz) ? 1 : 0;
164 count += (info->is_ohm) ? 1 : 0;
165 count += (info->is_farad) ? 1 : 0;
166 count += (info->is_ampere) ? 1 : 0;
167 count += (info->is_volt) ? 1 : 0;
168 if (count > 1) {
169 sr_dbg("More than one measurement type detected in packet.");
170 return FALSE;
171 }
172
173 /* Both AC and DC set? */
174 if (info->is_ac && info->is_dc) {
175 sr_dbg("Both AC and DC flags detected in packet.");
176 return FALSE;
177 }
178
179 /* RS232 flag set? */
180 if (!info->is_rs232) {
181 sr_dbg("No RS232 flag detected in packet.");
182 return FALSE;
183 }
184
185 return TRUE;
186}
187
188static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
189 int *exponent, const struct ms8250d_info *info)
190{
191 /* Factors */
192 if (info->is_nano)
193 *exponent -= 9;
194 if (info->is_micro)
195 *exponent -= 6;
196 if (info->is_milli)
197 *exponent -= 3;
198 if (info->is_kilo)
199 *exponent += 3;
200 if (info->is_mega)
201 *exponent += 6;
202 *floatval *= powf(10, *exponent);
203
204 /* Measurement modes */
205 if (info->is_volt) {
206 analog->meaning->mq = SR_MQ_VOLTAGE;
207 analog->meaning->unit = SR_UNIT_VOLT;
208 }
209 if (info->is_ampere) {
210 analog->meaning->mq = SR_MQ_CURRENT;
211 analog->meaning->unit = SR_UNIT_AMPERE;
212 }
213 if (info->is_ohm) {
214 analog->meaning->mq = SR_MQ_RESISTANCE;
215 analog->meaning->unit = SR_UNIT_OHM;
216 }
217 if (info->is_hz) {
218 analog->meaning->mq = SR_MQ_FREQUENCY;
219 analog->meaning->unit = SR_UNIT_HERTZ;
220 }
221 if (info->is_farad) {
222 analog->meaning->mq = SR_MQ_CAPACITANCE;
223 analog->meaning->unit = SR_UNIT_FARAD;
224 }
225 if (info->is_beep) {
226 analog->meaning->mq = SR_MQ_CONTINUITY;
227 analog->meaning->unit = SR_UNIT_BOOLEAN;
228 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
229 }
230 if (info->is_diode) {
231 analog->meaning->mq = SR_MQ_VOLTAGE;
232 analog->meaning->unit = SR_UNIT_VOLT;
233 }
234 if (info->is_percent) {
235 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
236 analog->meaning->unit = SR_UNIT_PERCENTAGE;
237 }
238
239 /* Measurement related flags */
240 if (info->is_ac)
241 analog->meaning->mqflags |= SR_MQFLAG_AC;
242 if (info->is_dc)
243 analog->meaning->mqflags |= SR_MQFLAG_DC;
244 if (info->is_auto)
245 analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
246 if (info->is_diode)
247 analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
248 if (info->is_hold)
249 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
250 if (info->is_rel)
251 analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
252
253 /* Other flags */
254 if (info->is_rs232)
255 sr_spew("RS232 enabled.");
256 if (info->is_bat)
257 sr_spew("Battery is low.");
258 if (info->is_beep)
259 sr_spew("Beep is active");
260}
261
262SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf)
263{
264 struct ms8250d_info info;
265
266 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
267 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
268 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
269 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
270 buf[14], buf[15], buf[16], buf[17]);
271
272 parse_flags(buf, &info);
273
274 if ((buf[17] == 0x00) && flags_valid(&info))
275 return TRUE;
276
277 return FALSE;
278}
279
280/**
281 * Parse a protocol packet.
282 *
283 * @param buf Buffer containing the 18-byte protocol packet. Must not be NULL.
284 * @param floatval Pointer to a float variable. That variable will contain the
285 * result value upon parsing success. Must not be NULL.
286 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
287 * filled with data according to the protocol packet.
288 * Must not be NULL.
289 * @param info Pointer to a struct ms8250d_info. The struct will be filled
290 * with data according to the protocol packet. Must not be NULL.
291 *
292 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
293 * 'analog' variable contents are undefined and should not be used.
294 */
295SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
296 struct sr_datafeed_analog *analog, void *info)
297{
298 int exponent = 0, sec_exponent = 0, sign;
299 float sec_floatval;
300
301 /* buf[0] bar display. */
302 /* buf[1] bar display. */
303
304 /* Parse seven segment digit. */
305 int16_t digit4 = parse_digit(((buf[7] & 0x73) << 4) | (buf[8] & 0x3));
306
307 int16_t digit3 = parse_digit(((buf[6] & 0x07) << 8) | (buf[5] & 0x30) \
308 | ((buf[6] & 0x30) >> 4));
309
310 int16_t digit2 = parse_digit(((buf[4] & 0x73) << 4) | (buf[5] & 0x03));
311
312 int16_t digit1 = parse_digit(((buf[3] & 0x07) << 8) | (buf[2] & 0x30) \
313 | ((buf[3] & 0x30) >> 4));
314
315 sr_dbg("Digits: %d %d %d %d.", digit1, digit2, digit3, digit4);
316
317 /* Decimal point position. */
318 if ((buf[3] & (1 << 6)) != 0) {
319 exponent = -3;
320 sr_spew("Decimal point after first digit.");
321 } else if ((buf[5] & (1 << 6)) != 0) {
322 exponent = -2;
323 sr_spew("Decimal point after second digit.");
324 } else if ((buf[7] & (1 << 2)) != 0) {
325 exponent = -1;
326 sr_spew("Decimal point after third digit.");
327 } else {
328 exponent = 0;
329 sr_spew("No decimal point in the number.");
330 }
331
332 struct ms8250d_info *info_local;
333
334 info_local = info;
335
336 parse_flags(buf, info_local);
337
338 /* Sign */
339 sign = (buf[0] & (1 << 2)) ? -1 : 1;
340
341 /* Parse second display. */
342 int16_t sec_digit4 = parse_digit2(buf[12] & 0x7F);
343 int16_t sec_digit3 = parse_digit2(buf[13] & 0x7F);
344 int16_t sec_digit2 = parse_digit2(buf[14] & 0x7F);
345 int16_t sec_digit1 = parse_digit2(buf[15] & 0x7F);
346
347 sr_dbg("Digits (2nd display): %d %d %d %d.",
348 sec_digit1, sec_digit2, sec_digit3, sec_digit4);
349
350 /* Second display decimal point position. */
351 if ((buf[14] & (1 << 7)) != 0) {
352 sec_exponent = -3;
353 sr_spew("Sec decimal point after first digit.");
354 } else if ((buf[13] & (1 << 7)) != 0) {
355 sec_exponent = -2;
356 sr_spew("Sec decimal point after second digit.");
357 } else if ((buf[12] & (1 << 7)) != 0) {
358 sec_exponent = -1;
359 sr_spew("Sec decimal point after third digit.");
360 } else {
361 sec_exponent = 0;
362 sr_spew("Sec no decimal point in the number.");
363 }
364
365 *floatval = (double)((digit1 * 1000) + (digit2 * 100) + (digit3 * 10) + digit4);
366
367 sec_floatval = (double)(sec_digit1 * 1000) + (sec_digit2 * 100) + (sec_digit3 * 10) + sec_digit4;
368 sec_floatval *= powf(10, sec_exponent);
369
370 /* Apply sign. */
371 *floatval *= sign;
372
373 handle_flags(analog, floatval, &exponent, info_local);
374
375 /* Check for "OL". */
376 if (digit3 == 0x0F) {
377 sr_spew("Over limit.");
378 *floatval = INFINITY;
379 return SR_OK;
380 }
381
382 sr_spew("The display value is %f.", (double)*floatval);
383 sr_spew("The 2nd display value is %f.", sec_floatval);
384
385 analog->encoding->digits = -exponent;
386 analog->spec->spec_digits = -exponent;
387
388 return SR_OK;
389}