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 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[14] & 0x04) {
165 analog[1].mq = SR_MQ_FREQUENCY;
166 analog[1].unit = SR_UNIT_HERTZ;
167 } else if (buf[9] & 0x40) {
168 analog[1].mq = SR_MQ_TEMPERATURE;
169 if (temp_unit == 'F')
170 analog[1].unit = SR_UNIT_FAHRENHEIT;
172 analog[1].unit = SR_UNIT_CELSIUS;
176 if (buf[9] & 0x20) analog[1].mqflags |= SR_MQFLAG_AC;
179 if (buf[ 9] & 0x01) floatval[1] *= 1e-6; /* µ */
180 if (buf[ 9] & 0x02) floatval[1] *= 1e-3; /* m */
181 if (buf[14] & 0x02) floatval[1] *= 1e3; /* k */
182 if (buf[14] & 0x01) floatval[1] *= 1e6; /* M */
186 sr_spew("Battery is low.");
189 static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
192 struct dev_context *devc;
193 struct sr_datafeed_packet packet;
194 struct sr_datafeed_analog analog[2];
200 analog[0].mqflags = 0;
203 analog[1].mqflags = 0;
205 brymen_bm86x_parse(buf, floatval, analog);
207 if (analog[0].mq != -1) {
208 /* Got a measurement. */
209 analog[0].num_samples = 1;
210 analog[0].data = &floatval[0];
211 analog[0].channels = g_slist_append(NULL, sdi->channels->data);
212 packet.type = SR_DF_ANALOG;
213 packet.payload = &analog[0];
214 sr_session_send(sdi, &packet);
215 g_slist_free(analog[0].channels);
218 if (analog[1].mq != -1) {
219 /* Got a measurement. */
220 analog[1].num_samples = 1;
221 analog[1].data = &floatval[1];
222 analog[1].channels = g_slist_append(NULL, sdi->channels->next->data);
223 packet.type = SR_DF_ANALOG;
224 packet.payload = &analog[1];
225 sr_session_send(sdi, &packet);
226 g_slist_free(analog[1].channels);
229 if (analog[0].mq != -1 || analog[1].mq != -1)
233 static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
235 struct sr_usb_dev_inst *usb;
236 unsigned char buf[] = { 0x00, 0x86, 0x66 };
241 sr_dbg("Sending HID set report.");
242 ret = libusb_control_transfer(usb->devhdl,
243 LIBUSB_REQUEST_TYPE_CLASS |
244 LIBUSB_RECIPIENT_INTERFACE |
246 9, /* bRequest: HID set_report */
247 0x300, /* wValue: HID feature, report num 0 */
248 0, /* wIndex: interface 0 */
249 buf, sizeof(buf), USB_TIMEOUT);
252 sr_err("HID feature report error: %s.", libusb_error_name(ret));
256 if (ret != sizeof(buf)) {
257 sr_err("Short packet: sent %d/%ld bytes.", ret, sizeof(buf));
264 static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
266 struct dev_context *devc;
267 struct sr_usb_dev_inst *usb;
268 unsigned char buf[24];
269 int ret, transferred;
274 sr_dbg("Reading HID interrupt report.");
275 /* Get data from EP1 using an interrupt transfer. */
276 ret = libusb_interrupt_transfer(usb->devhdl,
277 LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */
279 &transferred, USB_TIMEOUT);
281 if (ret == LIBUSB_ERROR_TIMEOUT) {
282 if (++devc->interrupt_pending > 3)
283 devc->interrupt_pending = 0;
288 sr_err("USB receive error: %s.", libusb_error_name(ret));
292 if (transferred != sizeof(buf)) {
293 sr_err("Short packet: received %d/%d bytes.", transferred, sizeof(buf));
297 devc->interrupt_pending = 0;
298 brymen_bm86x_handle_packet(sdi, buf);
303 SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
305 struct sr_dev_inst *sdi;
306 struct dev_context *devc;
312 if (!(sdi = cb_data))
315 if (!(devc = sdi->priv))
318 if (!devc->interrupt_pending) {
319 if (brymen_bm86x_send_command(sdi))
321 devc->interrupt_pending = 1;
324 if (brymen_bm86x_read_interrupt(sdi))
327 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
328 sr_info("Requested number of samples reached, stopping.");
329 sdi->driver->dev_acquisition_stop(sdi, cb_data);
333 if (devc->limit_msec) {
334 time = (g_get_monotonic_time() - devc->start_time) / 1000;
335 if (time > (int64_t)devc->limit_msec) {
336 sr_info("Requested time limit reached, stopping.");
337 sdi->driver->dev_acquisition_stop(sdi, cb_data);