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 *digits, int flag)
58 for (i = 0; i < length; i++) {
59 if (i && i < 5 && buf[i+1] & 0x01) {
63 c = char_map[buf[i+1] >> 1];
64 if (i == 5 && (c == 'C' || c == 'F'))
76 if ((ret = sr_atof_ascii(str, floatval))) {
77 sr_dbg("invalid float string: '%s'", str);
84 static void brymen_bm86x_parse(unsigned char *buf, float *floatval,
85 struct sr_datafeed_analog *analog)
87 char str[16], temp_unit;
88 int ret1, ret2, digits[2], over_limit;
90 ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0],
91 &temp_unit, &digits[0], 0x80);
92 over_limit = strstr(str, "0L") || strstr(str, "0.L");
93 ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1],
94 &temp_unit, &digits[1], 0x10);
97 if (ret1 == SR_OK || over_limit) {
100 analog[0].meaning->mq = SR_MQ_VOLTAGE;
101 analog[0].meaning->unit = SR_UNIT_VOLT;
102 if (!strcmp(str, "diod"))
103 analog[0].meaning->mqflags |= SR_MQFLAG_DIODE;
104 } else if (buf[14] & 0x80) {
105 analog[0].meaning->mq = SR_MQ_CURRENT;
106 analog[0].meaning->unit = SR_UNIT_AMPERE;
107 } else if (buf[14] & 0x20) {
108 analog[0].meaning->mq = SR_MQ_CAPACITANCE;
109 analog[0].meaning->unit = SR_UNIT_FARAD;
110 } else if (buf[14] & 0x10) {
111 analog[0].meaning->mq = SR_MQ_CONDUCTANCE;
112 analog[0].meaning->unit = SR_UNIT_SIEMENS;
113 } else if (buf[15] & 0x01) {
114 analog[0].meaning->mq = SR_MQ_FREQUENCY;
115 analog[0].meaning->unit = SR_UNIT_HERTZ;
116 } else if (buf[10] & 0x01) {
117 analog[0].meaning->mq = SR_MQ_CONTINUITY;
118 analog[0].meaning->unit = SR_UNIT_OHM;
119 } else if (buf[15] & 0x10) {
120 analog[0].meaning->mq = SR_MQ_RESISTANCE;
121 analog[0].meaning->unit = SR_UNIT_OHM;
122 } else if (buf[15] & 0x02) {
123 analog[0].meaning->mq = SR_MQ_POWER;
124 analog[0].meaning->unit = SR_UNIT_DECIBEL_MW;
125 } else if (buf[15] & 0x80) {
126 analog[0].meaning->mq = SR_MQ_DUTY_CYCLE;
127 analog[0].meaning->unit = SR_UNIT_PERCENTAGE;
128 } else if (buf[ 2] & 0x0A) {
129 analog[0].meaning->mq = SR_MQ_TEMPERATURE;
130 if (temp_unit == 'F')
131 analog[0].meaning->unit = SR_UNIT_FAHRENHEIT;
133 analog[0].meaning->unit = SR_UNIT_CELSIUS;
136 /* when MIN MAX and AVG are displayed at the same time, remove them */
137 if ((buf[1] & 0xE0) == 0xE0)
140 /* AC/DC/Auto flags */
141 if (buf[1] & 0x10) analog[0].meaning->mqflags |= SR_MQFLAG_DC;
142 if (buf[2] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AC;
143 if (buf[1] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AUTORANGE;
144 if (buf[1] & 0x08) analog[0].meaning->mqflags |= SR_MQFLAG_HOLD;
145 if (buf[1] & 0x20) analog[0].meaning->mqflags |= SR_MQFLAG_MAX;
146 if (buf[1] & 0x40) analog[0].meaning->mqflags |= SR_MQFLAG_MIN;
147 if (buf[1] & 0x80) analog[0].meaning->mqflags |= SR_MQFLAG_AVG;
148 if (buf[3] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_RELATIVE;
150 /* when dBm is displayed, remove the m suffix so that it is
151 not considered as the 10e-3 SI prefix */
156 if (buf[14] & 0x40) { floatval[0] *= 1e-9; digits[0] += 9; } /* n */
157 if (buf[15] & 0x08) { floatval[0] *= 1e-6; digits[0] += 6; } /* µ */
158 if (buf[15] & 0x04) { floatval[0] *= 1e-3; digits[0] += 3; } /* m */
159 if (buf[15] & 0x40) { floatval[0] *= 1e3; digits[0] -= 3; } /* k */
160 if (buf[15] & 0x20) { floatval[0] *= 1e6; digits[0] -= 6; } /* M */
162 if (over_limit) floatval[0] = INFINITY;
164 analog[0].encoding->digits = digits[0];
165 analog[0].spec->spec_digits = digits[0];
168 /* secondary display */
171 if (buf[14] & 0x08) {
172 analog[1].meaning->mq = SR_MQ_VOLTAGE;
173 analog[1].meaning->unit = SR_UNIT_VOLT;
174 } else if (buf[9] & 0x04) {
175 analog[1].meaning->mq = SR_MQ_CURRENT;
176 analog[1].meaning->unit = SR_UNIT_AMPERE;
177 } else if (buf[9] & 0x08) {
178 analog[1].meaning->mq = SR_MQ_CURRENT;
179 analog[1].meaning->unit = SR_UNIT_PERCENTAGE;
180 } else if (buf[14] & 0x04) {
181 analog[1].meaning->mq = SR_MQ_FREQUENCY;
182 analog[1].meaning->unit = SR_UNIT_HERTZ;
183 } else if (buf[9] & 0x40) {
184 analog[1].meaning->mq = SR_MQ_TEMPERATURE;
185 if (temp_unit == 'F')
186 analog[1].meaning->unit = SR_UNIT_FAHRENHEIT;
188 analog[1].meaning->unit = SR_UNIT_CELSIUS;
192 if (buf[9] & 0x20) analog[1].meaning->mqflags |= SR_MQFLAG_AC;
195 if (buf[ 9] & 0x01) { floatval[1] *= 1e-6; digits[1] += 6; } /* µ */
196 if (buf[ 9] & 0x02) { floatval[1] *= 1e-3; digits[1] += 3; } /* m */
197 if (buf[14] & 0x02) { floatval[1] *= 1e3; digits[1] -= 3; } /* k */
198 if (buf[14] & 0x01) { floatval[1] *= 1e6; digits[1] -= 6; } /* M */
200 analog[1].encoding->digits = digits[1];
201 analog[1].spec->spec_digits = digits[1];
205 sr_spew("Battery is low.");
208 static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
211 struct dev_context *devc;
212 struct sr_datafeed_packet packet;
213 struct sr_datafeed_analog analog[2];
214 struct sr_analog_encoding encoding[2];
215 struct sr_analog_meaning meaning[2];
216 struct sr_analog_spec spec[2];
217 struct sr_channel *channel;
218 int sent_ch1, sent_ch2;
223 /* Note: digits/spec_digits will be overridden later. */
224 sr_analog_init(&analog[0], &encoding[0], &meaning[0], &spec[0], 0);
225 sr_analog_init(&analog[1], &encoding[1], &meaning[1], &spec[1], 0);
227 brymen_bm86x_parse(buf, floatval, analog);
228 sent_ch1 = sent_ch2 = 0;
230 channel = sdi->channels->data;
231 if (analog[0].meaning->mq != 0 && channel->enabled) {
232 /* Got a measurement. */
234 analog[0].num_samples = 1;
235 analog[0].data = &floatval[0];
236 analog[0].meaning->channels = g_slist_append(NULL, channel);
237 packet.type = SR_DF_ANALOG;
238 packet.payload = &analog[0];
239 sr_session_send(sdi, &packet);
240 g_slist_free(analog[0].meaning->channels);
243 channel = sdi->channels->next->data;
244 if (analog[1].meaning->mq != 0 && channel->enabled) {
245 /* Got a measurement. */
247 analog[1].num_samples = 1;
248 analog[1].data = &floatval[1];
249 analog[1].meaning->channels = g_slist_append(NULL, channel);
250 packet.type = SR_DF_ANALOG;
251 packet.payload = &analog[1];
252 sr_session_send(sdi, &packet);
253 g_slist_free(analog[1].meaning->channels);
256 if (sent_ch1 || sent_ch2)
257 sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
260 static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
262 struct sr_usb_dev_inst *usb;
263 unsigned char buf[] = { 0x00, 0x86, 0x66 };
268 sr_dbg("Sending HID set report.");
269 ret = libusb_control_transfer(usb->devhdl,
270 LIBUSB_REQUEST_TYPE_CLASS |
271 LIBUSB_RECIPIENT_INTERFACE |
273 9, /* bRequest: HID set_report */
274 0x300, /* wValue: HID feature, report num 0 */
275 0, /* wIndex: interface 0 */
276 buf, sizeof(buf), USB_TIMEOUT);
279 sr_err("HID feature report error: %s.", libusb_error_name(ret));
283 if (ret != sizeof(buf)) {
284 sr_err("Short packet: sent %d/%zu bytes.", ret, sizeof(buf));
291 static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
293 struct dev_context *devc;
294 struct sr_usb_dev_inst *usb;
295 unsigned char buf[24];
296 int ret, transferred;
301 sr_dbg("Reading HID interrupt report.");
302 /* Get data from EP1 using an interrupt transfer. */
303 ret = libusb_interrupt_transfer(usb->devhdl,
304 LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */
306 &transferred, USB_TIMEOUT);
308 if (ret == LIBUSB_ERROR_TIMEOUT) {
309 if (++devc->interrupt_pending > 3)
310 devc->interrupt_pending = 0;
315 sr_err("USB receive error: %s.", libusb_error_name(ret));
319 if (transferred != sizeof(buf)) {
320 sr_err("Short packet: received %d/%zu bytes.", transferred, sizeof(buf));
324 devc->interrupt_pending = 0;
325 brymen_bm86x_handle_packet(sdi, buf);
330 SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
332 struct sr_dev_inst *sdi;
333 struct dev_context *devc;
338 if (!(sdi = cb_data))
341 if (!(devc = sdi->priv))
344 if (!devc->interrupt_pending) {
345 if (brymen_bm86x_send_command(sdi))
347 devc->interrupt_pending = 1;
350 if (brymen_bm86x_read_interrupt(sdi))
353 if (sr_sw_limits_check(&devc->sw_limits))
354 sr_dev_acquisition_stop(sdi);