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