]>
Commit | Line | Data |
---|---|---|
7b4edcb6 MH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
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 | ||
f5792417 MH |
20 | #include <math.h> |
21 | #include <string.h> | |
22 | ||
7b4edcb6 MH |
23 | #include "protocol.h" |
24 | ||
f5792417 MH |
25 | /** Set or clear flags in devc->mqflags. */ |
26 | static void setmqf(struct dev_context *devc, uint64_t flags, gboolean set) | |
27 | { | |
28 | if (set) | |
29 | devc->mqflags |= flags; | |
30 | else | |
31 | devc->mqflags &= ~flags; | |
32 | } | |
33 | ||
34 | /** Decode current type and measured value, Metrahit 12-16. | |
35 | */ | |
36 | static void decode_ctmv_16(guchar ctmv, struct dev_context *devc) | |
37 | { | |
38 | devc->mq = 0; | |
39 | devc->unit = 0; | |
40 | devc->mqflags = 0; | |
41 | ||
42 | switch (ctmv) { | |
43 | case 0x00: /* 0000 - */ | |
44 | break; | |
45 | case 0x01: /* 0001 mV DC */ | |
46 | devc->scale1000 = -1; /* Fall through */ | |
47 | case 0x02: /* 0010 V DC */ | |
48 | case 0x03: /* 0011 V AC+DC */ | |
49 | case 0x04: /* 0100 V AC */ | |
50 | devc->mq = SR_MQ_VOLTAGE; | |
51 | devc->unit = SR_UNIT_VOLT; | |
52 | if (ctmv <= 0x03) | |
53 | devc->mqflags |= SR_MQFLAG_DC; | |
54 | if (ctmv >= 0x03) { | |
55 | devc->mqflags |= SR_MQFLAG_AC; | |
56 | if (devc->model >= SR_METRAHIT_16S) | |
57 | devc->mqflags |= SR_MQFLAG_RMS; | |
58 | } | |
59 | break; | |
60 | case 0x05: /* 0101 Hz (15S/16S only) */ | |
61 | case 0x06: /* 0110 kHz (15S/16S only) */ | |
62 | devc->mq = SR_MQ_FREQUENCY; | |
63 | devc->unit = SR_UNIT_HERTZ; | |
64 | if (ctmv == 0x06) | |
65 | devc->scale1000 = 1; | |
66 | break; | |
67 | case 0x07: /* 0111 % (15S/16S only) */ | |
68 | devc->mq = SR_MQ_DUTY_CYCLE; | |
69 | devc->unit = SR_UNIT_PERCENTAGE; | |
70 | break; | |
71 | case 0x08: /* 1000 Diode */ | |
72 | devc->mq = SR_MQ_VOLTAGE; | |
73 | devc->unit = SR_UNIT_VOLT; | |
74 | devc->mqflags |= SR_MQFLAG_DIODE; | |
75 | break; | |
76 | case 0x09: /* 1001 Ohm, °C */ | |
77 | case 0x0a: /* 1010 kOhm */ | |
78 | case 0x0b: /* 1011 MOhm */ | |
79 | devc->mq = SR_MQ_RESISTANCE; /* Changed to temp. later if req.*/ | |
80 | devc->unit = SR_UNIT_OHM; | |
81 | devc->scale1000 = ctmv - 0x09; | |
82 | break; | |
83 | case 0x0c: /* 1100 nF (15S/16S only) */ | |
84 | case 0x0d: /* 1101 µF (15S/16S only) */ | |
85 | devc->mq = SR_MQ_CAPACITANCE; | |
86 | devc->unit = SR_UNIT_FARAD; | |
87 | if (ctmv == 0x0c) | |
88 | devc->scale1000 = -3; | |
89 | else | |
90 | devc->scale1000 = -2; | |
91 | break; | |
92 | case 0x0e: /* mA, µA */ | |
93 | devc->scale1000 = -1; /* Fall through. */ | |
94 | case 0x0f: /* A */ | |
95 | devc->mq = SR_MQ_CURRENT; | |
96 | devc->unit = SR_UNIT_AMPERE; | |
97 | if (devc->model == SR_METRAHIT_16S) | |
98 | devc->mqflags |= SR_MQFLAG_RMS; | |
99 | /* 16I A only with clamp, RMS questionable. */ | |
100 | break; | |
101 | } | |
102 | } | |
103 | ||
104 | /** Decode range/sign/acdc byte special chars (Metrahit 12-16). | |
105 | * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). | |
106 | */ | |
107 | static void decode_rs_16(guchar rs, struct dev_context *devc) | |
108 | { | |
109 | sr_spew("decode_rs_16(%d) scale = %f", rs, devc->scale); | |
110 | ||
111 | if (rs & 0x08) /* Sign */ | |
112 | devc->scale *= -1.0; | |
113 | ||
114 | if (devc->mq == SR_MQ_CURRENT) { | |
115 | if (rs & 0x04) /* Current is AC */ | |
116 | devc->mqflags |= SR_MQFLAG_AC; | |
117 | else | |
118 | devc->mqflags |= SR_MQFLAG_DC; | |
119 | } | |
120 | ||
121 | switch (rs & 0x03) { | |
122 | case 0: | |
123 | if (devc->mq == SR_MQ_VOLTAGE) /* V */ | |
124 | devc->scale *= 0.1; | |
125 | else if (devc->mq == SR_MQ_CURRENT) /* 000.0 µA */ | |
126 | devc->scale *= 0.0000001; /* Untested! */ | |
127 | else if (devc->mq == SR_MQ_RESISTANCE) { | |
128 | if (devc->buflen >= 10) { | |
129 | /* °C with 10 byte msg type, otherwise GOhm. */ | |
130 | devc->mq = SR_MQ_TEMPERATURE; | |
131 | devc->unit = SR_UNIT_CELSIUS; | |
132 | devc->scale *= 0.01; | |
133 | } | |
134 | else if ((devc->scale1000 == 2)) { | |
135 | /* 16I Iso 500/1000V 3 GOhm */ | |
136 | devc->scale *= 0.1; | |
137 | } | |
138 | } | |
139 | break; | |
140 | case 1: | |
141 | devc->scale *= 0.0001; | |
142 | break; | |
143 | case 2: | |
144 | devc->scale *= 0.001; | |
145 | break; | |
146 | case 3: | |
147 | devc->scale *= 0.01; | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
152 | /** Decode special chars, Metrahit 12-16. | |
153 | * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). | |
154 | */ | |
155 | static void decode_spc_16(guchar spc, struct dev_context *devc) | |
156 | { | |
157 | /* xxxx1xxx ON */ | |
158 | /* TODO: What does that mean? Power on? The 16I sets this. */ | |
159 | /* xxxxx1xx BEEP */ | |
160 | /* xxxxxx1x Low battery */ | |
161 | /* xxxxxxx1 FUSE */ | |
162 | /* 1xxxxxxx MIN */ | |
163 | setmqf(devc, SR_MQFLAG_MIN, spc & 0x80); | |
164 | ||
165 | /* x1xxxxxx MAN */ | |
166 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x40)); | |
167 | ||
168 | /* xx1xxxxx DATA */ | |
169 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x20); | |
170 | ||
171 | /* xxx1xxxx MAX */ | |
172 | setmqf(devc, SR_MQFLAG_MAX, spc & 0x10); | |
173 | } | |
174 | ||
175 | /** Decode current type and measured value, Metrahit 18. | |
176 | */ | |
177 | static void decode_ctmv_18(guchar ctmv, struct dev_context *devc) | |
178 | { | |
179 | devc->mq = 0; | |
180 | devc->unit = 0; | |
181 | devc->mqflags = 0; | |
182 | ||
183 | switch (ctmv) { | |
184 | case 0x00: /* 0000 - */ | |
185 | break; | |
186 | case 0x01: /* 0001 V AC */ | |
187 | case 0x02: /* 0010 V AC+DC */ | |
188 | case 0x03: /* 0011 V DC */ | |
189 | devc->mq = SR_MQ_VOLTAGE; | |
190 | devc->unit = SR_UNIT_VOLT; | |
191 | if (ctmv <= 0x02) | |
192 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
193 | if (ctmv >= 0x02) | |
194 | devc->mqflags |= SR_MQFLAG_DC; | |
195 | break; | |
196 | case 0x04: /* 0100 Ohm/Ohm with buzzer */ | |
197 | devc->mq = SR_MQ_RESISTANCE; | |
198 | devc->unit = SR_UNIT_OHM; | |
199 | break; | |
200 | case 0x05: /* 0101 Diode/Diode with buzzer*/ | |
201 | devc->mq = SR_MQ_VOLTAGE; | |
202 | devc->unit = SR_UNIT_VOLT; | |
203 | devc->mqflags |= SR_MQFLAG_DIODE; | |
204 | break; | |
205 | case 0x06: /* 0110 °C */ | |
206 | devc->mq = SR_MQ_TEMPERATURE; | |
207 | devc->unit = SR_UNIT_CELSIUS; | |
208 | break; | |
209 | case 0x07: /* 0111 F */ | |
210 | devc->mq = SR_MQ_CAPACITANCE; | |
211 | devc->unit = SR_UNIT_FARAD; | |
212 | break; | |
213 | case 0x08: /* 1000 mA DC */ | |
214 | case 0x09: /* 1001 A DC */ | |
215 | case 0x0a: /* 1010 mA AC+DC */ | |
216 | case 0x0b: /* 1011 A AC+DC */ | |
217 | devc->mq = SR_MQ_CURRENT; | |
218 | devc->unit = SR_UNIT_AMPERE; | |
219 | devc->mqflags |= SR_MQFLAG_DC; | |
220 | if (ctmv >= 0x0a) | |
221 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
222 | if ((ctmv == 0x08) || (ctmv == 0x0a)) | |
223 | devc->scale1000 = -1; | |
224 | break; | |
225 | case 0x0c: /* 1100 Hz */ | |
226 | devc->mq = SR_MQ_FREQUENCY; | |
227 | devc->unit = SR_UNIT_HERTZ; | |
228 | break; | |
229 | case 0x0d: /* 1101 dB */ | |
230 | devc->mq = SR_MQ_VOLTAGE; | |
231 | devc->unit = SR_UNIT_DECIBEL_VOLT; | |
232 | devc->mqflags |= SR_MQFLAG_AC; /* dB available for AC only */ | |
233 | break; | |
234 | case 0x0e: /* 1110 Events AC, Events AC+DC. Actually delivers just | |
235 | * current voltage via IR, nothing more. */ | |
236 | devc->mq = SR_MQ_VOLTAGE; | |
237 | devc->unit = SR_UNIT_VOLT; | |
238 | devc->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; | |
239 | break; | |
240 | case 0x0f: /* 1111 Clock */ | |
241 | devc->mq = SR_MQ_TIME; | |
242 | devc->unit = SR_UNIT_SECOND; | |
243 | devc->mqflags |= SR_MQFLAG_DURATION; | |
244 | break; | |
245 | } | |
246 | } | |
247 | ||
248 | /** Decode range/sign/acdc byte special chars, Metrahit 18. | |
249 | * \param[in] rs Rance/sign byte | |
250 | */ | |
251 | static void decode_rs_18(guchar rs, struct dev_context *devc) | |
252 | { | |
253 | int range; | |
254 | ||
255 | /* Sign */ | |
256 | if (((devc->scale > 0) && (rs & 0x08)) || | |
257 | ((devc->scale < 0) && !(rs & 0x08))) | |
258 | devc->scale *= -1.0; | |
259 | ||
260 | /* Range */ | |
261 | range = rs & 0x07; | |
262 | switch (devc->mq) { | |
263 | case SR_MQ_VOLTAGE: | |
264 | if (devc->unit == SR_UNIT_DECIBEL_VOLT) { | |
265 | devc->scale *= pow(10.0, -2); | |
266 | /* When entering relative mode, the device switches | |
267 | * from 10 byte to 6 byte msg format. Unfortunately | |
268 | * it switches back to 10 byte when the second value | |
269 | * is measured, so that's not sufficient to | |
270 | * identify relative mode. | |
271 | */ | |
272 | } | |
273 | else if (devc->vmains_29S) | |
274 | devc->scale *= pow(10.0, range - 2); | |
275 | else | |
276 | devc->scale *= pow(10.0, range - 5); | |
277 | break; | |
278 | case SR_MQ_CURRENT: | |
279 | if (devc->scale1000 == -1) | |
280 | devc->scale *= pow(10.0, range - 5); | |
281 | else | |
282 | devc->scale *= pow(10.0, range - 4); | |
283 | break; | |
284 | case SR_MQ_RESISTANCE: | |
285 | devc->scale *= pow(10.0, range - 2); | |
286 | break; | |
287 | case SR_MQ_FREQUENCY: | |
288 | devc->scale *= pow(10.0, range - 3); | |
289 | break; | |
290 | case SR_MQ_TEMPERATURE: | |
291 | devc->scale *= pow(10.0, range - 2); | |
292 | break; | |
293 | case SR_MQ_CAPACITANCE: | |
294 | devc->scale *= pow(10.0, range - 14); | |
295 | break; | |
296 | /* TODO: 29S Mains measurements. */ | |
297 | } | |
298 | } | |
299 | ||
300 | /** Decode special chars, Metrahit 18. | |
301 | * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). | |
302 | */ | |
303 | static void decode_spc_18(guchar spc, struct dev_context *devc) | |
304 | { | |
305 | /* xxxx1xxx ZERO */ | |
306 | /* xxxxx1xx BEEP */ | |
307 | /* xxxxxx1x Low battery */ | |
308 | /* xxxxxxx1 Fuse */ | |
309 | ||
310 | if (devc->mq == SR_MQ_TIME) { | |
311 | /* xxx1xxxx Clock running: 1; stop: 0 */ | |
312 | sr_spew("Clock running: %d", spc >> 4); | |
313 | } | |
314 | else { | |
315 | /* 1xxxxxxx MAN */ | |
316 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); | |
317 | ||
318 | /* x1xxxxxx MIN */ | |
319 | setmqf(devc, SR_MQFLAG_MIN, spc & 0x40); | |
320 | ||
321 | /* xx1xxxxx MAX */ | |
322 | setmqf(devc, SR_MQFLAG_MAX, spc & 0x20); | |
323 | ||
324 | /* xxx1xxxx DATA */ | |
325 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); | |
326 | } | |
327 | } | |
328 | ||
329 | /** Decode current type and measured value, Metrahit 2x. | |
330 | * \param[in] ctmv Current type and measured value (v1 | (v2<<4)). | |
331 | */ | |
332 | static void decode_ctmv_2x(guchar ctmv, struct dev_context *devc) | |
333 | { | |
334 | if ((ctmv > 0x1c) || (!devc)) { | |
335 | sr_err("decode_ct_mv_2x(%d): invalid param(s)!", ctmv); | |
336 | return; | |
337 | } | |
338 | ||
339 | devc->mq = 0; | |
340 | devc->unit = 0; | |
341 | devc->mqflags = 0; | |
342 | ||
343 | switch (ctmv) { | |
344 | /* 00000 unused */ | |
345 | case 0x01: /* 00001 V DC */ | |
346 | case 0x02: /* 00010 V AC+DC */ | |
347 | case 0x03: /* 00011 V AC */ | |
348 | devc->mq = SR_MQ_VOLTAGE; | |
349 | devc->unit = SR_UNIT_VOLT; | |
350 | if (ctmv <= 0x02) | |
351 | devc->mqflags |= SR_MQFLAG_DC; | |
352 | if (ctmv >= 0x02) { | |
353 | devc->mqflags |= SR_MQFLAG_AC; | |
354 | if (devc->model >= SR_METRAHIT_24S) | |
355 | devc->model |= SR_MQFLAG_RMS; | |
356 | } | |
357 | break; | |
358 | case 0x04: /* 00100 mA DC */ | |
359 | case 0x05: /* 00101 mA AC+DC */ | |
360 | devc->scale1000 = -1; | |
361 | case 0x06: /* 00110 A DC */ | |
362 | case 0x07: /* 00111 A AC+DC */ | |
363 | devc->mq = SR_MQ_CURRENT; | |
364 | devc->unit = SR_UNIT_AMPERE; | |
365 | devc->mqflags |= SR_MQFLAG_DC; | |
366 | if ((ctmv == 0x05) || (ctmv == 0x07)) { | |
367 | devc->mqflags |= SR_MQFLAG_AC; | |
368 | if (devc->model >= SR_METRAHIT_24S) | |
369 | devc->mqflags |= SR_MQFLAG_RMS; | |
370 | } | |
371 | break; | |
372 | case 0x08: /* 01000 Ohm */ | |
373 | devc->mq = SR_MQ_RESISTANCE; | |
374 | devc->unit = SR_UNIT_OHM; | |
375 | break; | |
376 | case 0x09: /* 01001 F */ | |
377 | devc->mq = SR_MQ_CAPACITANCE; | |
378 | devc->unit = SR_UNIT_FARAD; | |
379 | devc->scale *= 0.1; | |
380 | break; | |
381 | case 0x0a: /* 01010 dB */ | |
382 | devc->mq = SR_MQ_VOLTAGE; | |
383 | devc->unit = SR_UNIT_DECIBEL_VOLT; | |
384 | devc->mqflags |= SR_MQFLAG_AC; | |
385 | break; | |
386 | case 0x0b: /* 01011 Hz U ACDC */ | |
387 | case 0x0c: /* 01100 Hz U AC */ | |
388 | devc->mq = SR_MQ_FREQUENCY; | |
389 | devc->unit = SR_UNIT_HERTZ; | |
390 | devc->mqflags |= SR_MQFLAG_AC; | |
391 | if (ctmv <= 0x0b) | |
392 | devc->mqflags |= SR_MQFLAG_DC; | |
393 | break; | |
394 | case 0x0d: /* 01101 W on power, mA range (29S only) */ | |
395 | case 0x0e: /* 01110 W on power, A range (29S only) */ | |
396 | /* TODO: Differences between Send Mode and bidir protocol here */ | |
397 | devc->mq = SR_MQ_POWER; | |
398 | devc->unit = SR_UNIT_WATT; | |
399 | break; | |
400 | case 0x0f: /* 01111 Diode */ | |
401 | case 0x10: /* 10000 Diode with buzzer (actually cont. with voltage) */ | |
402 | devc->unit = SR_UNIT_VOLT; | |
403 | if (ctmv == 0x0f) { | |
404 | devc->mq = SR_MQ_VOLTAGE; | |
405 | devc->mqflags |= SR_MQFLAG_DIODE; | |
406 | devc->scale *= 0.1; | |
407 | } | |
408 | else { | |
409 | devc->mq = SR_MQ_CONTINUITY; | |
410 | devc->scale *= 0.00001; | |
411 | } | |
412 | devc->unit = SR_UNIT_VOLT; | |
413 | break; | |
414 | case 0x11: /* 10001 Ohm with buzzer */ | |
415 | devc->mq = SR_MQ_CONTINUITY; | |
416 | devc->unit = SR_UNIT_OHM; | |
417 | devc->scale1000 = -1; | |
418 | break; | |
419 | case 0x12: /* 10010 Temperature */ | |
420 | devc->mq = SR_MQ_TEMPERATURE; | |
421 | devc->unit = SR_UNIT_CELSIUS; | |
422 | /* This can be Fahrenheit. That is detected by range=4 later. */ | |
423 | break; | |
424 | /* 0x13 10011, 0x14 10100 unsed */ | |
425 | case 0x15: /* 10101 Press (29S only) */ | |
426 | /* TODO: What does that mean? Possibly phase shift? | |
427 | Then we need a unit/flag for it. */ | |
428 | devc->mq = SR_MQ_GAIN; | |
429 | devc->unit = SR_UNIT_PERCENTAGE; | |
430 | break; | |
431 | case 0x16: /* 10110 Pulse W (29S only) */ | |
432 | /* TODO: Own unit and flag for this! */ | |
433 | devc->mq = SR_MQ_POWER; | |
434 | devc->unit = SR_UNIT_WATT; | |
435 | break; | |
436 | case 0x17: /* 10111 TRMS V on mains (29S only) */ | |
437 | devc->mq = SR_MQ_VOLTAGE; | |
438 | devc->unit = SR_UNIT_VOLT; | |
439 | devc->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_RMS); | |
440 | devc->vmains_29S = TRUE; | |
441 | break; | |
442 | case 0x18: /* 11000 Counter (zero crossings of a signal) */ | |
443 | devc->mq = SR_MQ_VOLTAGE; | |
444 | devc->unit = SR_UNIT_UNITLESS; | |
445 | break; | |
446 | case 0x19: /* 11001 Events U ACDC */ | |
447 | case 0x1a: /* 11010 Events U AC */ | |
448 | /* TODO: No unit or flags for this yet! */ | |
449 | devc->mq = SR_MQ_VOLTAGE; | |
450 | devc->unit = SR_UNIT_UNITLESS; | |
451 | devc->mqflags |= SR_MQFLAG_AC; | |
452 | if (ctmv <= 0x19) | |
453 | devc->mqflags |= SR_MQFLAG_DC; | |
454 | break; | |
455 | case 0x1b: /* 11011 pulse on mains (29S only) */ | |
456 | /* TODO: No unit or flags for this yet! */ | |
457 | devc->mq = SR_MQ_VOLTAGE; | |
458 | devc->unit = SR_UNIT_UNITLESS; | |
459 | devc->mqflags |= SR_MQFLAG_AC; | |
460 | break; | |
461 | case 0x1c: /* 11100 dropout on mains (29S only) */ | |
462 | /* TODO: No unit or flags for this yet! */ | |
463 | devc->mq = SR_MQ_VOLTAGE; | |
464 | devc->unit = SR_UNIT_UNITLESS; | |
465 | devc->mqflags |= SR_MQFLAG_AC; | |
466 | break; | |
467 | default: | |
468 | sr_err("decode_ctmv_2x(%d, ...): Unknown ctmv!"); | |
469 | } | |
470 | } | |
471 | ||
472 | /** Decode range/sign/acdc byte special chars, Metrahit 2x. | |
473 | * \param[in] rs Rance/sign byte | |
474 | */ | |
475 | static void decode_rs_2x(guchar rs, struct dev_context *devc) | |
476 | { | |
477 | int range; | |
478 | ||
479 | /* Sign */ | |
480 | if (((devc->scale > 0) && (rs & 0x08)) || | |
481 | ((devc->scale < 0) && !(rs & 0x08))) | |
482 | devc->scale *= -1.0; | |
483 | ||
484 | /* Range */ | |
485 | range = rs & 0x07; | |
486 | switch (devc->mq) { | |
487 | case SR_MQ_VOLTAGE: | |
488 | if (devc->unit == SR_UNIT_DECIBEL_VOLT) { | |
489 | devc->scale *= pow(10.0, -3); | |
490 | } | |
491 | else if (devc->vmains_29S) | |
492 | devc->scale *= pow(10.0, range - 2); | |
493 | else if(devc->mqflags & SR_MQFLAG_AC) | |
494 | devc->scale *= pow(10.0, range - 6); | |
495 | else /* "Undocumented feature": Between AC and DC | |
496 | scaling differs by 1. */ | |
497 | devc->scale *= pow(10.0, range - 5); | |
498 | break; | |
499 | case SR_MQ_CURRENT: | |
500 | if (devc->scale1000 == -1) | |
501 | devc->scale *= pow(10.0, range - 5); | |
502 | else | |
503 | devc->scale *= pow(10.0, range - 4); | |
504 | break; | |
505 | case SR_MQ_RESISTANCE: | |
506 | devc->scale *= pow(10.0, range - 3); | |
507 | break; | |
508 | case SR_MQ_FREQUENCY: | |
509 | devc->scale *= pow(10.0, range - 3); | |
510 | break; | |
511 | case SR_MQ_TEMPERATURE: | |
512 | if (range == 4) { /* Indicator for °F */ | |
513 | devc->unit = SR_UNIT_FAHRENHEIT; | |
514 | } | |
515 | ||
516 | devc->scale *= pow(10.0, - 2); | |
517 | break; | |
518 | case SR_MQ_CAPACITANCE: | |
519 | devc->scale *= pow(10.0, range - 13); | |
520 | break; | |
521 | /* TODO: 29S Mains measurements. */ | |
522 | } | |
523 | } | |
524 | ||
525 | /** Decode special chars (Metrahit 2x). | |
526 | * \param[in] spc Special characters 1 and 2 (s1 | (s2<<4)). | |
527 | */ | |
528 | static void decode_spc_2x(guchar spc, struct dev_context *devc) | |
529 | { | |
530 | /* xxxxxxx1 Fuse */ | |
531 | ||
532 | /* xxxxxx1x Low battery */ | |
533 | ||
534 | /* xxxxx1xx BEEP */ | |
535 | ||
536 | /* xxxx1xxx ZERO */ | |
537 | ||
538 | /* xxx1xxxx DATA */ | |
539 | setmqf(devc, SR_MQFLAG_HOLD, spc & 0x10); | |
540 | ||
541 | /* x11xxxxx unused */ | |
542 | /* 1xxxxxxx MAN */ | |
543 | setmqf(devc, SR_MQFLAG_AUTORANGE, !(spc & 0x80)); | |
544 | } | |
545 | ||
546 | /** Clean range and sign. */ | |
547 | static void clean_rs_v(struct dev_context *devc) | |
548 | { | |
549 | devc->value = 0.0; | |
550 | devc->scale = 1.0; | |
551 | } | |
552 | ||
553 | /** Clean current type, measured variable, range and sign. */ | |
554 | static void clean_ctmv_rs_v(struct dev_context *devc) | |
555 | { | |
556 | devc->mq = 0; | |
557 | devc->unit = 0; | |
558 | devc->mqflags = 0; | |
559 | devc->scale1000 = 0; | |
560 | devc->vmains_29S = FALSE; | |
561 | clean_rs_v(devc); | |
562 | } | |
563 | ||
564 | /** Send prepared value. */ | |
565 | static void send_value(struct sr_dev_inst *sdi) | |
566 | { | |
567 | struct dev_context *devc; | |
568 | struct sr_datafeed_analog analog; | |
569 | struct sr_datafeed_packet packet; | |
570 | ||
571 | devc = sdi->priv; | |
572 | ||
573 | memset(&analog, 0, sizeof(analog)); | |
574 | analog.probes = sdi->probes; | |
575 | analog.num_samples = 1; | |
576 | analog.mq = devc->mq; | |
577 | analog.unit = devc->unit; | |
578 | analog.mqflags = devc->mqflags; | |
579 | analog.data = &devc->value; | |
580 | ||
581 | memset(&packet, 0, sizeof(packet)); | |
582 | packet.type = SR_DF_ANALOG; | |
583 | packet.payload = &analog; | |
584 | sr_session_send(devc->cb_data, &packet); | |
585 | ||
586 | devc->num_samples++; | |
587 | } | |
588 | ||
589 | /** Process 6-byte data message, Metrahit 1x/2x. */ | |
590 | static void process_msg_dta_6(struct sr_dev_inst *sdi) | |
591 | { | |
592 | struct dev_context *devc; | |
593 | int cnt; | |
594 | guchar dgt; | |
595 | ||
596 | devc = sdi->priv; | |
597 | clean_rs_v(devc); | |
598 | ||
599 | /* Byte 0, range and sign */ | |
600 | if (devc->model <= SR_METRAHIT_16X) { | |
601 | decode_rs_16(bc(devc->buf[0]), devc); | |
602 | } | |
603 | else if (devc->model < SR_METRAHIT_2X){ | |
604 | decode_rs_18(bc(devc->buf[0]), devc); | |
605 | } else { | |
606 | decode_rs_2x(bc(devc->buf[0]), devc); | |
607 | } | |
608 | ||
609 | /* Bytes 1-5, digits (ls first). */ | |
610 | for (cnt = 0; cnt < 5; cnt++) { | |
611 | dgt = bc(devc->buf[1 + cnt]); | |
612 | if (dgt >= 10) { | |
613 | /* 10 Overload; on model <= 16X also 11 possible. */ | |
614 | devc->value = NAN; | |
615 | devc->scale = 1.0; | |
616 | break; | |
617 | } | |
618 | devc->value += pow(10.0, cnt) * dgt; | |
619 | } | |
620 | sr_spew("process_msg_dta_6() value=%f scale=%f scalet=%d", | |
621 | devc->value, devc->scale, devc->scale1000); | |
622 | if (devc->value != NAN) | |
623 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
624 | ||
625 | /* Create and send packet. */ | |
626 | send_value(sdi); | |
627 | } | |
628 | ||
629 | /** Process 5-byte info message, Metrahit 1x/2x. */ | |
630 | static void process_msg_inf_5(struct sr_dev_inst *sdi) | |
631 | { | |
632 | struct dev_context *devc; | |
633 | enum model model; | |
634 | ||
635 | devc = sdi->priv; | |
636 | ||
637 | clean_ctmv_rs_v(devc); | |
638 | ||
639 | /* Process byte 0 */ | |
640 | model = sr_gmc_decode_model_sm(bc(devc->buf[0])); | |
641 | if (model != devc->model) { | |
642 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
643 | sr_gmc_model_str(devc->model), | |
644 | sr_gmc_model_str(model)); | |
645 | } | |
646 | ||
647 | /* Process bytes 1-4 */ | |
648 | if (devc->model <= SR_METRAHIT_16X) { | |
649 | decode_ctmv_16(bc(devc->buf[1]), devc); | |
650 | decode_spc_16(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
651 | decode_rs_16(bc(devc->buf[4]), devc); | |
652 | } | |
653 | else if (devc->model <= SR_METRAHIT_18S) { | |
654 | decode_ctmv_18(bc(devc->buf[1]), devc); | |
655 | decode_spc_18(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
656 | decode_rs_18(bc(devc->buf[4]), devc); | |
657 | } | |
658 | else { /* Must be Metrahit 2x */ | |
659 | decode_ctmv_2x(bc(devc->buf[1]), devc); | |
660 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
661 | decode_rs_2x(bc(devc->buf[4]), devc); | |
662 | } | |
663 | } | |
664 | ||
665 | /** Process 10-byte info/data message, Metrahit 15+. */ | |
666 | static void process_msg_inf_10(struct sr_dev_inst *sdi) | |
667 | { | |
668 | struct dev_context *devc; | |
669 | int cnt; | |
670 | guchar dgt; | |
671 | ||
672 | devc = sdi->priv; | |
673 | ||
674 | process_msg_inf_5(sdi); | |
675 | ||
676 | /* Now decode numbers */ | |
677 | for (cnt = 0; cnt < 5; cnt++) { | |
678 | dgt = bc(devc->buf[5 + cnt]); | |
679 | if (dgt >= 10) { /* Overload */ | |
680 | devc->value = NAN; | |
681 | devc->scale = 1.0; | |
682 | break; | |
683 | } | |
684 | devc->value += pow(10.0, cnt) * dgt; | |
685 | } | |
686 | sr_spew("process_msg_inf_10() value=%f scale=%f scalet=%d", | |
687 | devc->value, devc->scale, devc->scale1000); | |
688 | ||
689 | if (devc->value != NAN) | |
690 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
691 | ||
692 | /* Create and send packet. */ | |
693 | send_value(sdi); | |
694 | } | |
695 | ||
696 | /** Decode send interval (Metrahit 2x only). */ | |
697 | static const char* decode_send_interval(guchar si) | |
698 | { | |
699 | switch (si) { | |
700 | case 0x00: return "0.05"; | |
701 | case 0x01: return "0.1"; | |
702 | case 0x02: return "0.2"; | |
703 | case 0x03: return "0.5"; | |
704 | case 0x04: return "00:01"; | |
705 | case 0x05: return "00:02"; | |
706 | case 0x06: return "00:05"; | |
707 | case 0x07: return "00:10"; | |
708 | case 0x08: return "00:20"; | |
709 | case 0x09: return "00:30"; | |
710 | case 0x0a: return "01:00"; | |
711 | case 0x0b: return "02:00"; | |
712 | case 0x0c: return "05:00"; | |
713 | case 0x0d: return "10:00"; | |
714 | case 0x0e: return "----"; | |
715 | case 0x0f: return "data"; | |
716 | default: return "Unknown value"; | |
717 | } | |
718 | } | |
719 | ||
720 | /** Process 13-byte info/data message, Metrahit 2x. */ | |
721 | static void process_msg_inf_13(struct sr_dev_inst *sdi) | |
722 | { | |
723 | struct dev_context *devc; | |
724 | enum model model; | |
725 | int cnt; | |
726 | guchar dgt; | |
727 | ||
728 | devc = sdi->priv; | |
729 | ||
730 | clean_ctmv_rs_v(devc); | |
731 | ||
732 | /* Byte 0, model. */ | |
733 | model = sr_gmc_decode_model_sm(bc(devc->buf[0])); | |
734 | if (model != devc->model) { | |
735 | sr_warn("Model mismatch in data: Detected %s, now %s", | |
736 | sr_gmc_model_str(devc->model), | |
737 | sr_gmc_model_str(model)); | |
738 | } | |
739 | ||
740 | /* Bytes 1-4, 11. */ | |
741 | decode_ctmv_2x(bc(devc->buf[1]) | (bc(devc->buf[11]) << 4), devc); | |
742 | decode_spc_2x(bc(devc->buf[2]) | (bc(devc->buf[3]) << 4), devc); | |
743 | decode_rs_2x(bc(devc->buf[4]), devc); | |
744 | ||
745 | /* Bytes 5-10, digits (ls first). */ | |
746 | for (cnt = 0; cnt < 6; cnt++) { | |
747 | dgt = bc(devc->buf[5 + cnt]); | |
748 | if (dgt == 10) { /* Overload */ | |
749 | devc->value = NAN; | |
750 | devc->scale = 1.0; | |
751 | break; | |
752 | } | |
753 | devc->value += pow(10.0, cnt) * dgt; | |
754 | } | |
755 | sr_spew("process_msg_inf_13() value=%f scale=%f scale1000=%d mq=%d " | |
756 | "unit=%d mqflags=0x%02llx", devc->value, devc->scale, | |
757 | devc->scale1000, devc->mq, devc->unit, devc->mqflags); | |
758 | if (devc->value != NAN) | |
759 | devc->value *= devc->scale * pow(1000.0, devc->scale1000); | |
760 | ||
761 | /* Byte 12, Send Interval */ | |
762 | sr_spew("Send interval: %s", decode_send_interval(bc(devc->buf[12]))); | |
763 | ||
764 | /* Create and send packet. */ | |
765 | send_value(sdi); | |
766 | } | |
767 | ||
768 | ||
7b4edcb6 MH |
769 | SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data) |
770 | { | |
f5792417 | 771 | struct sr_dev_inst *sdi; |
7b4edcb6 | 772 | struct dev_context *devc; |
f5792417 MH |
773 | struct sr_serial_dev_inst *serial; |
774 | unsigned char buf, msgt; | |
775 | int len; | |
7b4edcb6 MH |
776 | |
777 | (void)fd; | |
778 | ||
779 | if (!(sdi = cb_data)) | |
780 | return TRUE; | |
781 | ||
782 | if (!(devc = sdi->priv)) | |
783 | return TRUE; | |
784 | ||
f5792417 MH |
785 | serial = sdi->conn; |
786 | ||
787 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
788 | while (GMC_BUFSIZE - devc->buflen - 1 > 0) { | |
789 | len = serial_read(serial, devc->buf + devc->buflen, 1); | |
790 | if (len < 1) | |
791 | break; | |
792 | buf = *(devc->buf + devc->buflen); | |
793 | sr_spew("read 0x%02x/%d/%d", buf, buf, buf & MSGC_MASK); | |
794 | devc->buflen += len; | |
795 | if (!devc->settings_ok) { | |
796 | /* If no device type/settings record processed | |
797 | * yet, wait for one. */ | |
798 | if ((devc->buf[0] & MSGID_MASK) != MSGID_INF) { | |
799 | devc->buflen = 0; | |
800 | continue; | |
801 | } | |
802 | devc->settings_ok = TRUE; | |
803 | } | |
804 | ||
805 | msgt = devc->buf[0] & MSGID_MASK; | |
806 | switch (msgt) { | |
807 | case MSGID_INF: | |
808 | if (devc->buflen == 13) { | |
809 | process_msg_inf_13(sdi); | |
810 | devc->buflen = 0; | |
811 | continue; | |
812 | } | |
813 | else if ((devc->buflen == 10) && | |
814 | (devc->model <= SR_METRAHIT_18S)) { | |
815 | process_msg_inf_10(sdi); | |
816 | devc->buflen = 0; | |
817 | continue; | |
818 | } | |
819 | else if ((devc->buflen >= 5) && | |
820 | (devc->buf[devc->buflen-1] & | |
821 | MSGID_MASK) != MSGID_DATA) { | |
822 | /* Char just received is beginning | |
823 | * of next message */ | |
824 | process_msg_inf_5(sdi); | |
825 | devc->buf[0] =devc->buf[devc->buflen-1]; | |
826 | devc->buflen = 1; | |
827 | continue; | |
828 | } | |
829 | break; | |
830 | case MSGID_DTA: | |
831 | case MSGID_D10: | |
832 | if (devc->buflen == 6) { | |
833 | process_msg_dta_6(sdi); | |
834 | devc->buflen = 0; | |
835 | } | |
836 | break; | |
837 | case MSGID_DATA: | |
838 | sr_err("Comm error, unexpected data byte!"); | |
839 | devc->buflen = 0; | |
840 | break; | |
841 | } | |
842 | } | |
843 | } | |
844 | ||
845 | /* If number of samples or time limit reached, stop aquisition. */ | |
846 | if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) { | |
847 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
848 | } | |
849 | ||
850 | if (devc->limit_msec) { | |
851 | gdouble elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); | |
852 | if ((elapsed_s * 1000) >= devc->limit_msec) { | |
853 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
854 | } | |
7b4edcb6 MH |
855 | } |
856 | ||
857 | return TRUE; | |
858 | } | |
f5792417 MH |
859 | |
860 | ||
861 | /** Decode model in "send mode" */ | |
862 | SR_PRIV int sr_gmc_decode_model_sm(guchar mcode) | |
863 | { | |
864 | if (mcode > 0xf) { | |
865 | sr_err("decode_model(%d): Model code 0..15 expected!", mcode); | |
866 | return SR_METRAHIT_NONE; | |
867 | } | |
868 | ||
869 | switch(mcode) { | |
870 | case 0x04: /* 0100b */ | |
871 | return SR_METRAHIT_12S; | |
872 | case 0x08: /* 1000b */ | |
873 | return SR_METRAHIT_13S14A; | |
874 | case 0x09: /* 1001b */ | |
875 | return SR_METRAHIT_14S; | |
876 | case 0x0A: /* 1010b */ | |
877 | return SR_METRAHIT_15S; | |
878 | case 0x0B: /* 1011b */ | |
879 | return SR_METRAHIT_16S; | |
880 | case 0x06: /* 0110b (undocumented by GMC!) */ | |
881 | return SR_METRAHIT_16I; | |
882 | case 0x0D: /* 1101b */ | |
883 | return SR_METRAHIT_18S; | |
884 | case 0x02: /* 0010b */ | |
885 | return SR_METRAHIT_22SM; | |
886 | case 0x03: /* 0011b */ | |
887 | return SR_METRAHIT_23S; | |
888 | case 0x0f: /* 1111b */ | |
889 | return SR_METRAHIT_24S; | |
890 | case 0x05: /* 0101b */ | |
891 | return SR_METRAHIT_25SM; | |
892 | case 0x01: /* 0001b */ | |
893 | return SR_METRAHIT_26S; | |
894 | case 0x0c: /* 1100b */ | |
895 | return SR_METRAHIT_28S; | |
896 | case 0x0e: /* 1110b */ | |
897 | return SR_METRAHIT_29S; | |
898 | default: | |
899 | sr_err("Unknown model code %d!", mcode); | |
900 | return SR_METRAHIT_NONE; | |
901 | } | |
902 | } | |
903 | ||
904 | /** Decode model in bidirectional mode. | |
905 | \param[in] mcode Model code. | |
906 | ||
907 | \return Model code | |
908 | */ | |
909 | SR_PRIV int sr_gmc_decode_model_bidi(guchar mcode) | |
910 | { | |
911 | switch(mcode) { | |
912 | case 2: return SR_METRAHIT_22SM; | |
913 | case 3: return SR_METRAHIT_23S; | |
914 | case 4: return SR_METRAHIT_24S; | |
915 | case 5: return SR_METRAHIT_25SM; | |
916 | case 1: return SR_METRAHIT_26S; | |
917 | case 12: return SR_METRAHIT_28S; | |
918 | case 14: return SR_METRAHIT_29S; | |
919 | default: | |
920 | sr_err("Unknown model code %d!", mcode); | |
921 | return SR_METRAHIT_NONE; | |
922 | } | |
923 | } | |
924 | ||
925 | SR_PRIV const char* sr_gmc_model_str(enum model mcode) | |
926 | { | |
927 | switch (mcode) { | |
928 | case SR_METRAHIT_NONE: | |
929 | return "-uninitialized model variable-"; | |
930 | case SR_METRAHIT_12S: | |
931 | return "METRAHit 12S"; | |
932 | case SR_METRAHIT_13S14A: | |
933 | return "METRAHit 13S/14A"; | |
934 | case SR_METRAHIT_14S: | |
935 | return "METRAHit 14S"; | |
936 | case SR_METRAHIT_15S: | |
937 | return "METRAHit 15S"; | |
938 | case SR_METRAHIT_16S: | |
939 | return "METRAHit 16S"; | |
940 | case SR_METRAHIT_16I: | |
941 | return "METRAHit 16I"; | |
942 | case SR_METRAHIT_18S: | |
943 | return "METRAHit 18S"; | |
944 | case SR_METRAHIT_22SM: | |
945 | return "METRAHit 22S/M"; | |
946 | case SR_METRAHIT_23S: | |
947 | return "METRAHit 23S"; | |
948 | case SR_METRAHIT_24S: | |
949 | return "METRAHit 24S"; | |
950 | case SR_METRAHIT_25SM: | |
951 | return "METRAHit 25S/M"; | |
952 | case SR_METRAHIT_26S: | |
953 | return "METRAHit 26S"; | |
954 | case SR_METRAHIT_28S: | |
955 | return "METRAHit 28S"; | |
956 | case SR_METRAHIT_29S: | |
957 | return "METRAHit 29S"; | |
958 | default: | |
959 | return "Unknown model code"; | |
960 | } | |
961 | } |