]>
Commit | Line | Data |
---|---|---|
79081ec8 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
79081ec8 UH |
3 | * |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
79081ec8 UH |
18 | */ |
19 | ||
20 | /* | |
21 | * Fortune Semiconductor FS9922-DMM3/FS9922-DMM4 protocol parser. | |
22 | */ | |
23 | ||
6ec6c43b | 24 | #include <config.h> |
79081ec8 UH |
25 | #include <string.h> |
26 | #include <ctype.h> | |
27 | #include <math.h> | |
28 | #include <glib.h> | |
c1aae900 | 29 | #include <libsigrok/libsigrok.h> |
79081ec8 UH |
30 | #include "libsigrok-internal.h" |
31 | ||
3544f848 | 32 | #define LOG_PREFIX "fs9922" |
79081ec8 | 33 | |
913abe83 UH |
34 | static gboolean flags_valid(const struct fs9922_info *info) |
35 | { | |
36 | int count; | |
37 | ||
38 | /* Does the packet have more than one multiplier? */ | |
39 | count = 0; | |
40 | count += (info->is_nano) ? 1 : 0; | |
41 | count += (info->is_micro) ? 1 : 0; | |
42 | count += (info->is_milli) ? 1 : 0; | |
43 | count += (info->is_kilo) ? 1 : 0; | |
44 | count += (info->is_mega) ? 1 : 0; | |
45 | if (count > 1) { | |
ec5186f9 | 46 | sr_dbg("More than one multiplier detected in packet."); |
913abe83 UH |
47 | return FALSE; |
48 | } | |
49 | ||
649a4cd6 UH |
50 | /* |
51 | * Does the packet "measure" more than one type of value? | |
52 | * | |
53 | * Note: In "diode mode", both is_diode and is_volt will be set. | |
54 | * That is a valid use-case, so we don't want to error out below | |
55 | * if it happens. Thus, we don't check for is_diode here. | |
56 | */ | |
913abe83 | 57 | count = 0; |
649a4cd6 | 58 | // count += (info->is_diode) ? 1 : 0; |
913abe83 UH |
59 | count += (info->is_percent) ? 1 : 0; |
60 | count += (info->is_volt) ? 1 : 0; | |
61 | count += (info->is_ampere) ? 1 : 0; | |
62 | count += (info->is_ohm) ? 1 : 0; | |
63 | count += (info->is_hfe) ? 1 : 0; | |
64 | count += (info->is_hertz) ? 1 : 0; | |
65 | count += (info->is_farad) ? 1 : 0; | |
66 | count += (info->is_celsius) ? 1 : 0; | |
67 | count += (info->is_fahrenheit) ? 1 : 0; | |
68 | if (count > 1) { | |
ec5186f9 | 69 | sr_dbg("More than one measurement type detected in packet."); |
913abe83 UH |
70 | return FALSE; |
71 | } | |
72 | ||
73 | /* Both AC and DC set? */ | |
74 | if (info->is_ac && info->is_dc) { | |
ec5186f9 | 75 | sr_dbg("Both AC and DC flags detected in packet."); |
913abe83 UH |
76 | return FALSE; |
77 | } | |
78 | ||
79 | /* Both Celsius and Fahrenheit set? */ | |
80 | if (info->is_celsius && info->is_fahrenheit) { | |
ec5186f9 | 81 | sr_dbg("Both Celsius and Fahrenheit flags detected in packet."); |
913abe83 UH |
82 | return FALSE; |
83 | } | |
84 | ||
85 | return TRUE; | |
86 | } | |
87 | ||
aad6d8d2 | 88 | static int parse_value(const uint8_t *buf, float *result, int *exponent) |
79081ec8 UH |
89 | { |
90 | int sign, intval; | |
91 | float floatval; | |
92 | ||
93 | /* Byte 0: Sign ('+' or '-') */ | |
94 | if (buf[0] == '+') { | |
95 | sign = 1; | |
96 | } else if (buf[0] == '-') { | |
97 | sign = -1; | |
98 | } else { | |
ec5186f9 | 99 | sr_dbg("Invalid sign byte: 0x%02x.", buf[0]); |
79081ec8 UH |
100 | return SR_ERR; |
101 | } | |
102 | ||
103 | /* | |
104 | * Bytes 1-4: Value (4 decimal digits) | |
105 | * | |
106 | * Over limit: "0.L" on the display, "?0:?" as protocol "digits". | |
107 | */ | |
108 | if (buf[1] == '?' && buf[2] == '0' && buf[3] == ':' && buf[4] == '?') { | |
109 | sr_spew("Over limit."); | |
110 | *result = INFINITY; | |
111 | return SR_OK; | |
112 | } else if (!isdigit(buf[1]) || !isdigit(buf[2]) || | |
113 | !isdigit(buf[3]) || !isdigit(buf[4])) { | |
ec5186f9 | 114 | sr_dbg("Value contained invalid digits: %02x %02x %02x %02x (" |
6433156c DE |
115 | "%c %c %c %c).", |
116 | buf[1], buf[2], buf[3], buf[4], | |
117 | buf[1], buf[2], buf[3], buf[4]); | |
79081ec8 UH |
118 | return SR_ERR; |
119 | } | |
120 | intval = 0; | |
121 | intval += (buf[1] - '0') * 1000; | |
122 | intval += (buf[2] - '0') * 100; | |
123 | intval += (buf[3] - '0') * 10; | |
124 | intval += (buf[4] - '0') * 1; | |
125 | ||
126 | floatval = (float)intval; | |
127 | ||
128 | /* Byte 5: Always ' ' (space, 0x20) */ | |
129 | ||
130 | /* | |
131 | * Byte 6: Decimal point position ('0', '1', '2', or '4') | |
132 | * | |
133 | * Note: The Fortune Semiconductor FS9922-DMM3/4 datasheets both have | |
134 | * an error/typo here. They claim that the values '0'/'1'/'2'/'3' are | |
135 | * used, but '0'/'1'/'2'/'4' is actually correct. | |
136 | */ | |
137 | if (buf[6] != '0' && buf[6] != '1' && buf[6] != '2' && buf[6] != '4') { | |
ec5186f9 | 138 | sr_dbg("Invalid decimal point value: 0x%02x.", buf[6]); |
79081ec8 UH |
139 | return SR_ERR; |
140 | } | |
141 | if (buf[6] == '0') | |
aad6d8d2 | 142 | *exponent = 0; |
79081ec8 | 143 | else if (buf[6] == '1') |
aad6d8d2 | 144 | *exponent = -3; |
79081ec8 | 145 | else if (buf[6] == '2') |
aad6d8d2 | 146 | *exponent = -2; |
79081ec8 | 147 | else if (buf[6] == '4') |
aad6d8d2 | 148 | *exponent = -1; |
79081ec8 UH |
149 | |
150 | /* Apply sign. */ | |
151 | floatval *= sign; | |
152 | ||
153 | sr_spew("The display value is %f.", floatval); | |
154 | ||
155 | *result = floatval; | |
156 | ||
157 | return SR_OK; | |
158 | } | |
159 | ||
913abe83 | 160 | static void parse_flags(const uint8_t *buf, struct fs9922_info *info) |
79081ec8 | 161 | { |
79081ec8 UH |
162 | /* Z1/Z2/Z3/Z4 are bits for user-defined LCD symbols (on/off). */ |
163 | ||
164 | /* Byte 7 */ | |
165 | /* Bit 7: Always 0 */ | |
166 | /* Bit 6: Always 0 */ | |
913abe83 UH |
167 | info->is_auto = (buf[7] & (1 << 5)) != 0; |
168 | info->is_dc = (buf[7] & (1 << 4)) != 0; | |
169 | info->is_ac = (buf[7] & (1 << 3)) != 0; | |
170 | info->is_rel = (buf[7] & (1 << 2)) != 0; | |
171 | info->is_hold = (buf[7] & (1 << 1)) != 0; | |
172 | info->is_bpn = (buf[7] & (1 << 0)) != 0; /* Bargraph shown */ | |
79081ec8 UH |
173 | |
174 | /* Byte 8 */ | |
913abe83 UH |
175 | info->is_z1 = (buf[8] & (1 << 7)) != 0; /* User symbol 1 */ |
176 | info->is_z2 = (buf[8] & (1 << 6)) != 0; /* User symbol 2 */ | |
177 | info->is_max = (buf[8] & (1 << 5)) != 0; | |
178 | info->is_min = (buf[8] & (1 << 4)) != 0; | |
179 | info->is_apo = (buf[8] & (1 << 3)) != 0; /* Auto-poweroff on */ | |
180 | info->is_bat = (buf[8] & (1 << 2)) != 0; /* Battery low */ | |
181 | info->is_nano = (buf[8] & (1 << 1)) != 0; | |
182 | info->is_z3 = (buf[8] & (1 << 0)) != 0; /* User symbol 3 */ | |
79081ec8 UH |
183 | |
184 | /* Byte 9 */ | |
913abe83 UH |
185 | info->is_micro = (buf[9] & (1 << 7)) != 0; |
186 | info->is_milli = (buf[9] & (1 << 6)) != 0; | |
187 | info->is_kilo = (buf[9] & (1 << 5)) != 0; | |
188 | info->is_mega = (buf[9] & (1 << 4)) != 0; | |
189 | info->is_beep = (buf[9] & (1 << 3)) != 0; | |
190 | info->is_diode = (buf[9] & (1 << 2)) != 0; | |
191 | info->is_percent = (buf[9] & (1 << 1)) != 0; | |
98494dc8 | 192 | info->is_z4 = (buf[9] & (1 << 0)) != 0; /* User symbol 4 */ |
79081ec8 UH |
193 | |
194 | /* Byte 10 */ | |
913abe83 UH |
195 | info->is_volt = (buf[10] & (1 << 7)) != 0; |
196 | info->is_ampere = (buf[10] & (1 << 6)) != 0; | |
197 | info->is_ohm = (buf[10] & (1 << 5)) != 0; | |
198 | info->is_hfe = (buf[10] & (1 << 4)) != 0; | |
199 | info->is_hertz = (buf[10] & (1 << 3)) != 0; | |
200 | info->is_farad = (buf[10] & (1 << 2)) != 0; | |
201 | info->is_celsius = (buf[10] & (1 << 1)) != 0; /* Only FS9922-DMM4 */ | |
202 | info->is_fahrenheit = (buf[10] & (1 << 0)) != 0; /* Only FS9922-DMM4 */ | |
79081ec8 UH |
203 | |
204 | /* | |
205 | * Byte 11: Bar graph | |
206 | * | |
207 | * Bit 7 contains the sign of the bargraph number (if the bit is set, | |
208 | * the number is negative), bits 6..0 contain the actual number. | |
209 | * Valid range: 0-40 (FS9922-DMM3), 0-60 (FS9922-DMM4). | |
210 | * | |
211 | * Upon "over limit" the bargraph value is 1 count above the highest | |
212 | * valid number (i.e. 41 or 61, depending on chip). | |
213 | */ | |
913abe83 UH |
214 | if (info->is_bpn) { |
215 | info->bargraph_sign = ((buf[11] & (1 << 7)) != 0) ? -1 : 1; | |
216 | info->bargraph_value = (buf[11] & 0x7f); | |
217 | info->bargraph_value *= info->bargraph_sign; | |
79081ec8 UH |
218 | } |
219 | ||
220 | /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */ | |
221 | ||
222 | /* Byte 13: Always '\n' (newline, 0x0a, 10) */ | |
913abe83 | 223 | } |
79081ec8 | 224 | |
b02bb45f | 225 | static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, |
aad6d8d2 | 226 | int *exponent, const struct fs9922_info *info) |
913abe83 | 227 | { |
79081ec8 | 228 | /* Factors */ |
913abe83 | 229 | if (info->is_nano) |
aad6d8d2 | 230 | *exponent -= 9; |
913abe83 | 231 | if (info->is_micro) |
aad6d8d2 | 232 | *exponent -= 6; |
913abe83 | 233 | if (info->is_milli) |
aad6d8d2 | 234 | *exponent -= 3; |
913abe83 | 235 | if (info->is_kilo) |
aad6d8d2 | 236 | *exponent += 3; |
913abe83 | 237 | if (info->is_mega) |
aad6d8d2 AJ |
238 | *exponent += 6; |
239 | *floatval *= powf(10, *exponent); | |
79081ec8 UH |
240 | |
241 | /* Measurement modes */ | |
649a4cd6 UH |
242 | if (info->is_volt || info->is_diode) { |
243 | /* Note: In "diode mode" both is_diode and is_volt are set. */ | |
b02bb45f UH |
244 | analog->meaning->mq = SR_MQ_VOLTAGE; |
245 | analog->meaning->unit = SR_UNIT_VOLT; | |
79081ec8 | 246 | } |
913abe83 | 247 | if (info->is_ampere) { |
b02bb45f UH |
248 | analog->meaning->mq = SR_MQ_CURRENT; |
249 | analog->meaning->unit = SR_UNIT_AMPERE; | |
79081ec8 | 250 | } |
913abe83 | 251 | if (info->is_ohm) { |
b02bb45f UH |
252 | analog->meaning->mq = SR_MQ_RESISTANCE; |
253 | analog->meaning->unit = SR_UNIT_OHM; | |
79081ec8 | 254 | } |
913abe83 | 255 | if (info->is_hfe) { |
b02bb45f UH |
256 | analog->meaning->mq = SR_MQ_GAIN; |
257 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
79081ec8 | 258 | } |
913abe83 | 259 | if (info->is_hertz) { |
b02bb45f UH |
260 | analog->meaning->mq = SR_MQ_FREQUENCY; |
261 | analog->meaning->unit = SR_UNIT_HERTZ; | |
79081ec8 | 262 | } |
913abe83 | 263 | if (info->is_farad) { |
b02bb45f UH |
264 | analog->meaning->mq = SR_MQ_CAPACITANCE; |
265 | analog->meaning->unit = SR_UNIT_FARAD; | |
79081ec8 | 266 | } |
913abe83 | 267 | if (info->is_celsius) { |
b02bb45f UH |
268 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
269 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
79081ec8 | 270 | } |
913abe83 | 271 | if (info->is_fahrenheit) { |
b02bb45f UH |
272 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
273 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
79081ec8 | 274 | } |
913abe83 | 275 | if (info->is_beep) { |
b02bb45f UH |
276 | analog->meaning->mq = SR_MQ_CONTINUITY; |
277 | analog->meaning->unit = SR_UNIT_BOOLEAN; | |
ad00a54d | 278 | *floatval = (*floatval == INFINITY) ? 0.0 : 1.0; |
79081ec8 | 279 | } |
913abe83 | 280 | if (info->is_percent) { |
b02bb45f UH |
281 | analog->meaning->mq = SR_MQ_DUTY_CYCLE; |
282 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
79081ec8 UH |
283 | } |
284 | ||
285 | /* Measurement related flags */ | |
913abe83 | 286 | if (info->is_ac) |
b02bb45f | 287 | analog->meaning->mqflags |= SR_MQFLAG_AC; |
913abe83 | 288 | if (info->is_dc) |
b02bb45f | 289 | analog->meaning->mqflags |= SR_MQFLAG_DC; |
913abe83 | 290 | if (info->is_auto) |
b02bb45f | 291 | analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; |
a6ed50f4 | 292 | if (info->is_diode) |
b02bb45f | 293 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; |
913abe83 | 294 | if (info->is_hold) |
b02bb45f | 295 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; |
913abe83 | 296 | if (info->is_max) |
b02bb45f | 297 | analog->meaning->mqflags |= SR_MQFLAG_MAX; |
913abe83 | 298 | if (info->is_min) |
b02bb45f | 299 | analog->meaning->mqflags |= SR_MQFLAG_MIN; |
913abe83 | 300 | if (info->is_rel) |
b02bb45f | 301 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; |
79081ec8 UH |
302 | |
303 | /* Other flags */ | |
913abe83 | 304 | if (info->is_apo) |
79081ec8 | 305 | sr_spew("Automatic power-off function is active."); |
913abe83 | 306 | if (info->is_bat) |
79081ec8 | 307 | sr_spew("Battery is low."); |
913abe83 | 308 | if (info->is_z1) |
79081ec8 | 309 | sr_spew("User-defined LCD symbol 1 is active."); |
913abe83 | 310 | if (info->is_z2) |
79081ec8 | 311 | sr_spew("User-defined LCD symbol 2 is active."); |
913abe83 | 312 | if (info->is_z3) |
79081ec8 | 313 | sr_spew("User-defined LCD symbol 3 is active."); |
913abe83 | 314 | if (info->is_z4) |
79081ec8 | 315 | sr_spew("User-defined LCD symbol 4 is active."); |
913abe83 UH |
316 | if (info->is_bpn) |
317 | sr_spew("The bargraph value is %d.", info->bargraph_value); | |
318 | else | |
319 | sr_spew("The bargraph is not active."); | |
79081ec8 | 320 | |
913abe83 UH |
321 | } |
322 | ||
323 | SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf) | |
324 | { | |
325 | struct fs9922_info info; | |
326 | ||
327 | /* Byte 0: Sign (must be '+' or '-') */ | |
328 | if (buf[0] != '+' && buf[0] != '-') | |
329 | return FALSE; | |
330 | ||
331 | /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */ | |
332 | /* Byte 13: Always '\n' (newline, 0x0a, 10) */ | |
333 | if (buf[12] != '\r' || buf[13] != '\n') | |
334 | return FALSE; | |
335 | ||
336 | parse_flags(buf, &info); | |
337 | ||
338 | return flags_valid(&info); | |
79081ec8 UH |
339 | } |
340 | ||
341 | /** | |
913abe83 | 342 | * Parse a protocol packet. |
79081ec8 | 343 | * |
913abe83 UH |
344 | * @param buf Buffer containing the protocol packet. Must not be NULL. |
345 | * @param floatval Pointer to a float variable. That variable will contain the | |
346 | * result value upon parsing success. Must not be NULL. | |
b02bb45f | 347 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be |
79081ec8 | 348 | * filled with data according to the protocol packet. |
913abe83 UH |
349 | * Must not be NULL. |
350 | * @param info Pointer to a struct fs9922_info. The struct will be filled | |
351 | * with data according to the protocol packet. Must not be NULL. | |
79081ec8 UH |
352 | * |
353 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the | |
354 | * 'analog' variable contents are undefined and should not be used. | |
355 | */ | |
913abe83 | 356 | SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval, |
b02bb45f | 357 | struct sr_datafeed_analog *analog, void *info) |
79081ec8 | 358 | { |
aad6d8d2 | 359 | int ret, exponent = 0; |
913abe83 UH |
360 | struct fs9922_info *info_local; |
361 | ||
362 | info_local = (struct fs9922_info *)info; | |
79081ec8 | 363 | |
aad6d8d2 | 364 | if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) { |
ec5186f9 | 365 | sr_dbg("Error parsing value: %d.", ret); |
79081ec8 UH |
366 | return ret; |
367 | } | |
368 | ||
913abe83 | 369 | parse_flags(buf, info_local); |
aad6d8d2 AJ |
370 | handle_flags(analog, floatval, &exponent, info_local); |
371 | ||
d9251a2c | 372 | analog->encoding->digits = -exponent; |
aad6d8d2 | 373 | analog->spec->spec_digits = -exponent; |
79081ec8 UH |
374 | |
375 | return SR_OK; | |
376 | } | |
e52bb9be | 377 | |
b02bb45f | 378 | SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info) |
e52bb9be UH |
379 | { |
380 | struct fs9922_info *info_local; | |
381 | ||
382 | info_local = (struct fs9922_info *)info; | |
383 | ||
384 | /* User-defined z1 flag means "diode mode". */ | |
385 | if (info_local->is_z1) { | |
b02bb45f UH |
386 | analog->meaning->mq = SR_MQ_VOLTAGE; |
387 | analog->meaning->unit = SR_UNIT_VOLT; | |
388 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; | |
e52bb9be UH |
389 | } |
390 | } |