2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 /* Byte 1 of the packet, and the modes it represents */
33 #define IND1_KILO 0x20
34 #define IND1_MEGA 0x10
35 #define IND1_FARAD 0x08
37 #define IND1_VOLT 0x02
38 #define IND1_MILI 0x01
39 /* Byte 2 of the packet, and the modes it represents */
40 #define IND2_MICRO 0x80
41 #define IND2_NANO 0x40
44 #define IND2_DUTY 0x08
48 /* Byte 7 of the packet, and the modes it represents */
49 #define INFO_BEEP 0x80
50 #define INFO_DIODE 0x30
52 #define INFO_HOLD 0x10
55 #define INFO_RS232 0x02
56 #define INFO_AUTO 0x01
57 /* Instead of a decimal point, digit 4 carries the MAX flag */
59 /* Mask to remove the decimal point from a digit */
62 /* What the LCD values represent */
102 MODE_VOLT_WIDTH = 17,
119 static gboolean checksum_valid(const struct rs_22_812_packet *rs_packet)
125 raw = (void *)rs_packet;
127 for (i = 0; i < RS_22_812_PACKET_SIZE - 1; i++)
130 /* This is just a funky constant added to the checksum. */
132 sum -= rs_packet->checksum;
136 static gboolean selection_good(const struct rs_22_812_packet *rs_packet)
140 /* Does the packet have more than one multiplier ? */
142 count += (rs_packet->indicatrix1 & IND1_KILO) ? 1 : 0;
143 count += (rs_packet->indicatrix1 & IND1_MEGA) ? 1 : 0;
144 count += (rs_packet->indicatrix1 & IND1_MILI) ? 1 : 0;
145 count += (rs_packet->indicatrix2 & IND2_MICRO) ? 1 : 0;
146 count += (rs_packet->indicatrix2 & IND2_NANO) ? 1 : 0;
148 sr_err("More than one multiplier detected in packet.");
152 /* Does the packet "measure" more than one type of value? */
154 count += (rs_packet->indicatrix1 & IND1_HZ) ? 1 : 0;
155 count += (rs_packet->indicatrix1 & IND1_OHM) ? 1 : 0;
156 count += (rs_packet->indicatrix1 & IND1_FARAD) ? 1 : 0;
157 count += (rs_packet->indicatrix1 & IND1_AMP) ? 1 : 0;
158 count += (rs_packet->indicatrix1 & IND1_VOLT) ? 1 : 0;
159 count += (rs_packet->indicatrix2 & IND2_DBM) ? 1 : 0;
160 count += (rs_packet->indicatrix2 & IND2_SEC) ? 1 : 0;
161 count += (rs_packet->indicatrix2 & IND2_DUTY) ? 1 : 0;
162 count += (rs_packet->indicatrix2 & IND2_HFE) ? 1 : 0;
164 sr_err("More than one measurement type detected in packet.");
172 * Since the 22-812 does not identify itself in any way, shape, or form,
173 * we really don't know for sure who is sending the data. We must use every
174 * possible check to filter out bad packets, especially since detection of the
175 * 22-812 depends on how well we can filter the packets.
177 SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet)
180 * Check for valid mode first, before calculating the checksum.
181 * No point calculating the checksum, if we know we'll reject the packet
183 if (!(rs_packet->mode < MODE_INVALID))
186 if (!checksum_valid(rs_packet)) {
187 sr_spew("Packet with invalid checksum. Discarding.");
191 if (!selection_good(rs_packet)) {
192 sr_spew("Packet with invalid selection bits. Discarding.");
199 static uint8_t decode_digit(uint8_t raw_digit)
201 /* Take out the decimal point, so we can use a simple switch(). */
202 raw_digit &= ~DP_MASK;
227 sr_err("Invalid digit byte: 0x%02x.", raw_digit);
232 static double lcd_to_double(const struct rs_22_812_packet *rs_packet, int type)
234 double rawval, multiplier = 1;
235 uint8_t digit, raw_digit;
236 gboolean dp_reached = FALSE;
239 /* end = 1: Don't parse last digit. end = 0: Parse all digits. */
240 end = (type == READ_TEMP) ? 1 : 0;
242 /* We have 4 digits, and we start from the most significant. */
243 for (i = 3; i >= end; i--) {
244 raw_digit = *(&(rs_packet->digit4) + i);
245 digit = decode_digit(raw_digit);
251 * Digit 1 does not have a decimal point. Instead, the decimal
252 * point is used to indicate MAX, so we must avoid testing it.
254 if ((i < 3) && (raw_digit & DP_MASK))
258 rawval = rawval * 10 + digit;
260 rawval *= multiplier;
261 if (rs_packet->info & INFO_NEG)
264 /* See if we need to multiply our raw value by anything. */
265 if (rs_packet->indicatrix1 & IND2_NANO) {
267 } else if (rs_packet->indicatrix2 & IND2_MICRO) {
269 } else if (rs_packet->indicatrix1 & IND1_MILI) {
271 } else if (rs_packet->indicatrix1 & IND1_KILO) {
273 } else if (rs_packet->indicatrix1 & IND1_MEGA) {
280 static gboolean is_celsius(struct rs_22_812_packet *rs_packet)
282 return ((rs_packet->digit4 & ~DP_MASK) == LCD_C);
285 static gboolean is_shortcirc(struct rs_22_812_packet *rs_packet)
287 return ((rs_packet->digit2 & ~DP_MASK) == LCD_h);
290 static gboolean is_logic_high(struct rs_22_812_packet *rs_packet)
292 sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~DP_MASK);
293 return ((rs_packet->digit2 & ~DP_MASK) == LCD_H);
296 static void handle_packet(struct rs_22_812_packet *rs_packet,
297 struct dev_context *devc)
300 struct sr_datafeed_packet packet;
301 struct sr_datafeed_analog *analog;
303 rawval = lcd_to_double(rs_packet, READ_ALL);
305 /* TODO: Check malloc return value. */
306 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
307 analog->num_samples = 1;
308 /* TODO: Check malloc return value. */
309 analog->data = g_try_malloc(sizeof(float));
310 *analog->data = (float)rawval;
313 switch (rs_packet->mode) {
315 analog->mq = SR_MQ_VOLTAGE;
316 analog->unit = SR_UNIT_VOLT;
317 analog->mqflags |= SR_MQFLAG_DC;
320 analog->mq = SR_MQ_VOLTAGE;
321 analog->unit = SR_UNIT_VOLT;
322 analog->mqflags |= SR_MQFLAG_AC;
327 analog->mq = SR_MQ_CURRENT;
328 analog->unit = SR_UNIT_AMPERE;
329 analog->mqflags |= SR_MQFLAG_DC;
334 analog->mq = SR_MQ_CURRENT;
335 analog->unit = SR_UNIT_AMPERE;
336 analog->mqflags |= SR_MQFLAG_AC;
339 analog->mq = SR_MQ_RESISTANCE;
340 analog->unit = SR_UNIT_OHM;
343 analog->mq = SR_MQ_CAPACITANCE;
344 analog->unit = SR_UNIT_FARAD;
347 analog->mq = SR_MQ_CONTINUITY;
348 analog->unit = SR_UNIT_BOOLEAN;
349 *analog->data = is_shortcirc(rs_packet);
352 analog->mq = SR_MQ_VOLTAGE;
353 analog->unit = SR_UNIT_VOLT;
354 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
359 analog->mq = SR_MQ_FREQUENCY;
360 analog->unit = SR_UNIT_HERTZ;
364 * No matter whether or not we have an actual voltage reading,
365 * we are measuring voltage, so we set our MQ as VOLTAGE.
367 analog->mq = SR_MQ_VOLTAGE;
368 if (!isnan(rawval)) {
369 /* We have an actual voltage. */
370 analog->unit = SR_UNIT_VOLT;
372 /* We have either HI or LOW. */
373 analog->unit = SR_UNIT_BOOLEAN;
374 *analog->data = is_logic_high(rs_packet);
378 analog->mq = SR_MQ_GAIN;
379 analog->unit = SR_UNIT_UNITLESS;
384 analog->mq = SR_MQ_DUTY_CYCLE;
385 analog->unit = SR_UNIT_PERCENTAGE;
388 case MODE_VOLT_WIDTH:
390 analog->mq = SR_MQ_PULSE_WIDTH;
391 analog->unit = SR_UNIT_SECOND;
393 analog->mq = SR_MQ_TEMPERATURE;
394 /* We need to reparse. */
395 *analog->data = lcd_to_double(rs_packet, READ_TEMP);
396 analog->unit = is_celsius(rs_packet) ?
397 SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
400 analog->mq = SR_MQ_POWER;
401 analog->unit = SR_UNIT_DECIBEL_MW;
402 analog->mqflags |= SR_MQFLAG_AC;
405 sr_err("Unknown mode: %d.", rs_packet->mode);
409 if (rs_packet->info & INFO_HOLD)
410 analog->mqflags |= SR_MQFLAG_HOLD;
411 if (rs_packet->digit4 & DIG4_MAX)
412 analog->mqflags |= SR_MQFLAG_MAX;
413 if (rs_packet->indicatrix2 & IND2_MIN)
414 analog->mqflags |= SR_MQFLAG_MIN;
415 if (rs_packet->info & INFO_AUTO)
416 analog->mqflags |= SR_MQFLAG_AUTORANGE;
418 if (analog->mq != -1) {
419 /* Got a measurement. */
420 sr_spew("Value: %f.", rawval);
421 packet.type = SR_DF_ANALOG;
422 packet.payload = analog;
423 sr_session_send(devc->cb_data, &packet);
426 g_free(analog->data);
430 static void handle_new_data(struct dev_context *devc)
433 size_t i, offset = 0;
434 struct rs_22_812_packet *rs_packet;
436 /* Try to get as much data as the buffer can hold. */
437 len = RS_DMM_BUFSIZE - devc->buflen;
438 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
440 sr_err("Serial port read error.");
445 /* Now look for packets in that data. */
446 while ((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) {
447 rs_packet = (void *)(devc->buf + offset);
448 if (rs_22_812_packet_valid(rs_packet)) {
449 handle_packet(rs_packet, devc);
450 offset += RS_22_812_PACKET_SIZE;
456 /* If we have any data left, move it to the beginning of our buffer. */
457 for (i = 0; i < devc->buflen - offset; i++)
458 devc->buf[i] = devc->buf[offset + i];
459 devc->buflen -= offset;
462 SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data)
464 struct sr_dev_inst *sdi;
465 struct dev_context *devc;
469 if (!(sdi = cb_data))
472 if (!(devc = sdi->priv))
475 if (revents == G_IO_IN) {
476 /* Serial data arrived. */
477 handle_new_data(devc);
480 if (devc->num_samples >= devc->limit_samples) {
481 sdi->driver->dev_acquisition_stop(sdi, cb_data);