2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * RadioShack 22-812 protocol parser.
23 * This protocol is currently encountered on the RadioShack 22-812 DMM.
24 * It is a 9-byte packet representing a 1:1 mapping of the LCD segments, hence
27 * The chip is a bare die covered by a plastic blob. It is unclear if this chip
28 * and protocol is used on any other device.
36 #include <libsigrok/libsigrok.h>
37 #include "libsigrok-internal.h"
39 #define LOG_PREFIX "rs9lcd"
41 /* Byte 1 of the packet, and the modes it represents */
42 #define IND1_HZ (1 << 7)
43 #define IND1_OHM (1 << 6)
44 #define IND1_KILO (1 << 5)
45 #define IND1_MEGA (1 << 4)
46 #define IND1_FARAD (1 << 3)
47 #define IND1_AMP (1 << 2)
48 #define IND1_VOLT (1 << 1)
49 #define IND1_MILI (1 << 0)
50 /* Byte 2 of the packet, and the modes it represents */
51 #define IND2_MICRO (1 << 7)
52 #define IND2_NANO (1 << 6)
53 #define IND2_DBM (1 << 5)
54 #define IND2_SEC (1 << 4)
55 #define IND2_DUTY (1 << 3)
56 #define IND2_HFE (1 << 2)
57 #define IND2_REL (1 << 1)
58 #define IND2_MIN (1 << 0)
59 /* Byte 7 of the packet, and the modes it represents */
60 #define INFO_BEEP (1 << 7)
61 #define INFO_DIODE (1 << 6)
62 #define INFO_BAT (1 << 5)
63 #define INFO_HOLD (1 << 4)
64 #define INFO_NEG (1 << 3)
65 #define INFO_AC (1 << 2)
66 #define INFO_RS232 (1 << 1)
67 #define INFO_AUTO (1 << 0)
68 /* Instead of a decimal point, digit 4 carries the MAX flag */
69 #define DIG4_MAX (1 << 3)
70 /* Mask to remove the decimal point from a digit */
71 #define DP_MASK (1 << 3)
73 /* What the LCD values represent */
107 MODE_VOLT_HZ = 11, /* Dial set to V, Hz selected by Hz button */
108 MODE_AMP_HZ = 12, /* Dial set to A, Hz selected by Hz button */
110 MODE_VOLT_DUTY = 14, /* Dial set to V, duty cycle selected */
111 MODE_AMP_DUTY = 15, /* Dial set to A, duty cycle selected */
113 MODE_VOLT_WIDTH = 17, /* Dial set to V, pulse width selected */
114 MODE_AMP_WIDTH = 18, /* Dial set to A, pulse width selected */
120 /* MODE_EF = 24, */ /* Not encountered on any DMM */
130 struct rs9lcd_packet {
142 static gboolean checksum_valid(const struct rs9lcd_packet *rs_packet)
148 raw = (void *)rs_packet;
150 for (i = 0; i < RS9LCD_PACKET_SIZE - 1; i++)
153 /* This is just a funky constant added to the checksum. */
155 sum -= rs_packet->checksum;
159 static gboolean selection_good(const struct rs9lcd_packet *rs_packet)
163 /* Does the packet have more than one multiplier? */
165 count += (rs_packet->indicatrix1 & IND1_KILO) ? 1 : 0;
166 count += (rs_packet->indicatrix1 & IND1_MEGA) ? 1 : 0;
167 count += (rs_packet->indicatrix1 & IND1_MILI) ? 1 : 0;
168 count += (rs_packet->indicatrix2 & IND2_MICRO) ? 1 : 0;
169 count += (rs_packet->indicatrix2 & IND2_NANO) ? 1 : 0;
171 sr_dbg("More than one multiplier detected in packet.");
175 /* Does the packet "measure" more than one type of value? */
177 count += (rs_packet->indicatrix1 & IND1_HZ) ? 1 : 0;
178 count += (rs_packet->indicatrix1 & IND1_OHM) ? 1 : 0;
179 count += (rs_packet->indicatrix1 & IND1_FARAD) ? 1 : 0;
180 count += (rs_packet->indicatrix1 & IND1_AMP) ? 1 : 0;
181 count += (rs_packet->indicatrix1 & IND1_VOLT) ? 1 : 0;
182 count += (rs_packet->indicatrix2 & IND2_DBM) ? 1 : 0;
183 count += (rs_packet->indicatrix2 & IND2_SEC) ? 1 : 0;
184 count += (rs_packet->indicatrix2 & IND2_DUTY) ? 1 : 0;
185 count += (rs_packet->indicatrix2 & IND2_HFE) ? 1 : 0;
187 sr_dbg("More than one measurement type detected in packet.");
195 * Since the 22-812 does not identify itself in any way, shape, or form,
196 * we really don't know for sure who is sending the data. We must use every
197 * possible check to filter out bad packets, especially since detection of the
198 * 22-812 depends on how well we can filter the packets.
200 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf)
202 const struct rs9lcd_packet *rs_packet = (void *)buf;
205 * Check for valid mode first, before calculating the checksum. No
206 * point calculating the checksum, if we know we'll reject the packet.
208 if (!(rs_packet->mode < MODE_INVALID))
211 if (!checksum_valid(rs_packet)) {
212 sr_spew("Packet with invalid checksum. Discarding.");
216 if (!selection_good(rs_packet)) {
217 sr_spew("Packet with invalid selection bits. Discarding.");
224 static uint8_t decode_digit(uint8_t raw_digit)
226 /* Take out the decimal point, so we can use a simple switch(). */
227 raw_digit &= ~DP_MASK;
252 sr_dbg("Invalid digit byte: 0x%02x.", raw_digit);
257 static double lcd_to_double(const struct rs9lcd_packet *rs_packet, int type)
259 double rawval = 0, multiplier = 1;
260 uint8_t digit, raw_digit;
261 gboolean dp_reached = FALSE;
264 /* end = 1: Don't parse last digit. end = 0: Parse all digits. */
265 end = (type == READ_TEMP) ? 1 : 0;
267 /* We have 4 digits, and we start from the most significant. */
268 for (i = 3; i >= end; i--) {
269 raw_digit = *(&(rs_packet->digit4) + i);
270 digit = decode_digit(raw_digit);
276 * Digit 1 does not have a decimal point. Instead, the decimal
277 * point is used to indicate MAX, so we must avoid testing it.
279 if ((i < 3) && (raw_digit & DP_MASK))
283 rawval = rawval * 10 + digit;
285 rawval *= multiplier;
286 if (rs_packet->info & INFO_NEG)
289 /* See if we need to multiply our raw value by anything. */
290 if (rs_packet->indicatrix2 & IND2_NANO)
292 else if (rs_packet->indicatrix2 & IND2_MICRO)
294 else if (rs_packet->indicatrix1 & IND1_MILI)
296 else if (rs_packet->indicatrix1 & IND1_KILO)
298 else if (rs_packet->indicatrix1 & IND1_MEGA)
304 static gboolean is_celsius(const struct rs9lcd_packet *rs_packet)
306 return ((rs_packet->digit4 & ~DP_MASK) == LCD_C);
309 static gboolean is_shortcirc(const struct rs9lcd_packet *rs_packet)
311 return ((rs_packet->digit2 & ~DP_MASK) == LCD_h);
314 static gboolean is_logic_high(const struct rs9lcd_packet *rs_packet)
316 sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~DP_MASK);
317 return ((rs_packet->digit2 & ~DP_MASK) == LCD_H);
320 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
321 struct sr_datafeed_analog_old *analog, void *info)
323 const struct rs9lcd_packet *rs_packet = (void *)buf;
328 rawval = lcd_to_double(rs_packet, READ_ALL);
330 switch (rs_packet->mode) {
332 analog->mq = SR_MQ_VOLTAGE;
333 analog->unit = SR_UNIT_VOLT;
334 analog->mqflags |= SR_MQFLAG_DC;
337 analog->mq = SR_MQ_VOLTAGE;
338 analog->unit = SR_UNIT_VOLT;
339 analog->mqflags |= SR_MQFLAG_AC;
341 case MODE_DC_UA: /* Fall through */
342 case MODE_DC_MA: /* Fall through */
344 analog->mq = SR_MQ_CURRENT;
345 analog->unit = SR_UNIT_AMPERE;
346 analog->mqflags |= SR_MQFLAG_DC;
348 case MODE_AC_UA: /* Fall through */
349 case MODE_AC_MA: /* Fall through */
351 analog->mq = SR_MQ_CURRENT;
352 analog->unit = SR_UNIT_AMPERE;
353 analog->mqflags |= SR_MQFLAG_AC;
356 analog->mq = SR_MQ_RESISTANCE;
357 analog->unit = SR_UNIT_OHM;
360 analog->mq = SR_MQ_CAPACITANCE;
361 analog->unit = SR_UNIT_FARAD;
364 analog->mq = SR_MQ_CONTINUITY;
365 analog->unit = SR_UNIT_BOOLEAN;
366 rawval = is_shortcirc(rs_packet);
369 analog->mq = SR_MQ_VOLTAGE;
370 analog->unit = SR_UNIT_VOLT;
371 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
373 case MODE_HZ: /* Fall through */
374 case MODE_VOLT_HZ: /* Fall through */
376 analog->mq = SR_MQ_FREQUENCY;
377 analog->unit = SR_UNIT_HERTZ;
381 * No matter whether or not we have an actual voltage reading,
382 * we are measuring voltage, so we set our MQ as VOLTAGE.
384 analog->mq = SR_MQ_VOLTAGE;
385 if (!isnan(rawval)) {
386 /* We have an actual voltage. */
387 analog->unit = SR_UNIT_VOLT;
389 /* We have either HI or LOW. */
390 analog->unit = SR_UNIT_BOOLEAN;
391 rawval = is_logic_high(rs_packet);
395 analog->mq = SR_MQ_GAIN;
396 analog->unit = SR_UNIT_UNITLESS;
398 case MODE_DUTY: /* Fall through */
399 case MODE_VOLT_DUTY: /* Fall through */
401 analog->mq = SR_MQ_DUTY_CYCLE;
402 analog->unit = SR_UNIT_PERCENTAGE;
404 case MODE_WIDTH: /* Fall through */
405 case MODE_VOLT_WIDTH: /* Fall through */
407 analog->mq = SR_MQ_PULSE_WIDTH;
408 analog->unit = SR_UNIT_SECOND;
411 analog->mq = SR_MQ_TEMPERATURE;
412 /* We need to reparse. */
413 rawval = lcd_to_double(rs_packet, READ_TEMP);
414 analog->unit = is_celsius(rs_packet) ?
415 SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
418 analog->mq = SR_MQ_POWER;
419 analog->unit = SR_UNIT_DECIBEL_MW;
420 analog->mqflags |= SR_MQFLAG_AC;
423 sr_dbg("Unknown mode: %d.", rs_packet->mode);
427 if (rs_packet->info & INFO_HOLD)
428 analog->mqflags |= SR_MQFLAG_HOLD;
429 if (rs_packet->digit4 & DIG4_MAX)
430 analog->mqflags |= SR_MQFLAG_MAX;
431 if (rs_packet->indicatrix2 & IND2_MIN)
432 analog->mqflags |= SR_MQFLAG_MIN;
433 if (rs_packet->info & INFO_AUTO)
434 analog->mqflags |= SR_MQFLAG_AUTORANGE;