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.
35 #include "libsigrok.h"
36 #include "libsigrok-internal.h"
38 #define LOG_PREFIX "rs9lcd"
40 /* Byte 1 of the packet, and the modes it represents */
41 #define IND1_HZ (1 << 7)
42 #define IND1_OHM (1 << 6)
43 #define IND1_KILO (1 << 5)
44 #define IND1_MEGA (1 << 4)
45 #define IND1_FARAD (1 << 3)
46 #define IND1_AMP (1 << 2)
47 #define IND1_VOLT (1 << 1)
48 #define IND1_MILI (1 << 0)
49 /* Byte 2 of the packet, and the modes it represents */
50 #define IND2_MICRO (1 << 7)
51 #define IND2_NANO (1 << 6)
52 #define IND2_DBM (1 << 5)
53 #define IND2_SEC (1 << 4)
54 #define IND2_DUTY (1 << 3)
55 #define IND2_HFE (1 << 2)
56 #define IND2_REL (1 << 1)
57 #define IND2_MIN (1 << 0)
58 /* Byte 7 of the packet, and the modes it represents */
59 #define INFO_BEEP (1 << 7)
60 #define INFO_DIODE (1 << 6)
61 #define INFO_BAT (1 << 5)
62 #define INFO_HOLD (1 << 4)
63 #define INFO_NEG (1 << 3)
64 #define INFO_AC (1 << 2)
65 #define INFO_RS232 (1 << 1)
66 #define INFO_AUTO (1 << 0)
67 /* Instead of a decimal point, digit 4 carries the MAX flag */
68 #define DIG4_MAX (1 << 3)
69 /* Mask to remove the decimal point from a digit */
70 #define DP_MASK (1 << 3)
72 /* What the LCD values represent */
106 MODE_VOLT_HZ = 11, /* Dial set to V, Hz selected by Hz button */
107 MODE_AMP_HZ = 12, /* Dial set to A, Hz selected by Hz button */
109 MODE_VOLT_DUTY = 14, /* Dial set to V, duty cycle selected */
110 MODE_AMP_DUTY = 15, /* Dial set to A, duty cycle selected */
112 MODE_VOLT_WIDTH = 17, /* Dial set to V, pulse width selected */
113 MODE_AMP_WIDTH = 18, /* Dial set to A, pulse width selected */
119 /* MODE_EF = 24, */ /* Not encountered on any DMM */
129 struct rs9lcd_packet {
141 static gboolean checksum_valid(const struct rs9lcd_packet *rs_packet)
147 raw = (void *)rs_packet;
149 for (i = 0; i < RS9LCD_PACKET_SIZE - 1; i++)
152 /* This is just a funky constant added to the checksum. */
154 sum -= rs_packet->checksum;
158 static gboolean selection_good(const struct rs9lcd_packet *rs_packet)
162 /* Does the packet have more than one multiplier? */
164 count += (rs_packet->indicatrix1 & IND1_KILO) ? 1 : 0;
165 count += (rs_packet->indicatrix1 & IND1_MEGA) ? 1 : 0;
166 count += (rs_packet->indicatrix1 & IND1_MILI) ? 1 : 0;
167 count += (rs_packet->indicatrix2 & IND2_MICRO) ? 1 : 0;
168 count += (rs_packet->indicatrix2 & IND2_NANO) ? 1 : 0;
170 sr_dbg("More than one multiplier detected in packet.");
174 /* Does the packet "measure" more than one type of value? */
176 count += (rs_packet->indicatrix1 & IND1_HZ) ? 1 : 0;
177 count += (rs_packet->indicatrix1 & IND1_OHM) ? 1 : 0;
178 count += (rs_packet->indicatrix1 & IND1_FARAD) ? 1 : 0;
179 count += (rs_packet->indicatrix1 & IND1_AMP) ? 1 : 0;
180 count += (rs_packet->indicatrix1 & IND1_VOLT) ? 1 : 0;
181 count += (rs_packet->indicatrix2 & IND2_DBM) ? 1 : 0;
182 count += (rs_packet->indicatrix2 & IND2_SEC) ? 1 : 0;
183 count += (rs_packet->indicatrix2 & IND2_DUTY) ? 1 : 0;
184 count += (rs_packet->indicatrix2 & IND2_HFE) ? 1 : 0;
186 sr_dbg("More than one measurement type detected in packet.");
194 * Since the 22-812 does not identify itself in any way, shape, or form,
195 * we really don't know for sure who is sending the data. We must use every
196 * possible check to filter out bad packets, especially since detection of the
197 * 22-812 depends on how well we can filter the packets.
199 SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf)
201 const struct rs9lcd_packet *rs_packet = (void *)buf;
204 * Check for valid mode first, before calculating the checksum. No
205 * point calculating the checksum, if we know we'll reject the packet.
207 if (!(rs_packet->mode < MODE_INVALID))
210 if (!checksum_valid(rs_packet)) {
211 sr_spew("Packet with invalid checksum. Discarding.");
215 if (!selection_good(rs_packet)) {
216 sr_spew("Packet with invalid selection bits. Discarding.");
223 static uint8_t decode_digit(uint8_t raw_digit)
225 /* Take out the decimal point, so we can use a simple switch(). */
226 raw_digit &= ~DP_MASK;
251 sr_dbg("Invalid digit byte: 0x%02x.", raw_digit);
256 static double lcd_to_double(const struct rs9lcd_packet *rs_packet, int type)
258 double rawval = 0, multiplier = 1;
259 uint8_t digit, raw_digit;
260 gboolean dp_reached = FALSE;
263 /* end = 1: Don't parse last digit. end = 0: Parse all digits. */
264 end = (type == READ_TEMP) ? 1 : 0;
266 /* We have 4 digits, and we start from the most significant. */
267 for (i = 3; i >= end; i--) {
268 raw_digit = *(&(rs_packet->digit4) + i);
269 digit = decode_digit(raw_digit);
275 * Digit 1 does not have a decimal point. Instead, the decimal
276 * point is used to indicate MAX, so we must avoid testing it.
278 if ((i < 3) && (raw_digit & DP_MASK))
282 rawval = rawval * 10 + digit;
284 rawval *= multiplier;
285 if (rs_packet->info & INFO_NEG)
288 /* See if we need to multiply our raw value by anything. */
289 if (rs_packet->indicatrix1 & IND2_NANO)
291 else if (rs_packet->indicatrix2 & IND2_MICRO)
293 else if (rs_packet->indicatrix1 & IND1_MILI)
295 else if (rs_packet->indicatrix1 & IND1_KILO)
297 else if (rs_packet->indicatrix1 & IND1_MEGA)
303 static gboolean is_celsius(const struct rs9lcd_packet *rs_packet)
305 return ((rs_packet->digit4 & ~DP_MASK) == LCD_C);
308 static gboolean is_shortcirc(const struct rs9lcd_packet *rs_packet)
310 return ((rs_packet->digit2 & ~DP_MASK) == LCD_h);
313 static gboolean is_logic_high(const struct rs9lcd_packet *rs_packet)
315 sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~DP_MASK);
316 return ((rs_packet->digit2 & ~DP_MASK) == LCD_H);
319 SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
320 struct sr_datafeed_analog *analog, void *info)
322 const struct rs9lcd_packet *rs_packet = (void *)buf;
327 rawval = lcd_to_double(rs_packet, READ_ALL);
329 switch (rs_packet->mode) {
331 analog->mq = SR_MQ_VOLTAGE;
332 analog->unit = SR_UNIT_VOLT;
333 analog->mqflags |= SR_MQFLAG_DC;
336 analog->mq = SR_MQ_VOLTAGE;
337 analog->unit = SR_UNIT_VOLT;
338 analog->mqflags |= SR_MQFLAG_AC;
340 case MODE_DC_UA: /* Fall through */
341 case MODE_DC_MA: /* Fall through */
343 analog->mq = SR_MQ_CURRENT;
344 analog->unit = SR_UNIT_AMPERE;
345 analog->mqflags |= SR_MQFLAG_DC;
347 case MODE_AC_UA: /* Fall through */
348 case MODE_AC_MA: /* Fall through */
350 analog->mq = SR_MQ_CURRENT;
351 analog->unit = SR_UNIT_AMPERE;
352 analog->mqflags |= SR_MQFLAG_AC;
355 analog->mq = SR_MQ_RESISTANCE;
356 analog->unit = SR_UNIT_OHM;
359 analog->mq = SR_MQ_CAPACITANCE;
360 analog->unit = SR_UNIT_FARAD;
363 analog->mq = SR_MQ_CONTINUITY;
364 analog->unit = SR_UNIT_BOOLEAN;
365 rawval = is_shortcirc(rs_packet);
368 analog->mq = SR_MQ_VOLTAGE;
369 analog->unit = SR_UNIT_VOLT;
370 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
372 case MODE_HZ: /* Fall through */
373 case MODE_VOLT_HZ: /* Fall through */
375 analog->mq = SR_MQ_FREQUENCY;
376 analog->unit = SR_UNIT_HERTZ;
380 * No matter whether or not we have an actual voltage reading,
381 * we are measuring voltage, so we set our MQ as VOLTAGE.
383 analog->mq = SR_MQ_VOLTAGE;
384 if (!isnan(rawval)) {
385 /* We have an actual voltage. */
386 analog->unit = SR_UNIT_VOLT;
388 /* We have either HI or LOW. */
389 analog->unit = SR_UNIT_BOOLEAN;
390 rawval = is_logic_high(rs_packet);
394 analog->mq = SR_MQ_GAIN;
395 analog->unit = SR_UNIT_UNITLESS;
397 case MODE_DUTY: /* Fall through */
398 case MODE_VOLT_DUTY: /* Fall through */
400 analog->mq = SR_MQ_DUTY_CYCLE;
401 analog->unit = SR_UNIT_PERCENTAGE;
403 case MODE_WIDTH: /* Fall through */
404 case MODE_VOLT_WIDTH: /* Fall through */
406 analog->mq = SR_MQ_PULSE_WIDTH;
407 analog->unit = SR_UNIT_SECOND;
410 analog->mq = SR_MQ_TEMPERATURE;
411 /* We need to reparse. */
412 rawval = lcd_to_double(rs_packet, READ_TEMP);
413 analog->unit = is_celsius(rs_packet) ?
414 SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
417 analog->mq = SR_MQ_POWER;
418 analog->unit = SR_UNIT_DECIBEL_MW;
419 analog->mqflags |= SR_MQFLAG_AC;
422 sr_dbg("Unknown mode: %d.", rs_packet->mode);
426 if (rs_packet->info & INFO_HOLD)
427 analog->mqflags |= SR_MQFLAG_HOLD;
428 if (rs_packet->digit4 & DIG4_MAX)
429 analog->mqflags |= SR_MQFLAG_MAX;
430 if (rs_packet->indicatrix2 & IND2_MIN)
431 analog->mqflags |= SR_MQFLAG_MIN;
432 if (rs_packet->info & INFO_AUTO)
433 analog->mqflags |= SR_MQFLAG_AUTORANGE;