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