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