]> sigrok.org Git - libsigrok.git/blame - src/hardware/victor-dmm/protocol.c
Build: Set local include directories in Makefile.am
[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>
c1aae900 23#include <libsigrok/libsigrok.h>
ac3898d2
BV
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
41caa319 170 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
ff945683
BV
171
172 /* Measurement mode */
a84a26d9
UH
173 meaning.channels = sdi->channels;
174 meaning.mq = 0;
ff945683
BV
175 switch (data[3]) {
176 case 0x00:
177 if (is_duty) {
a84a26d9
UH
178 meaning.mq = SR_MQ_DUTY_CYCLE;
179 meaning.unit = SR_UNIT_PERCENTAGE;
ff945683 180 } else
fa773062 181 sr_dbg("Unknown measurement mode: %.2x.", data[3]);
ff945683
BV
182 break;
183 case 0x01:
184 if (is_diode) {
a84a26d9
UH
185 meaning.mq = SR_MQ_VOLTAGE;
186 meaning.unit = SR_UNIT_VOLT;
187 meaning.mqflags |= SR_MQFLAG_DIODE;
ff945683
BV
188 if (ivalue < 0)
189 fvalue = NAN;
190 } else {
191 if (ivalue < 0)
192 break;
a84a26d9
UH
193 meaning.mq = SR_MQ_VOLTAGE;
194 meaning.unit = SR_UNIT_VOLT;
ff945683 195 if (is_ac)
a84a26d9 196 meaning.mqflags |= SR_MQFLAG_AC;
ff945683 197 if (is_dc)
a84a26d9 198 meaning.mqflags |= SR_MQFLAG_DC;
ff945683
BV
199 }
200 break;
201 case 0x02:
a84a26d9
UH
202 meaning.mq = SR_MQ_CURRENT;
203 meaning.unit = SR_UNIT_AMPERE;
ff945683 204 if (is_ac)
a84a26d9 205 meaning.mqflags |= SR_MQFLAG_AC;
ff945683 206 if (is_dc)
a84a26d9 207 meaning.mqflags |= SR_MQFLAG_DC;
ff945683
BV
208 break;
209 case 0x04:
210 if (is_continuity) {
a84a26d9
UH
211 meaning.mq = SR_MQ_CONTINUITY;
212 meaning.unit = SR_UNIT_BOOLEAN;
ff945683
BV
213 fvalue = ivalue < 0 ? 0.0 : 1.0;
214 } else {
a84a26d9
UH
215 meaning.mq = SR_MQ_RESISTANCE;
216 meaning.unit = SR_UNIT_OHM;
ff945683
BV
217 if (ivalue < 0)
218 fvalue = INFINITY;
219 }
220 break;
221 case 0x08:
222 /* Never seen */
fa773062 223 sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
ff945683
BV
224 break;
225 case 0x10:
a84a26d9
UH
226 meaning.mq = SR_MQ_FREQUENCY;
227 meaning.unit = SR_UNIT_HERTZ;
ff945683
BV
228 break;
229 case 0x20:
a84a26d9
UH
230 meaning.mq = SR_MQ_CAPACITANCE;
231 meaning.unit = SR_UNIT_FARAD;
ff945683
BV
232 break;
233 case 0x40:
a84a26d9
UH
234 meaning.mq = SR_MQ_TEMPERATURE;
235 meaning.unit = SR_UNIT_CELSIUS;
ff945683
BV
236 break;
237 case 0x80:
a84a26d9
UH
238 meaning.mq = SR_MQ_TEMPERATURE;
239 meaning.unit = SR_UNIT_FAHRENHEIT;
ff945683
BV
240 break;
241 default:
fa773062
UH
242 sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
243 break;
ff945683 244 }
a84a26d9 245 if (meaning.mq == 0)
ff945683
BV
246 return;
247
248 if (is_auto)
a84a26d9 249 meaning.mqflags |= SR_MQFLAG_AUTORANGE;
ff945683 250 if (is_hold)
a84a26d9 251 meaning.mqflags |= SR_MQFLAG_HOLD;
ff945683 252 if (is_max)
a84a26d9 253 meaning.mqflags |= SR_MQFLAG_MAX;
ff945683 254 if (is_min)
a84a26d9 255 meaning.mqflags |= SR_MQFLAG_MIN;
ff945683 256 if (is_relative)
a84a26d9
UH
257 meaning.mqflags |= SR_MQFLAG_RELATIVE;
258
ff945683 259 analog.data = &fvalue;
a84a26d9 260 analog.num_samples = 1;
a84a26d9
UH
261
262 packet.type = SR_DF_ANALOG2;
ff945683
BV
263 packet.payload = &analog;
264 sr_session_send(devc->cb_data, &packet);
265
266 devc->num_samples++;
267}
268
269SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
ac3898d2 270{
ff945683
BV
271 GString *dbg;
272 int i;
273 unsigned char data[DMM_DATA_SIZE];
274 unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
275 unsigned char shuffle[DMM_DATA_SIZE] = {
276 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
277 };
ac3898d2 278
ff945683
BV
279 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
280 if (i == DMM_DATA_SIZE) {
281 /* This DMM outputs all zeroes from time to time, just ignore it. */
282 sr_dbg("Received all zeroes.");
283 return SR_OK;
284 }
ac3898d2 285
ff945683
BV
286 /* Deobfuscate and reorder data. */
287 for (i = 0; i < DMM_DATA_SIZE; i++)
288 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
289
290 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
291 dbg = g_string_sized_new(128);
292 g_string_printf(dbg, "Deobfuscated.");
293 for (i = 0; i < DMM_DATA_SIZE; i++)
294 g_string_append_printf(dbg, " %.2x", data[i]);
295 sr_spew("%s", dbg->str);
296 g_string_free(dbg, TRUE);
ac3898d2
BV
297 }
298
69e19dd7 299 decode_buf(sdi, data);
ff945683
BV
300
301 return SR_OK;
ac3898d2 302}