]>
Commit | Line | Data |
---|---|---|
8d9c8554 AJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> | |
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 | ||
6ec6c43b | 20 | #include <config.h> |
ecaa89af AJ |
21 | #include <string.h> |
22 | #include <math.h> | |
8d9c8554 AJ |
23 | #include "protocol.h" |
24 | ||
ecaa89af AJ |
25 | #define USB_TIMEOUT 500 |
26 | ||
329733d9 | 27 | static const char char_map[128] = { |
ecaa89af AJ |
28 | [0x20] = '-', |
29 | [0x5F] = '0', | |
30 | [0x50] = '1', | |
31 | [0x6D] = '2', | |
32 | [0x7C] = '3', | |
33 | [0x72] = '4', | |
34 | [0x3E] = '5', | |
35 | [0x3F] = '6', | |
36 | [0x54] = '7', | |
37 | [0x7F] = '8', | |
38 | [0x7E] = '9', | |
39 | [0x0F] = 'C', | |
40 | [0x27] = 'F', | |
41 | [0x0B] = 'L', | |
42 | [0x79] = 'd', | |
43 | [0x10] = 'i', | |
44 | [0x39] = 'o', | |
45 | }; | |
46 | ||
47 | static int brymen_bm86x_parse_digits(const unsigned char *buf, int length, | |
48 | char *str, float *floatval, | |
49 | char *temp_unit, int flag) | |
50 | { | |
51 | char c, *p = str; | |
52 | int i, ret; | |
53 | ||
54 | if (buf[0] & flag) | |
55 | *p++ = '-'; | |
56 | for (i = 0; i < length; i++) { | |
57 | if (i && i < 5 && buf[i+1] & 0x01) | |
58 | *p++ = '.'; | |
59 | c = char_map[buf[i+1] >> 1]; | |
60 | if (i == 5 && (c == 'C' || c == 'F')) | |
61 | *temp_unit = c; | |
62 | else if (c) | |
63 | *p++ = c; | |
64 | } | |
65 | *p = 0; | |
66 | ||
67 | if ((ret = sr_atof_ascii(str, floatval))) { | |
68 | sr_dbg("invalid float string: '%s'", str); | |
69 | return ret; | |
70 | } | |
71 | ||
72 | return SR_OK; | |
73 | } | |
74 | ||
75 | static void brymen_bm86x_parse(unsigned char *buf, float *floatval, | |
a46f869b | 76 | struct sr_datafeed_analog *analog) |
ecaa89af AJ |
77 | { |
78 | char str[16], temp_unit; | |
79 | int ret1, ret2, over_limit; | |
80 | ||
81 | ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0], | |
82 | &temp_unit, 0x80); | |
83 | over_limit = strstr(str, "0L") || strstr(str, "0.L"); | |
84 | ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1], | |
85 | &temp_unit, 0x10); | |
86 | ||
87 | /* main display */ | |
88 | if (ret1 == SR_OK || over_limit) { | |
89 | /* SI unit */ | |
90 | if (buf[8] & 0x01) { | |
a46f869b UH |
91 | analog[0].meaning->mq = SR_MQ_VOLTAGE; |
92 | analog[0].meaning->unit = SR_UNIT_VOLT; | |
ecaa89af | 93 | if (!strcmp(str, "diod")) |
a46f869b | 94 | analog[0].meaning->mqflags |= SR_MQFLAG_DIODE; |
ecaa89af | 95 | } else if (buf[14] & 0x80) { |
a46f869b UH |
96 | analog[0].meaning->mq = SR_MQ_CURRENT; |
97 | analog[0].meaning->unit = SR_UNIT_AMPERE; | |
ecaa89af | 98 | } else if (buf[14] & 0x20) { |
a46f869b UH |
99 | analog[0].meaning->mq = SR_MQ_CAPACITANCE; |
100 | analog[0].meaning->unit = SR_UNIT_FARAD; | |
ecaa89af | 101 | } else if (buf[14] & 0x10) { |
a46f869b UH |
102 | analog[0].meaning->mq = SR_MQ_CONDUCTANCE; |
103 | analog[0].meaning->unit = SR_UNIT_SIEMENS; | |
ecaa89af | 104 | } else if (buf[15] & 0x01) { |
a46f869b UH |
105 | analog[0].meaning->mq = SR_MQ_FREQUENCY; |
106 | analog[0].meaning->unit = SR_UNIT_HERTZ; | |
ecaa89af | 107 | } else if (buf[10] & 0x01) { |
a46f869b UH |
108 | analog[0].meaning->mq = SR_MQ_CONTINUITY; |
109 | analog[0].meaning->unit = SR_UNIT_OHM; | |
ecaa89af | 110 | } else if (buf[15] & 0x10) { |
a46f869b UH |
111 | analog[0].meaning->mq = SR_MQ_RESISTANCE; |
112 | analog[0].meaning->unit = SR_UNIT_OHM; | |
ecaa89af | 113 | } else if (buf[15] & 0x02) { |
a46f869b UH |
114 | analog[0].meaning->mq = SR_MQ_POWER; |
115 | analog[0].meaning->unit = SR_UNIT_DECIBEL_MW; | |
ecaa89af | 116 | } else if (buf[15] & 0x80) { |
a46f869b UH |
117 | analog[0].meaning->mq = SR_MQ_DUTY_CYCLE; |
118 | analog[0].meaning->unit = SR_UNIT_PERCENTAGE; | |
ecaa89af | 119 | } else if (buf[ 2] & 0x0A) { |
a46f869b | 120 | analog[0].meaning->mq = SR_MQ_TEMPERATURE; |
ecaa89af | 121 | if (temp_unit == 'F') |
a46f869b | 122 | analog[0].meaning->unit = SR_UNIT_FAHRENHEIT; |
ecaa89af | 123 | else |
a46f869b | 124 | analog[0].meaning->unit = SR_UNIT_CELSIUS; |
ecaa89af AJ |
125 | } |
126 | ||
127 | /* when MIN MAX and AVG are displayed at the same time, remove them */ | |
128 | if ((buf[1] & 0xE0) == 0xE0) | |
129 | buf[1] &= ~0xE0; | |
130 | ||
131 | /* AC/DC/Auto flags */ | |
a46f869b UH |
132 | if (buf[1] & 0x10) analog[0].meaning->mqflags |= SR_MQFLAG_DC; |
133 | if (buf[2] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AC; | |
134 | if (buf[1] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AUTORANGE; | |
135 | if (buf[1] & 0x08) analog[0].meaning->mqflags |= SR_MQFLAG_HOLD; | |
136 | if (buf[1] & 0x20) analog[0].meaning->mqflags |= SR_MQFLAG_MAX; | |
137 | if (buf[1] & 0x40) analog[0].meaning->mqflags |= SR_MQFLAG_MIN; | |
138 | if (buf[1] & 0x80) analog[0].meaning->mqflags |= SR_MQFLAG_AVG; | |
139 | if (buf[3] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_RELATIVE; | |
ecaa89af AJ |
140 | |
141 | /* when dBm is displayed, remove the m suffix so that it is | |
142 | not considered as the 10e-3 SI prefix */ | |
143 | if (buf[15] & 0x02) | |
144 | buf[15] &= ~0x04; | |
145 | ||
146 | /* SI prefix */ | |
147 | if (buf[14] & 0x40) floatval[0] *= 1e-9; /* n */ | |
148 | if (buf[15] & 0x08) floatval[0] *= 1e-6; /* µ */ | |
149 | if (buf[15] & 0x04) floatval[0] *= 1e-3; /* m */ | |
150 | if (buf[15] & 0x40) floatval[0] *= 1e3; /* k */ | |
151 | if (buf[15] & 0x20) floatval[0] *= 1e6; /* M */ | |
152 | ||
153 | if (over_limit) floatval[0] = INFINITY; | |
154 | } | |
155 | ||
156 | /* secondary display */ | |
157 | if (ret2 == SR_OK) { | |
158 | /* SI unit */ | |
159 | if (buf[14] & 0x08) { | |
a46f869b UH |
160 | analog[1].meaning->mq = SR_MQ_VOLTAGE; |
161 | analog[1].meaning->unit = SR_UNIT_VOLT; | |
ecaa89af | 162 | } else if (buf[9] & 0x04) { |
a46f869b UH |
163 | analog[1].meaning->mq = SR_MQ_CURRENT; |
164 | analog[1].meaning->unit = SR_UNIT_AMPERE; | |
af1e487e | 165 | } else if (buf[9] & 0x08) { |
a46f869b UH |
166 | analog[1].meaning->mq = SR_MQ_CURRENT; |
167 | analog[1].meaning->unit = SR_UNIT_PERCENTAGE; | |
ecaa89af | 168 | } else if (buf[14] & 0x04) { |
a46f869b UH |
169 | analog[1].meaning->mq = SR_MQ_FREQUENCY; |
170 | analog[1].meaning->unit = SR_UNIT_HERTZ; | |
ecaa89af | 171 | } else if (buf[9] & 0x40) { |
a46f869b | 172 | analog[1].meaning->mq = SR_MQ_TEMPERATURE; |
ecaa89af | 173 | if (temp_unit == 'F') |
a46f869b | 174 | analog[1].meaning->unit = SR_UNIT_FAHRENHEIT; |
ecaa89af | 175 | else |
a46f869b | 176 | analog[1].meaning->unit = SR_UNIT_CELSIUS; |
ecaa89af AJ |
177 | } |
178 | ||
179 | /* AC flag */ | |
a46f869b | 180 | if (buf[9] & 0x20) analog[1].meaning->mqflags |= SR_MQFLAG_AC; |
ecaa89af AJ |
181 | |
182 | /* SI prefix */ | |
183 | if (buf[ 9] & 0x01) floatval[1] *= 1e-6; /* µ */ | |
184 | if (buf[ 9] & 0x02) floatval[1] *= 1e-3; /* m */ | |
185 | if (buf[14] & 0x02) floatval[1] *= 1e3; /* k */ | |
186 | if (buf[14] & 0x01) floatval[1] *= 1e6; /* M */ | |
187 | } | |
188 | ||
189 | if (buf[9] & 0x80) | |
190 | sr_spew("Battery is low."); | |
191 | } | |
192 | ||
193 | static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi, | |
194 | unsigned char *buf) | |
195 | { | |
196 | struct dev_context *devc; | |
197 | struct sr_datafeed_packet packet; | |
a46f869b UH |
198 | struct sr_datafeed_analog analog[2]; |
199 | struct sr_analog_encoding encoding[2]; | |
200 | struct sr_analog_meaning meaning[2]; | |
201 | struct sr_analog_spec spec[2]; | |
ecaa89af AJ |
202 | float floatval[2]; |
203 | ||
204 | devc = sdi->priv; | |
205 | ||
a46f869b UH |
206 | sr_analog_init(&analog[0], &encoding[0], &meaning[0], &spec[0], 0); |
207 | sr_analog_init(&analog[1], &encoding[1], &meaning[1], &spec[1], 0); | |
ecaa89af | 208 | |
a46f869b UH |
209 | analog[0].meaning->mq = 0; |
210 | analog[0].meaning->mqflags = 0; | |
211 | ||
212 | analog[1].meaning->mq = 0; | |
213 | analog[1].meaning->mqflags = 0; | |
ecaa89af AJ |
214 | |
215 | brymen_bm86x_parse(buf, floatval, analog); | |
216 | ||
a46f869b | 217 | if (analog[0].meaning->mq != 0) { |
ecaa89af AJ |
218 | /* Got a measurement. */ |
219 | analog[0].num_samples = 1; | |
220 | analog[0].data = &floatval[0]; | |
a46f869b UH |
221 | analog[0].meaning->channels = g_slist_append(NULL, sdi->channels->data); |
222 | packet.type = SR_DF_ANALOG; | |
ecaa89af | 223 | packet.payload = &analog[0]; |
102f1239 | 224 | sr_session_send(sdi, &packet); |
a46f869b | 225 | g_slist_free(analog[0].meaning->channels); |
ecaa89af AJ |
226 | } |
227 | ||
a46f869b | 228 | if (analog[1].meaning->mq != 0) { |
ecaa89af AJ |
229 | /* Got a measurement. */ |
230 | analog[1].num_samples = 1; | |
231 | analog[1].data = &floatval[1]; | |
a46f869b UH |
232 | analog[1].meaning->channels = g_slist_append(NULL, sdi->channels->next->data); |
233 | packet.type = SR_DF_ANALOG; | |
ecaa89af | 234 | packet.payload = &analog[1]; |
102f1239 | 235 | sr_session_send(sdi, &packet); |
a46f869b | 236 | g_slist_free(analog[1].meaning->channels); |
ecaa89af AJ |
237 | } |
238 | ||
a46f869b | 239 | if (analog[0].meaning->mq != 0 || analog[1].meaning->mq != 0) |
ab939ebb | 240 | sr_sw_limits_update_samples_read(&devc->sw_limits, 1); |
ecaa89af AJ |
241 | } |
242 | ||
243 | static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi) | |
244 | { | |
245 | struct sr_usb_dev_inst *usb; | |
246 | unsigned char buf[] = { 0x00, 0x86, 0x66 }; | |
247 | int ret; | |
248 | ||
249 | usb = sdi->conn; | |
250 | ||
251 | sr_dbg("Sending HID set report."); | |
252 | ret = libusb_control_transfer(usb->devhdl, | |
253 | LIBUSB_REQUEST_TYPE_CLASS | | |
254 | LIBUSB_RECIPIENT_INTERFACE | | |
255 | LIBUSB_ENDPOINT_OUT, | |
256 | 9, /* bRequest: HID set_report */ | |
257 | 0x300, /* wValue: HID feature, report num 0 */ | |
258 | 0, /* wIndex: interface 0 */ | |
259 | buf, sizeof(buf), USB_TIMEOUT); | |
260 | ||
261 | if (ret < 0) { | |
262 | sr_err("HID feature report error: %s.", libusb_error_name(ret)); | |
263 | return SR_ERR; | |
264 | } | |
265 | ||
266 | if (ret != sizeof(buf)) { | |
0c536bcd | 267 | sr_err("Short packet: sent %d/%zu bytes.", ret, sizeof(buf)); |
ecaa89af AJ |
268 | return SR_ERR; |
269 | } | |
270 | ||
271 | return SR_OK; | |
272 | } | |
273 | ||
274 | static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi) | |
275 | { | |
276 | struct dev_context *devc; | |
277 | struct sr_usb_dev_inst *usb; | |
278 | unsigned char buf[24]; | |
279 | int ret, transferred; | |
280 | ||
281 | devc = sdi->priv; | |
282 | usb = sdi->conn; | |
283 | ||
284 | sr_dbg("Reading HID interrupt report."); | |
285 | /* Get data from EP1 using an interrupt transfer. */ | |
286 | ret = libusb_interrupt_transfer(usb->devhdl, | |
287 | LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */ | |
288 | buf, sizeof(buf), | |
289 | &transferred, USB_TIMEOUT); | |
290 | ||
291 | if (ret == LIBUSB_ERROR_TIMEOUT) { | |
292 | if (++devc->interrupt_pending > 3) | |
293 | devc->interrupt_pending = 0; | |
294 | return SR_OK; | |
295 | } | |
296 | ||
297 | if (ret < 0) { | |
298 | sr_err("USB receive error: %s.", libusb_error_name(ret)); | |
299 | return SR_ERR; | |
300 | } | |
301 | ||
302 | if (transferred != sizeof(buf)) { | |
6433156c | 303 | sr_err("Short packet: received %d/%zu bytes.", transferred, sizeof(buf)); |
ecaa89af AJ |
304 | return SR_ERR; |
305 | } | |
306 | ||
307 | devc->interrupt_pending = 0; | |
308 | brymen_bm86x_handle_packet(sdi, buf); | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
8d9c8554 AJ |
313 | SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data) |
314 | { | |
ecaa89af | 315 | struct sr_dev_inst *sdi; |
8d9c8554 AJ |
316 | struct dev_context *devc; |
317 | ||
318 | (void)fd; | |
ecaa89af | 319 | (void)revents; |
8d9c8554 AJ |
320 | |
321 | if (!(sdi = cb_data)) | |
322 | return TRUE; | |
323 | ||
324 | if (!(devc = sdi->priv)) | |
325 | return TRUE; | |
326 | ||
ecaa89af AJ |
327 | if (!devc->interrupt_pending) { |
328 | if (brymen_bm86x_send_command(sdi)) | |
329 | return FALSE; | |
330 | devc->interrupt_pending = 1; | |
331 | } | |
332 | ||
333 | if (brymen_bm86x_read_interrupt(sdi)) | |
334 | return FALSE; | |
335 | ||
ab939ebb | 336 | if (sr_sw_limits_check(&devc->sw_limits)) |
695dc859 | 337 | sdi->driver->dev_acquisition_stop(sdi); |
8d9c8554 AJ |
338 | |
339 | return TRUE; | |
340 | } |