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