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/>.
25 #define USB_TIMEOUT 500
27 static const char char_map[128] = {
47 static int brymen_bm86x_parse_digits(const unsigned char *buf, int length,
48 char *str, float *floatval,
49 char *temp_unit, int flag)
56 for (i = 0; i < length; i++) {
57 if (i && i < 5 && buf[i+1] & 0x01)
59 c = char_map[buf[i+1] >> 1];
60 if (i == 5 && (c == 'C' || c == 'F'))
67 if ((ret = sr_atof_ascii(str, floatval))) {
68 sr_dbg("invalid float string: '%s'", str);
75 static void brymen_bm86x_parse(unsigned char *buf, float *floatval,
76 struct sr_datafeed_analog_old *analog)
78 char str[16], temp_unit;
79 int ret1, ret2, over_limit;
81 ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0],
83 over_limit = strstr(str, "0L") || strstr(str, "0.L");
84 ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1],
88 if (ret1 == SR_OK || over_limit) {
91 analog[0].mq = SR_MQ_VOLTAGE;
92 analog[0].unit = SR_UNIT_VOLT;
93 if (!strcmp(str, "diod"))
94 analog[0].mqflags |= SR_MQFLAG_DIODE;
95 } else if (buf[14] & 0x80) {
96 analog[0].mq = SR_MQ_CURRENT;
97 analog[0].unit = SR_UNIT_AMPERE;
98 } else if (buf[14] & 0x20) {
99 analog[0].mq = SR_MQ_CAPACITANCE;
100 analog[0].unit = SR_UNIT_FARAD;
101 } else if (buf[14] & 0x10) {
102 analog[0].mq = SR_MQ_CONDUCTANCE;
103 analog[0].unit = SR_UNIT_SIEMENS;
104 } else if (buf[15] & 0x01) {
105 analog[0].mq = SR_MQ_FREQUENCY;
106 analog[0].unit = SR_UNIT_HERTZ;
107 } else if (buf[10] & 0x01) {
108 analog[0].mq = SR_MQ_CONTINUITY;
109 analog[0].unit = SR_UNIT_OHM;
110 } else if (buf[15] & 0x10) {
111 analog[0].mq = SR_MQ_RESISTANCE;
112 analog[0].unit = SR_UNIT_OHM;
113 } else if (buf[15] & 0x02) {
114 analog[0].mq = SR_MQ_POWER;
115 analog[0].unit = SR_UNIT_DECIBEL_MW;
116 } else if (buf[15] & 0x80) {
117 analog[0].mq = SR_MQ_DUTY_CYCLE;
118 analog[0].unit = SR_UNIT_PERCENTAGE;
119 } else if (buf[ 2] & 0x0A) {
120 analog[0].mq = SR_MQ_TEMPERATURE;
121 if (temp_unit == 'F')
122 analog[0].unit = SR_UNIT_FAHRENHEIT;
124 analog[0].unit = SR_UNIT_CELSIUS;
127 /* when MIN MAX and AVG are displayed at the same time, remove them */
128 if ((buf[1] & 0xE0) == 0xE0)
131 /* AC/DC/Auto flags */
132 if (buf[1] & 0x10) analog[0].mqflags |= SR_MQFLAG_DC;
133 if (buf[2] & 0x01) analog[0].mqflags |= SR_MQFLAG_AC;
134 if (buf[1] & 0x01) analog[0].mqflags |= SR_MQFLAG_AUTORANGE;
135 if (buf[1] & 0x08) analog[0].mqflags |= SR_MQFLAG_HOLD;
136 if (buf[1] & 0x20) analog[0].mqflags |= SR_MQFLAG_MAX;
137 if (buf[1] & 0x40) analog[0].mqflags |= SR_MQFLAG_MIN;
138 if (buf[1] & 0x80) analog[0].mqflags |= SR_MQFLAG_AVG;
139 if (buf[3] & 0x01) analog[0].mqflags |= SR_MQFLAG_RELATIVE;
141 /* when dBm is displayed, remove the m suffix so that it is
142 not considered as the 10e-3 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 */
153 if (over_limit) floatval[0] = INFINITY;
156 /* secondary display */
159 if (buf[14] & 0x08) {
160 analog[1].mq = SR_MQ_VOLTAGE;
161 analog[1].unit = SR_UNIT_VOLT;
162 } else if (buf[9] & 0x04) {
163 analog[1].mq = SR_MQ_CURRENT;
164 analog[1].unit = SR_UNIT_AMPERE;
165 } else if (buf[9] & 0x08) {
166 analog[1].mq = SR_MQ_CURRENT;
167 analog[1].unit = SR_UNIT_PERCENTAGE;
168 } else if (buf[14] & 0x04) {
169 analog[1].mq = SR_MQ_FREQUENCY;
170 analog[1].unit = SR_UNIT_HERTZ;
171 } else if (buf[9] & 0x40) {
172 analog[1].mq = SR_MQ_TEMPERATURE;
173 if (temp_unit == 'F')
174 analog[1].unit = SR_UNIT_FAHRENHEIT;
176 analog[1].unit = SR_UNIT_CELSIUS;
180 if (buf[9] & 0x20) analog[1].mqflags |= SR_MQFLAG_AC;
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 */
190 sr_spew("Battery is low.");
193 static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
196 struct dev_context *devc;
197 struct sr_datafeed_packet packet;
198 struct sr_datafeed_analog_old analog[2];
204 analog[0].mqflags = 0;
207 analog[1].mqflags = 0;
209 brymen_bm86x_parse(buf, floatval, analog);
211 if (analog[0].mq != -1) {
212 /* Got a measurement. */
213 analog[0].num_samples = 1;
214 analog[0].data = &floatval[0];
215 analog[0].channels = g_slist_append(NULL, sdi->channels->data);
216 packet.type = SR_DF_ANALOG_OLD;
217 packet.payload = &analog[0];
218 sr_session_send(sdi, &packet);
219 g_slist_free(analog[0].channels);
222 if (analog[1].mq != -1) {
223 /* Got a measurement. */
224 analog[1].num_samples = 1;
225 analog[1].data = &floatval[1];
226 analog[1].channels = g_slist_append(NULL, sdi->channels->next->data);
227 packet.type = SR_DF_ANALOG_OLD;
228 packet.payload = &analog[1];
229 sr_session_send(sdi, &packet);
230 g_slist_free(analog[1].channels);
233 if (analog[0].mq != -1 || analog[1].mq != -1)
237 static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
239 struct sr_usb_dev_inst *usb;
240 unsigned char buf[] = { 0x00, 0x86, 0x66 };
245 sr_dbg("Sending HID set report.");
246 ret = libusb_control_transfer(usb->devhdl,
247 LIBUSB_REQUEST_TYPE_CLASS |
248 LIBUSB_RECIPIENT_INTERFACE |
250 9, /* bRequest: HID set_report */
251 0x300, /* wValue: HID feature, report num 0 */
252 0, /* wIndex: interface 0 */
253 buf, sizeof(buf), USB_TIMEOUT);
256 sr_err("HID feature report error: %s.", libusb_error_name(ret));
260 if (ret != sizeof(buf)) {
261 sr_err("Short packet: sent %d/%zu bytes.", ret, sizeof(buf));
268 static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
270 struct dev_context *devc;
271 struct sr_usb_dev_inst *usb;
272 unsigned char buf[24];
273 int ret, transferred;
278 sr_dbg("Reading HID interrupt report.");
279 /* Get data from EP1 using an interrupt transfer. */
280 ret = libusb_interrupt_transfer(usb->devhdl,
281 LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */
283 &transferred, USB_TIMEOUT);
285 if (ret == LIBUSB_ERROR_TIMEOUT) {
286 if (++devc->interrupt_pending > 3)
287 devc->interrupt_pending = 0;
292 sr_err("USB receive error: %s.", libusb_error_name(ret));
296 if (transferred != sizeof(buf)) {
297 sr_err("Short packet: received %d/%zu bytes.", transferred, sizeof(buf));
301 devc->interrupt_pending = 0;
302 brymen_bm86x_handle_packet(sdi, buf);
307 SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
309 struct sr_dev_inst *sdi;
310 struct dev_context *devc;
316 if (!(sdi = cb_data))
319 if (!(devc = sdi->priv))
322 if (!devc->interrupt_pending) {
323 if (brymen_bm86x_send_command(sdi))
325 devc->interrupt_pending = 1;
328 if (brymen_bm86x_read_interrupt(sdi))
331 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
332 sr_info("Requested number of samples reached, stopping.");
333 sdi->driver->dev_acquisition_stop(sdi, cb_data);
337 if (devc->limit_msec) {
338 time = (g_get_monotonic_time() - devc->start_time) / 1000;
339 if (time > (int64_t)devc->limit_msec) {
340 sr_info("Requested time limit reached, stopping.");
341 sdi->driver->dev_acquisition_stop(sdi, cb_data);