2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
24 #define USB_TIMEOUT 500
26 static const char char_map[128] = {
46 static int brymen_bm86x_parse_digits(const unsigned char *buf, int length,
47 char *str, float *floatval,
48 char *temp_unit, int flag)
55 for (i = 0; i < length; i++) {
56 if (i && i < 5 && buf[i+1] & 0x01)
58 c = char_map[buf[i+1] >> 1];
59 if (i == 5 && (c == 'C' || c == 'F'))
66 if ((ret = sr_atof_ascii(str, floatval))) {
67 sr_dbg("invalid float string: '%s'", str);
74 static void brymen_bm86x_parse(unsigned char *buf, float *floatval,
75 struct sr_datafeed_analog *analog)
77 char str[16], temp_unit;
78 int ret1, ret2, over_limit;
80 ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0],
82 over_limit = strstr(str, "0L") || strstr(str, "0.L");
83 ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1],
87 if (ret1 == SR_OK || over_limit) {
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;
123 analog[0].unit = SR_UNIT_CELSIUS;
126 /* when MIN MAX and AVG are displayed at the same time, remove them */
127 if ((buf[1] & 0xE0) == 0xE0)
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;
140 /* when dBm is displayed, remove the m suffix so that it is
141 not considered as the 10e-3 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 */
152 if (over_limit) floatval[0] = INFINITY;
155 /* secondary display */
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;
164 } else if (buf[9] & 0x08) {
165 analog[1].mq = SR_MQ_CURRENT;
166 analog[1].unit = SR_UNIT_PERCENTAGE;
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;
175 analog[1].unit = SR_UNIT_CELSIUS;
179 if (buf[9] & 0x20) analog[1].mqflags |= SR_MQFLAG_AC;
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 */
189 sr_spew("Battery is low.");
192 static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
195 struct dev_context *devc;
196 struct sr_datafeed_packet packet;
197 struct sr_datafeed_analog analog[2];
203 analog[0].mqflags = 0;
206 analog[1].mqflags = 0;
208 brymen_bm86x_parse(buf, floatval, analog);
210 if (analog[0].mq != -1) {
211 /* Got a measurement. */
212 analog[0].num_samples = 1;
213 analog[0].data = &floatval[0];
214 analog[0].channels = g_slist_append(NULL, sdi->channels->data);
215 packet.type = SR_DF_ANALOG;
216 packet.payload = &analog[0];
217 sr_session_send(sdi, &packet);
218 g_slist_free(analog[0].channels);
221 if (analog[1].mq != -1) {
222 /* Got a measurement. */
223 analog[1].num_samples = 1;
224 analog[1].data = &floatval[1];
225 analog[1].channels = g_slist_append(NULL, sdi->channels->next->data);
226 packet.type = SR_DF_ANALOG;
227 packet.payload = &analog[1];
228 sr_session_send(sdi, &packet);
229 g_slist_free(analog[1].channels);
232 if (analog[0].mq != -1 || analog[1].mq != -1)
236 static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
238 struct sr_usb_dev_inst *usb;
239 unsigned char buf[] = { 0x00, 0x86, 0x66 };
244 sr_dbg("Sending HID set report.");
245 ret = libusb_control_transfer(usb->devhdl,
246 LIBUSB_REQUEST_TYPE_CLASS |
247 LIBUSB_RECIPIENT_INTERFACE |
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);
255 sr_err("HID feature report error: %s.", libusb_error_name(ret));
259 if (ret != sizeof(buf)) {
260 sr_err("Short packet: sent %d/%ld bytes.", ret, sizeof(buf));
267 static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
269 struct dev_context *devc;
270 struct sr_usb_dev_inst *usb;
271 unsigned char buf[24];
272 int ret, transferred;
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 */
282 &transferred, USB_TIMEOUT);
284 if (ret == LIBUSB_ERROR_TIMEOUT) {
285 if (++devc->interrupt_pending > 3)
286 devc->interrupt_pending = 0;
291 sr_err("USB receive error: %s.", libusb_error_name(ret));
295 if (transferred != sizeof(buf)) {
296 sr_err("Short packet: received %d/%d bytes.", transferred, sizeof(buf));
300 devc->interrupt_pending = 0;
301 brymen_bm86x_handle_packet(sdi, buf);
306 SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
308 struct sr_dev_inst *sdi;
309 struct dev_context *devc;
315 if (!(sdi = cb_data))
318 if (!(devc = sdi->priv))
321 if (!devc->interrupt_pending) {
322 if (brymen_bm86x_send_command(sdi))
324 devc->interrupt_pending = 1;
327 if (brymen_bm86x_read_interrupt(sdi))
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);
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);