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