]>
Commit | Line | Data |
---|---|---|
6c701476 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | /* | |
22 | * Fortune Semiconductor FS9721_LP3/FS9721B protocol parser. | |
23 | * | |
24 | * FS9721_LP3: 4000 counts (3 3/4 digits) | |
25 | * FS9721B/Q100: 2400 counts (3 2/3 digits) | |
26 | * | |
27 | * Same for both chips: | |
28 | * - Packages: Bare die (78 pins) or QFP-100 | |
29 | * - Communication parameters: Unidirectional, 2400/8n1 | |
30 | * - The protocol seems to be exactly the same. | |
31 | */ | |
32 | ||
33 | #include <string.h> | |
34 | #include <ctype.h> | |
35 | #include <math.h> | |
36 | #include <glib.h> | |
37 | #include "libsigrok.h" | |
38 | #include "libsigrok-internal.h" | |
39 | ||
40 | /* Message logging helpers with driver-specific prefix string. */ | |
41 | #define DRIVER_LOG_DOMAIN "fs9721: " | |
42 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
43 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
44 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
45 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
46 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
47 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
48 | ||
49 | static int parse_digit(uint8_t b) | |
50 | { | |
51 | switch (b) { | |
52 | case 0x7d: | |
53 | return 0; | |
54 | case 0x05: | |
55 | return 1; | |
56 | case 0x5b: | |
57 | return 2; | |
58 | case 0x1f: | |
59 | return 3; | |
60 | case 0x27: | |
61 | return 4; | |
62 | case 0x3e: | |
63 | return 5; | |
64 | case 0x7e: | |
65 | return 6; | |
66 | case 0x15: | |
67 | return 7; | |
68 | case 0x7f: | |
69 | return 8; | |
70 | case 0x3f: | |
71 | return 9; | |
72 | default: | |
73 | sr_err("Invalid digit byte: 0x%02x.", b); | |
74 | return -1; | |
75 | } | |
76 | } | |
77 | ||
78 | /** | |
79 | * Parse the numerical value from a protocol packet. | |
80 | * | |
81 | * @param buf Buffer containing the 14-byte protocol packet. | |
82 | * @param result Pointer to a float variable. That variable will contain the | |
83 | * result value upon parsing success. | |
84 | * | |
85 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the result | |
86 | * variable contents are undefined and should not be used. | |
87 | */ | |
88 | static int parse_value(const uint8_t *buf, float *result) | |
89 | { | |
90 | int i, sign, intval = 0, digits[4]; | |
91 | uint8_t digit_bytes[4]; | |
92 | float floatval; | |
93 | ||
94 | /* Byte 1: LCD SEG2 */ | |
95 | sign = ((buf[1] & (1 << 3)) != 0) ? -1 : 1; | |
96 | ||
97 | /* | |
98 | * Bytes 1-8: Value (4 decimal digits, sign, decimal point) | |
99 | * | |
100 | * Over limit: "0L" (LCD), 0x00 0x7d 0x68 0x00 (digit bytes). | |
101 | */ | |
102 | ||
103 | /* Merge the two nibbles for a digit into one byte. */ | |
104 | for (i = 0; i < 4; i++) { | |
105 | digit_bytes[i] = ((buf[1 + (i * 2)] & 0x0f) << 4); | |
106 | digit_bytes[i] |= (buf[1 + (i * 2) + 1] & 0x0f); | |
107 | ||
108 | /* Bit 7 in the byte is not part of the digit. */ | |
109 | digit_bytes[i] &= ~(1 << 7); | |
110 | } | |
111 | ||
112 | /* Check for "OL". */ | |
113 | if (digit_bytes[0] == 0x00 && digit_bytes[1] == 0x7d && | |
114 | digit_bytes[2] == 0x68 && digit_bytes[3] == 0x00) { | |
115 | sr_spew("Over limit."); | |
116 | *result = INFINITY; | |
117 | return SR_OK; | |
118 | } | |
119 | ||
120 | /* Parse the digits. */ | |
121 | for (i = 0; i < 4; i++) | |
122 | digits[i] = parse_digit(digit_bytes[i]); | |
123 | sr_spew("Digits: %02x %02x %02x %02x (%d%d%d%d).", | |
124 | digit_bytes[0], digit_bytes[1], digit_bytes[2], digit_bytes[3], | |
125 | digits[0], digits[1], digits[2], digits[3]); | |
126 | ||
127 | /* Merge all digits into an integer value. */ | |
128 | for (i = 0; i < 4; i++) { | |
129 | intval *= 10; | |
130 | intval += digits[i]; | |
131 | } | |
132 | ||
133 | /* Store the value in a float variable. */ | |
134 | floatval = (float)intval; | |
135 | ||
136 | /* Decimal point position. */ | |
137 | if ((buf[3] & (1 << 3)) != 0) { | |
138 | floatval /= 1000; | |
139 | sr_spew("Decimal point after first digit."); | |
140 | } else if ((buf[5] & (1 << 3)) != 0) { | |
141 | floatval /= 100; | |
142 | sr_spew("Decimal point after second digit."); | |
143 | } else if ((buf[7] & (1 << 3)) != 0) { | |
144 | floatval /= 10; | |
145 | sr_spew("Decimal point after third digit."); | |
146 | } else { | |
147 | sr_spew("No decimal point in the number."); | |
148 | } | |
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 | ||
160 | /** | |
161 | * Parse various flags in a protocol packet. | |
162 | * | |
163 | * @param buf Buffer containing the 14-byte protocol packet. | |
164 | * @param floatval Pointer to a float variable which should contain the value | |
165 | * parsed using parse_value(). That variable will be modified | |
166 | * in-place depending on the flags in the protocol packet. | |
167 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be | |
168 | * filled with the relevant data according to the flags in the | |
169 | * protocol packet. | |
170 | * | |
171 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the 'floatval' | |
172 | * and 'analog' variable contents are undefined and should not be used. | |
173 | */ | |
174 | static int parse_flags(const uint8_t *buf, float *floatval, | |
175 | struct sr_datafeed_analog *analog) | |
176 | { | |
177 | gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo; | |
178 | gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad; | |
179 | gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat; | |
180 | gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00; | |
181 | ||
182 | /* Byte 0: LCD SEG1 */ | |
183 | is_ac = (buf[0] & (1 << 3)) != 0; | |
184 | is_dc = (buf[0] & (1 << 2)) != 0; | |
185 | is_auto = (buf[0] & (1 << 1)) != 0; | |
186 | is_rs232 = (buf[0] & (1 << 0)) != 0; | |
187 | ||
188 | /* Byte 9: LCD SEG10 */ | |
189 | is_micro = (buf[9] & (1 << 3)) != 0; | |
190 | is_nano = (buf[9] & (1 << 2)) != 0; | |
191 | is_kilo = (buf[9] & (1 << 1)) != 0; | |
192 | is_diode = (buf[9] & (1 << 0)) != 0; | |
193 | ||
194 | /* Byte 10: LCD SEG11 */ | |
195 | is_milli = (buf[10] & (1 << 3)) != 0; | |
196 | is_percent = (buf[10] & (1 << 2)) != 0; | |
197 | is_mega = (buf[10] & (1 << 1)) != 0; | |
198 | is_beep = (buf[10] & (1 << 0)) != 0; | |
199 | ||
200 | /* Byte 11: LCD SEG12 */ | |
201 | is_farad = (buf[11] & (1 << 3)) != 0; | |
202 | is_ohm = (buf[11] & (1 << 2)) != 0; | |
203 | is_rel = (buf[11] & (1 << 1)) != 0; | |
204 | is_hold = (buf[11] & (1 << 0)) != 0; | |
205 | ||
206 | /* Byte 12: LCD SEG13 */ | |
207 | is_ampere = (buf[12] & (1 << 3)) != 0; | |
208 | is_volt = (buf[12] & (1 << 2)) != 0; | |
209 | is_hz = (buf[12] & (1 << 1)) != 0; | |
210 | is_bat = (buf[12] & (1 << 0)) != 0; | |
211 | ||
212 | /* Byte 13: LCD SEG14 */ | |
213 | is_c2c1_11 = (buf[13] & (1 << 3)) != 0; | |
214 | is_c2c1_10 = (buf[13] & (1 << 2)) != 0; | |
215 | is_c2c1_01 = (buf[13] & (1 << 1)) != 0; | |
216 | is_c2c1_00 = (buf[13] & (1 << 0)) != 0; | |
217 | ||
218 | /* Factors */ | |
219 | if (is_nano) | |
220 | *floatval /= 1000000000; | |
221 | if (is_micro) | |
222 | *floatval /= 1000000; | |
223 | if (is_milli) | |
224 | *floatval /= 1000; | |
225 | if (is_kilo) | |
226 | *floatval *= 1000; | |
227 | if (is_mega) | |
228 | *floatval *= 1000000; | |
229 | ||
230 | /* Measurement modes */ | |
231 | if (is_volt) { | |
232 | analog->mq = SR_MQ_VOLTAGE; | |
233 | analog->unit = SR_UNIT_VOLT; | |
234 | } | |
235 | if (is_ampere) { | |
236 | analog->mq = SR_MQ_CURRENT; | |
237 | analog->unit = SR_UNIT_AMPERE; | |
238 | } | |
239 | if (is_ohm) { | |
240 | analog->mq = SR_MQ_RESISTANCE; | |
241 | analog->unit = SR_UNIT_OHM; | |
242 | } | |
243 | if (is_hz) { | |
244 | analog->mq = SR_MQ_FREQUENCY; | |
245 | analog->unit = SR_UNIT_HERTZ; | |
246 | } | |
247 | if (is_farad) { | |
248 | analog->mq = SR_MQ_CAPACITANCE; | |
249 | analog->unit = SR_UNIT_FARAD; | |
250 | } | |
251 | if (is_beep) { | |
252 | analog->mq = SR_MQ_CONTINUITY; | |
253 | analog->unit = SR_UNIT_BOOLEAN; | |
254 | *floatval = (*floatval < 0.0) ? 0.0 : 1.0; | |
255 | } | |
256 | if (is_diode) { | |
257 | analog->mq = SR_MQ_VOLTAGE; | |
258 | analog->unit = SR_UNIT_VOLT; | |
259 | } | |
260 | if (is_percent) { | |
261 | analog->mq = SR_MQ_DUTY_CYCLE; | |
262 | analog->unit = SR_UNIT_PERCENTAGE; | |
263 | } | |
264 | ||
265 | /* Measurement related flags */ | |
266 | if (is_ac) | |
267 | analog->mqflags |= SR_MQFLAG_AC; | |
268 | if (is_dc) | |
269 | analog->mqflags |= SR_MQFLAG_DC; | |
270 | if (is_auto) | |
271 | analog->mqflags |= SR_MQFLAG_AUTORANGE; | |
272 | if (is_hold) | |
273 | analog->mqflags |= SR_MQFLAG_HOLD; | |
274 | if (is_rel) | |
275 | analog->mqflags |= SR_MQFLAG_RELATIVE; | |
276 | ||
277 | /* Other flags */ | |
278 | if (is_rs232) | |
279 | sr_spew("RS232 enabled."); | |
280 | if (is_bat) | |
281 | sr_spew("Battery is low."); | |
282 | if (is_c2c1_00) | |
283 | sr_spew("User-defined LCD symbol 0 is active."); | |
284 | if (is_c2c1_01) | |
285 | sr_spew("User-defined LCD symbol 1 is active."); | |
286 | if (is_c2c1_10) | |
287 | sr_spew("User-defined LCD symbol 2 is active."); | |
288 | if (is_c2c1_11) | |
289 | sr_spew("User-defined LCD symbol 3 is active."); | |
290 | ||
291 | return SR_OK; | |
292 | } | |
293 | ||
294 | /** | |
295 | * Parse a protocol packet. | |
296 | * | |
297 | * @param buf Buffer containing the 14-byte protocol packet. | |
298 | * @param floatval Pointer to a float variable. That variable will be modified | |
299 | * in-place depending on the protocol packet. | |
300 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be | |
301 | * filled with data according to the protocol packet. | |
302 | * | |
303 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the | |
304 | * 'analog' variable contents are undefined and should not be used. | |
305 | */ | |
306 | SR_PRIV int sr_dmm_parse_fs9721(const uint8_t *buf, float *floatval, | |
307 | struct sr_datafeed_analog *analog) | |
308 | { | |
309 | int ret; | |
310 | ||
311 | if ((ret = parse_value(buf, floatval)) != SR_OK) { | |
312 | sr_err("Error parsing value: %d.", ret); | |
313 | return ret; | |
314 | } | |
315 | ||
316 | if ((ret = parse_flags(buf, floatval, analog)) != SR_OK) { | |
317 | sr_err("Error parsing flags: %d.", ret); | |
318 | return ret; | |
319 | } | |
320 | ||
321 | return SR_OK; | |
322 | } |