]>
Commit | Line | Data |
---|---|---|
b84c13d7 BV |
1 | /* |
2 | * This file is part of the sigrok 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 "libsigrok.h" | |
21 | #include "libsigrok-internal.h" | |
22 | #include "genericdmm.h" | |
23 | #include <libusb.h> | |
24 | #include <string.h> | |
25 | #include <math.h> | |
26 | ||
27 | #define DMM_DATA_SIZE 14 | |
28 | ||
29 | ||
30 | /* Reverse the high nibble into the low nibble */ | |
31 | static uint8_t decode_digit(uint8_t in) | |
32 | { | |
33 | uint8_t out, i; | |
34 | ||
35 | out = 0; | |
36 | in >>= 4; | |
37 | for (i = 0x08; i; i >>= 1) { | |
38 | out >>= 1; | |
39 | if (in & i) | |
40 | out |= 0x08; | |
41 | } | |
42 | ||
43 | return out; | |
44 | } | |
45 | ||
46 | static void decode_buf(struct dev_context *devc, unsigned char *data) | |
47 | { | |
48 | struct sr_datafeed_packet packet; | |
49 | struct sr_datafeed_analog analog; | |
50 | long factor, ivalue; | |
51 | uint8_t digits[4]; | |
52 | gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto, | |
53 | is_hold, is_max, is_min, is_relative, minus; | |
54 | float fvalue; | |
55 | ||
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]); | |
69a74024 | 60 | |
b84c13d7 BV |
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 */ | |
76 | switch (data[7] >> 4) { | |
77 | case 0x00: | |
78 | factor = 0; | |
79 | break; | |
80 | case 0x02: | |
81 | factor = 1; | |
82 | break; | |
83 | case 0x04: | |
84 | factor = 2; | |
85 | break; | |
86 | case 0x08: | |
87 | factor = 3; | |
88 | break; | |
89 | default: | |
90 | sr_err("genericdmm/victor-70c: unknown decimal point value %.2x", data[7]); | |
91 | } | |
92 | ||
93 | /* Minus flag */ | |
94 | minus = data[2] & 0x01; | |
95 | ||
96 | /* Mode detail symbols on the right side of the digits */ | |
97 | is_duty = is_continuity = is_diode = FALSE; | |
98 | switch (data[4]) { | |
99 | case 0x00: | |
100 | /* None. */ | |
101 | break; | |
102 | case 0x01: | |
103 | /* Micro */ | |
104 | factor += 6; | |
105 | break; | |
106 | case 0x02: | |
107 | /* Milli */ | |
108 | factor += 3; | |
109 | break; | |
110 | case 0x04: | |
111 | /* Kilo */ | |
112 | ivalue *= 1000; | |
113 | break; | |
114 | case 0x08: | |
115 | /* Mega */ | |
116 | ivalue *= 1000000; | |
117 | break; | |
118 | case 0x10: | |
119 | /* Continuity shows up as Ohm + this bit */ | |
120 | is_continuity = TRUE; | |
121 | break; | |
122 | case 0x20: | |
123 | /* Diode tester is Volt + this bit */ | |
124 | is_diode = TRUE; | |
125 | break; | |
126 | case 0x40: | |
127 | is_duty = TRUE; | |
128 | break; | |
129 | case 0x80: | |
130 | /* Never seen */ | |
131 | sr_dbg("genericdmm/victor-70c: unknown mode right detail %.2x", data[4]); | |
132 | break; | |
133 | default: | |
134 | sr_dbg("genericdmm/victor-70c: unknown/invalid mode right detail %.2x", data[4]); | |
135 | } | |
136 | ||
137 | /* Scale flags on the right, continued */ | |
138 | is_max = is_min = TRUE; | |
139 | if (data[5] & 0x04) | |
140 | is_max = TRUE; | |
141 | if (data[5] & 0x08) | |
142 | is_min = TRUE; | |
143 | if (data[5] & 0x40) | |
144 | /* Nano */ | |
145 | factor += 9; | |
146 | ||
147 | /* Mode detail symbols on the left side of the digits */ | |
148 | is_auto = is_dc = is_ac = is_hold = is_relative = FALSE; | |
149 | if (data[6] & 0x04) | |
150 | is_auto = TRUE; | |
151 | if (data[6] & 0x08) | |
152 | is_dc = TRUE; | |
153 | if (data[6] & 0x10) | |
154 | is_ac = TRUE; | |
155 | if (data[6] & 0x20) | |
156 | is_relative = TRUE; | |
157 | if (data[6] & 0x40) | |
158 | is_hold = TRUE; | |
159 | ||
160 | fvalue = (float)ivalue / pow(10, factor); | |
161 | if (minus) | |
162 | fvalue = -fvalue; | |
163 | ||
164 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
165 | ||
166 | /* Measurement mode */ | |
167 | analog.mq = -1; | |
168 | switch (data[3]) { | |
169 | case 0x00: | |
170 | if (is_duty) { | |
171 | analog.mq = SR_MQ_DUTY_CYCLE; | |
172 | analog.unit = SR_UNIT_PERCENTAGE; | |
173 | } else | |
174 | sr_dbg("genericdmm/victor-70c: unknown measurement mode %.2x", data[3]); | |
175 | break; | |
176 | case 0x01: | |
177 | if (is_diode) { | |
178 | analog.mq = SR_MQ_VOLTAGE; | |
179 | analog.unit = SR_UNIT_VOLT; | |
180 | analog.mqflags |= SR_MQFLAG_DIODE; | |
181 | if (ivalue < 0) | |
182 | fvalue = NAN; | |
183 | } else { | |
184 | if (ivalue < 0) | |
185 | break; | |
186 | analog.mq = SR_MQ_VOLTAGE; | |
187 | analog.unit = SR_UNIT_VOLT; | |
188 | if (is_ac) | |
189 | analog.mqflags |= SR_MQFLAG_AC; | |
190 | if (is_dc) | |
191 | analog.mqflags |= SR_MQFLAG_DC; | |
192 | } | |
193 | break; | |
194 | case 0x02: | |
195 | analog.mq = SR_MQ_CURRENT; | |
196 | analog.unit = SR_UNIT_AMPERE; | |
197 | if (is_ac) | |
198 | analog.mqflags |= SR_MQFLAG_AC; | |
199 | if (is_dc) | |
200 | analog.mqflags |= SR_MQFLAG_DC; | |
201 | break; | |
202 | case 0x04: | |
203 | if (is_continuity) { | |
204 | analog.mq = SR_MQ_CONTINUITY; | |
205 | analog.unit = SR_UNIT_BOOLEAN; | |
206 | fvalue = ivalue < 0 ? 0.0 : 1.0; | |
207 | } else { | |
208 | analog.mq = SR_MQ_RESISTANCE; | |
209 | analog.unit = SR_UNIT_OHM; | |
69a74024 BV |
210 | if (ivalue < 0) |
211 | fvalue = INFINITY; | |
b84c13d7 BV |
212 | } |
213 | break; | |
214 | case 0x08: | |
215 | /* Never seen */ | |
216 | sr_dbg("genericdmm/victor-70c: unknown measurement mode %.2x", data[3]); | |
217 | break; | |
218 | case 0x10: | |
219 | analog.mq = SR_MQ_FREQUENCY; | |
69a74024 | 220 | analog.unit = SR_UNIT_HERTZ; |
b84c13d7 BV |
221 | break; |
222 | case 0x20: | |
223 | analog.mq = SR_MQ_CAPACITANCE; | |
224 | analog.unit = SR_UNIT_FARAD; | |
225 | break; | |
226 | case 0x40: | |
227 | analog.mq = SR_MQ_TEMPERATURE; | |
228 | analog.unit = SR_UNIT_CELSIUS; | |
229 | break; | |
230 | case 0x80: | |
231 | analog.mq = SR_MQ_TEMPERATURE; | |
232 | analog.unit = SR_UNIT_FAHRENHEIT; | |
233 | break; | |
234 | default: | |
235 | sr_dbg("genericdmm/victor-70c: unknown/invalid measurement mode %.2x", data[3]); | |
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 | ||
261 | static int victor70c_data(struct sr_dev_inst *sdi) | |
262 | { | |
263 | struct dev_context *devc; | |
264 | GString *dbg; | |
265 | int len, ret, i; | |
266 | unsigned char buf[DMM_DATA_SIZE], data[DMM_DATA_SIZE]; | |
267 | unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia"; | |
268 | unsigned char shuffle[DMM_DATA_SIZE] = { | |
269 | 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1 | |
270 | }; | |
271 | ||
272 | devc = sdi->priv; | |
273 | ||
274 | if (sdi->status == SR_ST_INACTIVE) { | |
275 | /* First time through. */ | |
f6b8ffa6 BV |
276 | if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) { |
277 | if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) { | |
278 | sr_err("genericdmm/victor-70c: failed to detach kernel driver"); | |
279 | return SR_ERR; | |
280 | } | |
281 | } | |
b84c13d7 BV |
282 | |
283 | if (libusb_claim_interface(devc->usb->devhdl, 0)) { | |
284 | sr_err("genericdmm/victor-70c: failed to claim interface 0"); | |
285 | return SR_ERR; | |
286 | } | |
287 | sdi->status = SR_ST_ACTIVE; | |
288 | } | |
289 | ||
290 | ret = libusb_interrupt_transfer(devc->usb->devhdl, 0x81, buf, DMM_DATA_SIZE, | |
291 | &len, 100); | |
292 | if (ret != 0) { | |
293 | sr_err("genericdmm/victor-70c: failed to get data: libusb error %d", ret); | |
294 | return SR_ERR; | |
295 | } | |
296 | ||
297 | if (len != DMM_DATA_SIZE) { | |
298 | sr_dbg("genericdmm/victor-70c: short packet: received %d/%d bytes", | |
299 | len, DMM_DATA_SIZE); | |
300 | return SR_ERR; | |
301 | } | |
302 | ||
303 | for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++); | |
304 | if (i == DMM_DATA_SIZE) { | |
305 | /* This DMM outputs all zeroes from time to time, just ignore it. */ | |
306 | sr_dbg("genericdmm/victor-70c: received all zeroes"); | |
307 | return SR_OK; | |
308 | } | |
309 | ||
310 | /* Deobfuscate and reorder data. */ | |
311 | for (i = 0; i < DMM_DATA_SIZE; i++) | |
312 | data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff; | |
313 | ||
314 | if (sr_log_loglevel_get() >= SR_LOG_SPEW) { | |
315 | dbg = g_string_sized_new(128); | |
316 | g_string_printf(dbg, "genericdmm/victor-70c: deobfuscated"); | |
317 | for (i = 0; i < DMM_DATA_SIZE; i++) | |
318 | g_string_append_printf(dbg, " %.2x", data[i]); | |
319 | sr_spew("%s", dbg->str); | |
320 | g_string_free(dbg, TRUE); | |
321 | } | |
322 | ||
323 | decode_buf(devc, data); | |
324 | ||
325 | return SR_OK; | |
326 | } | |
327 | ||
328 | ||
329 | SR_PRIV struct dmmchip dmmchip_victor70c = { | |
330 | .data = victor70c_data, | |
331 | }; | |
332 |