]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | #include <glib.h> | |
21 | #include <string.h> | |
22 | #include <math.h> | |
23 | #include "libsigrok.h" | |
24 | #include "libsigrok-internal.h" | |
25 | #include "protocol.h" | |
26 | ||
27 | /* Reverse the high nibble into the low nibble */ | |
28 | static 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 | ||
43 | static void decode_buf(struct sr_dev_inst *sdi, unsigned char *data) | |
44 | { | |
45 | struct sr_datafeed_packet packet; | |
46 | struct sr_datafeed_analog2 analog; | |
47 | struct sr_analog_encoding encoding; | |
48 | struct sr_analog_meaning meaning; | |
49 | struct sr_analog_spec spec; | |
50 | struct dev_context *devc; | |
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 | ||
57 | devc = sdi->priv; | |
58 | ||
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 */ | |
79 | factor = 0; | |
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: | |
94 | sr_err("Unknown decimal point byte: 0x%.2x.", data[7]); | |
95 | break; | |
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 */ | |
136 | sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]); | |
137 | break; | |
138 | default: | |
139 | sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]); | |
140 | break; | |
141 | } | |
142 | ||
143 | /* Scale flags on the right, continued */ | |
144 | is_max = is_min = FALSE; | |
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 | ||
170 | sr_analog_init(&analog, &encoding, &meaning, &spec, 4); | |
171 | ||
172 | /* Measurement mode */ | |
173 | meaning.channels = sdi->channels; | |
174 | meaning.mq = 0; | |
175 | switch (data[3]) { | |
176 | case 0x00: | |
177 | if (is_duty) { | |
178 | meaning.mq = SR_MQ_DUTY_CYCLE; | |
179 | meaning.unit = SR_UNIT_PERCENTAGE; | |
180 | } else | |
181 | sr_dbg("Unknown measurement mode: %.2x.", data[3]); | |
182 | break; | |
183 | case 0x01: | |
184 | if (is_diode) { | |
185 | meaning.mq = SR_MQ_VOLTAGE; | |
186 | meaning.unit = SR_UNIT_VOLT; | |
187 | meaning.mqflags |= SR_MQFLAG_DIODE; | |
188 | if (ivalue < 0) | |
189 | fvalue = NAN; | |
190 | } else { | |
191 | if (ivalue < 0) | |
192 | break; | |
193 | meaning.mq = SR_MQ_VOLTAGE; | |
194 | meaning.unit = SR_UNIT_VOLT; | |
195 | if (is_ac) | |
196 | meaning.mqflags |= SR_MQFLAG_AC; | |
197 | if (is_dc) | |
198 | meaning.mqflags |= SR_MQFLAG_DC; | |
199 | } | |
200 | break; | |
201 | case 0x02: | |
202 | meaning.mq = SR_MQ_CURRENT; | |
203 | meaning.unit = SR_UNIT_AMPERE; | |
204 | if (is_ac) | |
205 | meaning.mqflags |= SR_MQFLAG_AC; | |
206 | if (is_dc) | |
207 | meaning.mqflags |= SR_MQFLAG_DC; | |
208 | break; | |
209 | case 0x04: | |
210 | if (is_continuity) { | |
211 | meaning.mq = SR_MQ_CONTINUITY; | |
212 | meaning.unit = SR_UNIT_BOOLEAN; | |
213 | fvalue = ivalue < 0 ? 0.0 : 1.0; | |
214 | } else { | |
215 | meaning.mq = SR_MQ_RESISTANCE; | |
216 | meaning.unit = SR_UNIT_OHM; | |
217 | if (ivalue < 0) | |
218 | fvalue = INFINITY; | |
219 | } | |
220 | break; | |
221 | case 0x08: | |
222 | /* Never seen */ | |
223 | sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]); | |
224 | break; | |
225 | case 0x10: | |
226 | meaning.mq = SR_MQ_FREQUENCY; | |
227 | meaning.unit = SR_UNIT_HERTZ; | |
228 | break; | |
229 | case 0x20: | |
230 | meaning.mq = SR_MQ_CAPACITANCE; | |
231 | meaning.unit = SR_UNIT_FARAD; | |
232 | break; | |
233 | case 0x40: | |
234 | meaning.mq = SR_MQ_TEMPERATURE; | |
235 | meaning.unit = SR_UNIT_CELSIUS; | |
236 | break; | |
237 | case 0x80: | |
238 | meaning.mq = SR_MQ_TEMPERATURE; | |
239 | meaning.unit = SR_UNIT_FAHRENHEIT; | |
240 | break; | |
241 | default: | |
242 | sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]); | |
243 | break; | |
244 | } | |
245 | if (meaning.mq == 0) | |
246 | return; | |
247 | ||
248 | if (is_auto) | |
249 | meaning.mqflags |= SR_MQFLAG_AUTORANGE; | |
250 | if (is_hold) | |
251 | meaning.mqflags |= SR_MQFLAG_HOLD; | |
252 | if (is_max) | |
253 | meaning.mqflags |= SR_MQFLAG_MAX; | |
254 | if (is_min) | |
255 | meaning.mqflags |= SR_MQFLAG_MIN; | |
256 | if (is_relative) | |
257 | meaning.mqflags |= SR_MQFLAG_RELATIVE; | |
258 | ||
259 | analog.data = &fvalue; | |
260 | analog.num_samples = 1; | |
261 | ||
262 | packet.type = SR_DF_ANALOG2; | |
263 | packet.payload = &analog; | |
264 | sr_session_send(devc->cb_data, &packet); | |
265 | ||
266 | devc->num_samples++; | |
267 | } | |
268 | ||
269 | SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf) | |
270 | { | |
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 | }; | |
278 | ||
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 | } | |
285 | ||
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); | |
297 | } | |
298 | ||
299 | decode_buf(sdi, data); | |
300 | ||
301 | return SR_OK; | |
302 | } |