]> sigrok.org Git - libsigrok.git/blob - hardware/victor-dmm/protocol.c
hardware: Call libusb_error_name() in all USB-related error messages
[libsigrok.git] / hardware / victor-dmm / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <glib.h>
21 #include <string.h>
22 #include <math.h>
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26
27 /* Reverse the high nibble into the low nibble */
28 static uint8_t decode_digit(uint8_t in)
29 {
30         uint8_t out, i;
31
32         out = 0;
33         in >>= 4;
34         for (i = 0x08; i; i >>= 1) {
35                 out >>= 1;
36                 if (in & i)
37                         out |= 0x08;
38         }
39
40         return out;
41 }
42
43 static void decode_buf(struct dev_context *devc, unsigned char *data)
44 {
45         struct sr_datafeed_packet packet;
46         struct sr_datafeed_analog analog;
47         long factor, ivalue;
48         uint8_t digits[4];
49         gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
50         gboolean is_hold, is_max, is_min, is_relative, minus;
51         float fvalue;
52
53         digits[0] = decode_digit(data[12]);
54         digits[1] = decode_digit(data[11]);
55         digits[2] = decode_digit(data[10]);
56         digits[3] = decode_digit(data[9]);
57
58         if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
59                         digits[3] == 0x0f)
60                 /* The "over limit" (OL) display comes through like this */
61                 ivalue = -1;
62         else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
63                 /* An invalid digit in any position denotes no value. */
64                 ivalue = -2;
65         else {
66                 ivalue = digits[0] * 1000;
67                 ivalue += digits[1] * 100;
68                 ivalue += digits[2] * 10;
69                 ivalue += digits[3];
70         }
71
72         /* Decimal point position */
73         switch (data[7] >> 4) {
74         case 0x00:
75                 factor = 0;
76                 break;
77         case 0x02:
78                 factor = 1;
79                 break;
80         case 0x04:
81                 factor = 2;
82                 break;
83         case 0x08:
84                 factor = 3;
85                 break;
86         default:
87                 sr_err("Unknown decimal point byte: 0x%.2x.", data[7]);
88                 break;
89         }
90
91         /* Minus flag */
92         minus = data[2] & 0x01;
93
94         /* Mode detail symbols on the right side of the digits */
95         is_duty = is_continuity = is_diode = FALSE;
96         switch (data[4]) {
97         case 0x00:
98                 /* None. */
99                 break;
100         case 0x01:
101                 /* Micro */
102                 factor += 6;
103                 break;
104         case 0x02:
105                 /* Milli */
106                 factor += 3;
107                 break;
108         case 0x04:
109                 /* Kilo */
110                 ivalue *= 1000;
111                 break;
112         case 0x08:
113                 /* Mega */
114                 ivalue *= 1000000;
115                 break;
116         case 0x10:
117                 /* Continuity shows up as Ohm + this bit */
118                 is_continuity = TRUE;
119                 break;
120         case 0x20:
121                 /* Diode tester is Volt + this bit */
122                 is_diode = TRUE;
123                 break;
124         case 0x40:
125                 is_duty = TRUE;
126                 break;
127         case 0x80:
128                 /* Never seen */
129                 sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]);
130                 break;
131         default:
132                 sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]);
133                 break;
134         }
135
136         /* Scale flags on the right, continued */
137         is_max = is_min = TRUE;
138         if (data[5] & 0x04)
139                 is_max = TRUE;
140         if (data[5] & 0x08)
141                 is_min = TRUE;
142         if (data[5] & 0x40)
143                 /* Nano */
144                 factor += 9;
145
146         /* Mode detail symbols on the left side of the digits */
147         is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
148         if (data[6] & 0x04)
149                 is_auto = TRUE;
150         if (data[6] & 0x08)
151                 is_dc = TRUE;
152         if (data[6] & 0x10)
153                 is_ac = TRUE;
154         if (data[6] & 0x20)
155                 is_relative = TRUE;
156         if (data[6] & 0x40)
157                 is_hold = TRUE;
158
159         fvalue = (float)ivalue / pow(10, factor);
160         if (minus)
161                 fvalue = -fvalue;
162
163         memset(&analog, 0, sizeof(struct sr_datafeed_analog));
164
165         /* Measurement mode */
166         analog.mq = -1;
167         switch (data[3]) {
168         case 0x00:
169                 if (is_duty) {
170                         analog.mq = SR_MQ_DUTY_CYCLE;
171                         analog.unit = SR_UNIT_PERCENTAGE;
172                 } else
173                         sr_dbg("Unknown measurement mode: %.2x.", data[3]);
174                 break;
175         case 0x01:
176                 if (is_diode) {
177                         analog.mq = SR_MQ_VOLTAGE;
178                         analog.unit = SR_UNIT_VOLT;
179                         analog.mqflags |= SR_MQFLAG_DIODE;
180                         if (ivalue < 0)
181                                 fvalue = NAN;
182                 } else {
183                         if (ivalue < 0)
184                                 break;
185                         analog.mq = SR_MQ_VOLTAGE;
186                         analog.unit = SR_UNIT_VOLT;
187                         if (is_ac)
188                                 analog.mqflags |= SR_MQFLAG_AC;
189                         if (is_dc)
190                                 analog.mqflags |= SR_MQFLAG_DC;
191                 }
192                 break;
193         case 0x02:
194                 analog.mq = SR_MQ_CURRENT;
195                 analog.unit = SR_UNIT_AMPERE;
196                 if (is_ac)
197                         analog.mqflags |= SR_MQFLAG_AC;
198                 if (is_dc)
199                         analog.mqflags |= SR_MQFLAG_DC;
200                 break;
201         case 0x04:
202                 if (is_continuity) {
203                         analog.mq = SR_MQ_CONTINUITY;
204                         analog.unit = SR_UNIT_BOOLEAN;
205                         fvalue = ivalue < 0 ? 0.0 : 1.0;
206                 } else {
207                         analog.mq = SR_MQ_RESISTANCE;
208                         analog.unit = SR_UNIT_OHM;
209                         if (ivalue < 0)
210                                 fvalue = INFINITY;
211                 }
212                 break;
213         case 0x08:
214                 /* Never seen */
215                 sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
216                 break;
217         case 0x10:
218                 analog.mq = SR_MQ_FREQUENCY;
219                 analog.unit = SR_UNIT_HERTZ;
220                 break;
221         case 0x20:
222                 analog.mq = SR_MQ_CAPACITANCE;
223                 analog.unit = SR_UNIT_FARAD;
224                 break;
225         case 0x40:
226                 analog.mq = SR_MQ_TEMPERATURE;
227                 analog.unit = SR_UNIT_CELSIUS;
228                 break;
229         case 0x80:
230                 analog.mq = SR_MQ_TEMPERATURE;
231                 analog.unit = SR_UNIT_FAHRENHEIT;
232                 break;
233         default:
234                 sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
235                 break;
236         }
237         if (analog.mq == -1)
238                 return;
239
240         if (is_auto)
241                 analog.mqflags |= SR_MQFLAG_AUTORANGE;
242         if (is_hold)
243                 analog.mqflags |= SR_MQFLAG_HOLD;
244         if (is_max)
245                 analog.mqflags |= SR_MQFLAG_MAX;
246         if (is_min)
247                 analog.mqflags |= SR_MQFLAG_MIN;
248         if (is_relative)
249                 analog.mqflags |= SR_MQFLAG_RELATIVE;
250
251         analog.num_samples = 1;
252         analog.data = &fvalue;
253         packet.type = SR_DF_ANALOG;
254         packet.payload = &analog;
255         sr_session_send(devc->cb_data, &packet);
256
257         devc->num_samples++;
258 }
259
260 SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
261 {
262         struct dev_context *devc;
263         GString *dbg;
264         int i;
265         unsigned char data[DMM_DATA_SIZE];
266         unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
267         unsigned char shuffle[DMM_DATA_SIZE] = {
268                 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
269         };
270
271         devc = sdi->priv;
272
273         for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
274         if (i == DMM_DATA_SIZE) {
275                 /* This DMM outputs all zeroes from time to time, just ignore it. */
276                 sr_dbg("Received all zeroes.");
277                 return SR_OK;
278         }
279
280         /* Deobfuscate and reorder data. */
281         for (i = 0; i < DMM_DATA_SIZE; i++)
282                 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
283
284         if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
285                 dbg = g_string_sized_new(128);
286                 g_string_printf(dbg, "Deobfuscated.");
287                 for (i = 0; i < DMM_DATA_SIZE; i++)
288                         g_string_append_printf(dbg, " %.2x", data[i]);
289                 sr_spew("%s", dbg->str);
290                 g_string_free(dbg, TRUE);
291         }
292
293         decode_buf(devc, data);
294
295         return SR_OK;
296 }