]> sigrok.org Git - libsigrok.git/blob - hardware/radioshack-dmm/radioshack.c
radioshack-dmm: Add support for Radioshack 22-812 DMM
[libsigrok.git] / hardware / radioshack-dmm / radioshack.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <glib.h>
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
24 #include "config.h"
25 #include "radioshack-dmm.h"
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29 #include <errno.h>
30
31
32 static gboolean rs_22_812_is_checksum_valid(const rs_22_812_packet *data)
33 {
34         uint8_t *raw = (void *) data;
35         uint8_t sum = 0;
36         size_t i;
37         for(i = 0; i < RS_22_812_PACKET_SIZE - 1; i++)
38                 sum += raw[i];
39         /* This is just a funky constant added to the checksum */
40         sum += 57;
41         sum -= data->checksum;
42         return(sum == 0);
43 }
44
45 static gboolean rs_22_812_is_mode_valid(rs_22_812_mode mode)
46 {
47         return(mode < RS_22_812_MODE_INVALID);
48 }
49
50 static gboolean rs_22_812_is_selection_good(const rs_22_812_packet *data)
51 {
52         int n_postfix = 0;
53         int n_type = 0;
54         /* Does the packet have more than one multiplier ? */
55         if(data->indicatrix1 & RS_22_812_IND1_KILO)
56                 n_postfix++;
57         if(data->indicatrix1 & RS_22_812_IND1_MEGA)
58                 n_postfix++;
59         if(data->indicatrix1 & RS_22_812_IND1_MILI)
60                 n_postfix++;
61         if(data->indicatrix2 & RS_22_812_IND2_MICRO)
62                 n_postfix++;
63         if(data->indicatrix2 & RS_22_812_IND2_NANO)
64                 n_postfix++;
65         if(n_postfix > 1)
66                 return FALSE;
67
68         /* Does the packet "measure" more than one type of value ?*/
69         if(data->indicatrix1 & RS_22_812_IND1_HZ)
70                 n_type++;
71         if(data->indicatrix1 & RS_22_812_IND1_OHM)
72                 n_type++;
73         if(data->indicatrix1 & RS_22_812_IND1_FARAD)
74                 n_type++;
75         if(data->indicatrix1 & RS_22_812_IND1_AMP)
76                 n_type++;
77         if(data->indicatrix1 & RS_22_812_IND1_VOLT)
78                 n_type++;
79         if(data->indicatrix2 & RS_22_812_IND2_DBM)
80                 n_type++;
81         if(data->indicatrix2 & RS_22_812_IND2_SEC)
82                 n_type++;
83         if(data->indicatrix2 & RS_22_812_IND2_DUTY)
84                 n_type++;
85         if(data->indicatrix2 & RS_22_812_IND2_HFE)
86                 n_type++;
87         if(n_type > 1)
88                 return FALSE;
89
90         /* OK, no duplicates */
91         return TRUE;
92 }
93
94 /* Since the RS 22-812 does not identify itslef in any way shape, or form,
95  * we really don't know for sure who is sending the data. We must use every
96  * possible check to filter out bad packets, especially since detection of the
97  * 22-812 depends on how well we can filter the packets */
98 SR_PRIV gboolean rs_22_812_is_packet_valid(const rs_22_812_packet *packet)
99 {
100         /* Unfortunately, the packet doesn't have a signature, so we must
101          * compute its checksum first */
102         if(!rs_22_812_is_checksum_valid(packet))
103                 return FALSE;
104
105         if(!rs_22_812_is_mode_valid(packet->mode))
106                 return FALSE;
107
108         if(!rs_22_812_is_selection_good(packet)) {
109                 return FALSE;
110         }
111         /* Made it here, huh? Then this looks to be a valid packet */
112         return TRUE;
113 }
114
115 static uint8_t rs_22_812_to_digit(uint8_t raw_digit)
116 {
117         /* Take out the decimal point, so we can use a simple switch() */
118         raw_digit &= ~RS_22_812_DP_MASK;
119         switch(raw_digit)
120         {
121         case 0x00:
122         case RS_22_812_LCD_0:
123                 return 0;
124         case RS_22_812_LCD_1:
125                 return 1;
126         case RS_22_812_LCD_2:
127                 return 2;
128         case RS_22_812_LCD_3:
129                 return 3;
130         case RS_22_812_LCD_4:
131                 return 4;
132         case RS_22_812_LCD_5:
133                 return 5;
134         case RS_22_812_LCD_6:
135                 return 6;
136         case RS_22_812_LCD_7:
137                 return 7;
138         case RS_22_812_LCD_8:
139                 return 8;
140         case RS_22_812_LCD_9:
141                 return 9;
142         default:
143                 return 0xff;
144         }
145 }
146
147 typedef enum {
148         READ_ALL,
149         READ_TEMP,
150 } value_type;
151
152 static double lcdraw_to_double(rs_22_812_packet *rs_packet, value_type type)
153 {
154         /* *********************************************************************
155          * Get a raw floating point value from the data
156          **********************************************************************/
157         double rawval;
158         double multiplier = 1;
159         uint8_t digit;
160         gboolean dp_reached = FALSE;
161         int i, end;
162         switch(type) {
163         case READ_TEMP:
164                 /* Do not parse the last digit */
165                 end = 1;
166                 break;
167         case READ_ALL:
168         default:
169                 /* Parse all digits */
170                 end = 0;
171         }
172         /* We have 4 digits, and we start from the most significant */
173         for(i = 3; i >= end; i--)
174         {
175                 uint8_t raw_digit = *(&(rs_packet->digit4) + i);
176                 digit = rs_22_812_to_digit(raw_digit);
177                 if(digit == 0xff) {
178                         rawval = NAN;
179                         break;
180                 }
181                 /* Digit 1 does not have a decimal point. Instead, the decimal
182                  * point is used to indicate MAX, so we must avoid testing it */
183                 if( (i < 3) && (raw_digit & RS_22_812_DP_MASK) )
184                         dp_reached = TRUE;
185                 if(dp_reached) multiplier /= 10;
186                 rawval = rawval * 10 + digit;
187         }
188         rawval *= multiplier;
189         if(rs_packet->info & RS_22_812_INFO_NEG)
190                 rawval *= -1;
191
192         /* See if we need to multiply our raw value by anything */
193         if(rs_packet->indicatrix1 & RS_22_812_IND2_NANO) {
194                 rawval *= 1E-9;
195         } else if(rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) {
196                 rawval *= 1E-6;
197         } else if(rs_packet->indicatrix1 & RS_22_812_IND1_MILI) {
198                 rawval *= 1E-3;
199         } else if(rs_packet->indicatrix1 & RS_22_812_IND1_KILO) {
200                 rawval *= 1E3;
201         } else if(rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) {
202                 rawval *= 1E6;
203         }
204
205         return rawval;
206 }
207
208 static gboolean rs_22_812_is_celsius(rs_22_812_packet *rs_packet)
209 {
210         return((rs_packet->digit4 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_C);
211 }
212
213 static gboolean rs_22_812_is_shortcirc(rs_22_812_packet *rs_packet)
214 {
215         return((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_h);
216 }
217
218 static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
219                                     rs_dev_ctx *devc)
220 {
221         double rawval = lcdraw_to_double(rs_packet, READ_ALL);
222         /* *********************************************************************
223          * Now see what the value means, and pass that on
224          **********************************************************************/
225         struct sr_datafeed_packet packet;
226         struct sr_datafeed_analog *analog;
227
228         analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
229         analog->num_samples = 1;
230         analog->data = g_try_malloc(sizeof(float));
231         *analog->data = (float)rawval;
232         analog->mq = -1;
233
234         switch(rs_packet->mode) {
235         case RS_22_812_MODE_DC_V:
236                 analog->mq = SR_MQ_VOLTAGE;
237                 analog->unit = SR_UNIT_VOLT;
238                 analog->mqflags |= SR_MQFLAG_DC;
239                 break;
240         case RS_22_812_MODE_AC_V:
241                 analog->mq = SR_MQ_VOLTAGE;
242                 analog->unit = SR_UNIT_VOLT;
243                 analog->mqflags |= SR_MQFLAG_AC;
244                 break;
245         case RS_22_812_MODE_DC_UA:
246         case RS_22_812_MODE_DC_MA:
247         case RS_22_812_MODE_DC_A:
248                 analog->mq = SR_MQ_CURRENT;
249                 analog->unit = SR_UNIT_AMPERE;
250                 analog->mqflags |= SR_MQFLAG_DC;
251                 break;
252         case RS_22_812_MODE_AC_UA:
253         case RS_22_812_MODE_AC_MA:
254         case RS_22_812_MODE_AC_A:
255                 analog->mq = SR_MQ_CURRENT;
256                 analog->unit = SR_UNIT_AMPERE;
257                 analog->mqflags |= SR_MQFLAG_AC;
258                 break;
259         case RS_22_812_MODE_OHM:
260                 analog->mq = SR_MQ_RESISTANCE;
261                 analog->unit = SR_UNIT_OHM;
262                 break;
263         case RS_22_812_MODE_FARAD:
264                 analog->mq = SR_MQ_CAPACITANCE;
265                 analog->unit = SR_UNIT_FARAD;
266                 break;
267         case RS_22_812_MODE_CONT:
268                 analog->mq = SR_MQ_CONTINUITY;
269                 analog->unit = SR_UNIT_BOOLEAN;
270                 *analog->data = rs_22_812_is_shortcirc(rs_packet);
271                 break;
272         case RS_22_812_MODE_DIODE:
273                 analog->mq = SR_MQ_VOLTAGE;
274                 analog->unit = SR_UNIT_VOLT;
275                 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
276                 break;
277         case RS_22_812_MODE_HZ:
278         case RS_22_812_MODE_VOLT_HZ:
279         case RS_22_812_MODE_AMP_HZ:
280                 analog->mq = SR_MQ_FREQUENCY;
281                 analog->unit = SR_UNIT_HERTZ;
282                 break;
283         case RS_22_812_MODE_LOGIC:
284                 analog->mq = 0; /* FIXME */
285                 analog->unit = SR_UNIT_BOOLEAN;
286                 sr_warn("radioshack-dmm: LOGIC mode not supported yet");
287                 g_free(analog->data);
288                 g_free(analog);
289                 return;
290                 break;
291         case RS_22_812_MODE_HFE:
292                 analog->mq = SR_MQ_GAIN;
293                 analog->unit = SR_UNIT_UNITLESS;
294                 break;
295         case RS_22_812_MODE_DUTY:
296         case RS_22_812_MODE_VOLT_DUTY:
297         case RS_22_812_MODE_AMP_DUTY:
298                 analog->mq = SR_MQ_DUTY_CYCLE;
299                 analog->unit = SR_UNIT_PERCENTAGE;
300                 break;
301         case RS_22_812_MODE_WIDTH:
302         case RS_22_812_MODE_VOLT_WIDTH:
303         case RS_22_812_MODE_AMP_WIDTH:
304                 analog->mq = SR_MQ_PULSE_WIDTH;
305                 analog->unit = SR_UNIT_SECOND;
306         case RS_22_812_MODE_TEMP:
307                 analog->mq = SR_MQ_TEMPERATURE;
308                 /* We need to reparse */
309                 *analog->data = lcdraw_to_double(rs_packet, READ_TEMP);
310                 analog->unit = rs_22_812_is_celsius(rs_packet)?
311                                 SR_UNIT_CELSIUS:SR_UNIT_FAHRENHEIT;
312                 break;
313         case RS_22_812_MODE_DBM:
314                 analog->mq = SR_MQ_POWER;
315                 analog->unit = SR_UNIT_DECIBEL_MW;
316                 analog->mqflags |= SR_MQFLAG_AC;
317                 break;
318         default:
319                 sr_warn("radioshack-dmm: unkown mode: %d", rs_packet->mode);
320                 break;
321         }
322
323         if(rs_packet->info & RS_22_812_INFO_HOLD) {
324                 analog->mqflags |= SR_MQFLAG_HOLD;
325         }
326         if(rs_packet->digit4 & RS_22_812_DIG4_MAX) {
327                 analog->mqflags |= SR_MQFLAG_MAX;
328         }
329         if(rs_packet->indicatrix2 & RS_22_812_IND2_MIN) {
330                 analog->mqflags |= SR_MQFLAG_MIN;
331         }
332         if(rs_packet->info & RS_22_812_INFO_AUTO) {
333                 analog->mqflags |= SR_MQFLAG_AUTORANGE;
334         }
335
336         if (analog->mq != -1) {
337                 /* Got a measurement. */
338                 sr_spew("radioshack-dmm: val %f", rawval);
339                 packet.type = SR_DF_ANALOG;
340                 packet.payload = analog;
341                 sr_session_send(devc->cb_data, &packet);
342                 devc->num_samples++;
343         }
344         g_free(analog->data);
345         g_free(analog);
346 }
347
348 static void handle_new_data(rs_dev_ctx *devc, int fd)
349 {
350         int len;
351         size_t i;
352         size_t offset = 0;
353         /* Try to get as much data as the buffer can hold */
354         len = RS_DMM_BUFSIZE - devc->buflen;
355         len = serial_read(fd, devc->buf + devc->buflen, len);
356         if (len < 1) {
357                 sr_err("radioshack-dmm: serial port read error!");
358                 return;
359         }
360         devc->buflen += len;
361
362         /* Now look for packets in that data */
363         while((devc->buflen - offset) >= RS_22_812_PACKET_SIZE)
364         {
365                 rs_22_812_packet * packet = (void *)(devc->buf + offset);
366                 if( rs_22_812_is_packet_valid(packet) )
367                 {
368                         rs_22_812_handle_packet(packet, devc);
369                         offset += RS_22_812_PACKET_SIZE;
370                 } else {
371                         offset++;
372                 }
373         }
374
375         /* If we have any data left, move it to the beginning of our buffer */
376         for(i = 0; i < devc->buflen - offset; i++)
377                 devc->buf[i] = devc->buf[offset + i];
378         devc->buflen -= offset;
379 }
380
381 SR_PRIV int radioshack_receive_data(int fd, int revents, void *cb_data)
382 {
383         const struct sr_dev_inst *sdi;
384         struct dev_context *devc;
385
386         if (!(sdi = cb_data))
387                 return TRUE;
388
389         if (!(devc = sdi->priv))
390                 return TRUE;
391
392         if (revents == G_IO_IN)
393         {
394                 /* Serial data arrived. */
395                 handle_new_data(devc, fd);
396         }
397
398         if (devc->num_samples >= devc->limit_samples) {
399                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
400                 return TRUE;
401         }
402
403         return TRUE;
404 }
405
406