]> sigrok.org Git - libsigrok.git/blame - hardware/victor-dmm/protocol.c
serial: more serial processing flags to turn off
[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
43static void decode_buf(struct dev_context *devc, unsigned char *data)
44{
45 struct sr_datafeed_packet packet;
46 struct sr_datafeed_analog analog;
47 long factor, ivalue;
48 uint8_t digits[4];
49 gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
50 gboolean is_hold, is_max, is_min, is_relative, minus;
51 float fvalue;
52
53 digits[0] = decode_digit(data[12]);
54 digits[1] = decode_digit(data[11]);
55 digits[2] = decode_digit(data[10]);
56 digits[3] = decode_digit(data[9]);
57
58 if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
59 digits[3] == 0x0f)
60 /* The "over limit" (OL) display comes through like this */
61 ivalue = -1;
62 else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
63 /* An invalid digit in any position denotes no value. */
64 ivalue = -2;
65 else {
66 ivalue = digits[0] * 1000;
67 ivalue += digits[1] * 100;
68 ivalue += digits[2] * 10;
69 ivalue += digits[3];
70 }
71
72 /* Decimal point position */
6375e1c3 73 factor = 0;
ff945683
BV
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:
fa773062
UH
88 sr_err("Unknown decimal point byte: 0x%.2x.", data[7]);
89 break;
ff945683
BV
90 }
91
92 /* Minus flag */
93 minus = data[2] & 0x01;
94
95 /* Mode detail symbols on the right side of the digits */
96 is_duty = is_continuity = is_diode = FALSE;
97 switch (data[4]) {
98 case 0x00:
99 /* None. */
100 break;
101 case 0x01:
102 /* Micro */
103 factor += 6;
104 break;
105 case 0x02:
106 /* Milli */
107 factor += 3;
108 break;
109 case 0x04:
110 /* Kilo */
111 ivalue *= 1000;
112 break;
113 case 0x08:
114 /* Mega */
115 ivalue *= 1000000;
116 break;
117 case 0x10:
118 /* Continuity shows up as Ohm + this bit */
119 is_continuity = TRUE;
120 break;
121 case 0x20:
122 /* Diode tester is Volt + this bit */
123 is_diode = TRUE;
124 break;
125 case 0x40:
126 is_duty = TRUE;
127 break;
128 case 0x80:
129 /* Never seen */
fa773062 130 sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]);
ff945683
BV
131 break;
132 default:
fa773062
UH
133 sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]);
134 break;
ff945683
BV
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
fa773062 174 sr_dbg("Unknown measurement mode: %.2x.", data[3]);
ff945683
BV
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;
210 if (ivalue < 0)
211 fvalue = INFINITY;
212 }
213 break;
214 case 0x08:
215 /* Never seen */
fa773062 216 sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
ff945683
BV
217 break;
218 case 0x10:
219 analog.mq = SR_MQ_FREQUENCY;
220 analog.unit = SR_UNIT_HERTZ;
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:
fa773062
UH
235 sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
236 break;
ff945683
BV
237 }
238 if (analog.mq == -1)
239 return;
240
241 if (is_auto)
242 analog.mqflags |= SR_MQFLAG_AUTORANGE;
243 if (is_hold)
244 analog.mqflags |= SR_MQFLAG_HOLD;
245 if (is_max)
246 analog.mqflags |= SR_MQFLAG_MAX;
247 if (is_min)
248 analog.mqflags |= SR_MQFLAG_MIN;
249 if (is_relative)
250 analog.mqflags |= SR_MQFLAG_RELATIVE;
251
252 analog.num_samples = 1;
253 analog.data = &fvalue;
254 packet.type = SR_DF_ANALOG;
255 packet.payload = &analog;
256 sr_session_send(devc->cb_data, &packet);
257
258 devc->num_samples++;
259}
260
261SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
ac3898d2 262{
ac3898d2 263 struct dev_context *devc;
ff945683
BV
264 GString *dbg;
265 int i;
266 unsigned char 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 };
ac3898d2 271
ff945683 272 devc = sdi->priv;
ac3898d2 273
ff945683
BV
274 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
275 if (i == DMM_DATA_SIZE) {
276 /* This DMM outputs all zeroes from time to time, just ignore it. */
277 sr_dbg("Received all zeroes.");
278 return SR_OK;
279 }
ac3898d2 280
ff945683
BV
281 /* Deobfuscate and reorder data. */
282 for (i = 0; i < DMM_DATA_SIZE; i++)
283 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
284
285 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
286 dbg = g_string_sized_new(128);
287 g_string_printf(dbg, "Deobfuscated.");
288 for (i = 0; i < DMM_DATA_SIZE; i++)
289 g_string_append_printf(dbg, " %.2x", data[i]);
290 sr_spew("%s", dbg->str);
291 g_string_free(dbg, TRUE);
ac3898d2
BV
292 }
293
ff945683
BV
294 decode_buf(devc, data);
295
296 return SR_OK;
ac3898d2 297}