X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fbrymen-bm86x%2Fprotocol.c;h=097b8874ef61c20b9a0c22b781eb3d515bd3c682;hb=8cd15dd4ce2fdbefbcc6e64632c8006e5404f253;hp=2840696f8127f8a84e9f1f664f468ba440d6eeba;hpb=6ec6c43b4738dbc7091f4a49a4ec80ea6102cb52;p=libsigrok.git diff --git a/src/hardware/brymen-bm86x/protocol.c b/src/hardware/brymen-bm86x/protocol.c index 2840696f..097b8874 100644 --- a/src/hardware/brymen-bm86x/protocol.c +++ b/src/hardware/brymen-bm86x/protocol.c @@ -46,24 +46,33 @@ static const char char_map[128] = { static int brymen_bm86x_parse_digits(const unsigned char *buf, int length, char *str, float *floatval, - char *temp_unit, int flag) + char *temp_unit, int *digits, int flag) { char c, *p = str; int i, ret; + *digits = INT_MIN; + if (buf[0] & flag) *p++ = '-'; for (i = 0; i < length; i++) { - if (i && i < 5 && buf[i+1] & 0x01) + if (i && i < 5 && buf[i+1] & 0x01) { *p++ = '.'; + *digits = 0; + } c = char_map[buf[i+1] >> 1]; if (i == 5 && (c == 'C' || c == 'F')) *temp_unit = c; - else if (c) + else if (c) { *p++ = c; + (*digits)++; + } } *p = 0; + if (*digits < 0) + *digits = 0; + if ((ret = sr_atof_ascii(str, floatval))) { sr_dbg("invalid float string: '%s'", str); return ret; @@ -76,52 +85,54 @@ static void brymen_bm86x_parse(unsigned char *buf, float *floatval, struct sr_datafeed_analog *analog) { char str[16], temp_unit; - int ret1, ret2, over_limit; + int ret1, ret2, digits[2], over_limit; ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0], - &temp_unit, 0x80); + &temp_unit, &digits[0], 0x80); over_limit = strstr(str, "0L") || strstr(str, "0.L"); ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1], - &temp_unit, 0x10); + &temp_unit, &digits[1], 0x10); /* main display */ if (ret1 == SR_OK || over_limit) { /* SI unit */ if (buf[8] & 0x01) { - analog[0].mq = SR_MQ_VOLTAGE; - analog[0].unit = SR_UNIT_VOLT; - if (!strcmp(str, "diod")) - analog[0].mqflags |= SR_MQFLAG_DIODE; + analog[0].meaning->mq = SR_MQ_VOLTAGE; + analog[0].meaning->unit = SR_UNIT_VOLT; + if (!strcmp(str, "diod")) { + analog[0].meaning->mqflags |= SR_MQFLAG_DIODE; + analog[0].meaning->mqflags |= SR_MQFLAG_DC; + } } else if (buf[14] & 0x80) { - analog[0].mq = SR_MQ_CURRENT; - analog[0].unit = SR_UNIT_AMPERE; + analog[0].meaning->mq = SR_MQ_CURRENT; + analog[0].meaning->unit = SR_UNIT_AMPERE; } else if (buf[14] & 0x20) { - analog[0].mq = SR_MQ_CAPACITANCE; - analog[0].unit = SR_UNIT_FARAD; + analog[0].meaning->mq = SR_MQ_CAPACITANCE; + analog[0].meaning->unit = SR_UNIT_FARAD; } else if (buf[14] & 0x10) { - analog[0].mq = SR_MQ_CONDUCTANCE; - analog[0].unit = SR_UNIT_SIEMENS; + analog[0].meaning->mq = SR_MQ_CONDUCTANCE; + analog[0].meaning->unit = SR_UNIT_SIEMENS; } else if (buf[15] & 0x01) { - analog[0].mq = SR_MQ_FREQUENCY; - analog[0].unit = SR_UNIT_HERTZ; + analog[0].meaning->mq = SR_MQ_FREQUENCY; + analog[0].meaning->unit = SR_UNIT_HERTZ; } else if (buf[10] & 0x01) { - analog[0].mq = SR_MQ_CONTINUITY; - analog[0].unit = SR_UNIT_OHM; + analog[0].meaning->mq = SR_MQ_CONTINUITY; + analog[0].meaning->unit = SR_UNIT_OHM; } else if (buf[15] & 0x10) { - analog[0].mq = SR_MQ_RESISTANCE; - analog[0].unit = SR_UNIT_OHM; + analog[0].meaning->mq = SR_MQ_RESISTANCE; + analog[0].meaning->unit = SR_UNIT_OHM; } else if (buf[15] & 0x02) { - analog[0].mq = SR_MQ_POWER; - analog[0].unit = SR_UNIT_DECIBEL_MW; + analog[0].meaning->mq = SR_MQ_POWER; + analog[0].meaning->unit = SR_UNIT_DECIBEL_MW; } else if (buf[15] & 0x80) { - analog[0].mq = SR_MQ_DUTY_CYCLE; - analog[0].unit = SR_UNIT_PERCENTAGE; + analog[0].meaning->mq = SR_MQ_DUTY_CYCLE; + analog[0].meaning->unit = SR_UNIT_PERCENTAGE; } else if (buf[ 2] & 0x0A) { - analog[0].mq = SR_MQ_TEMPERATURE; + analog[0].meaning->mq = SR_MQ_TEMPERATURE; if (temp_unit == 'F') - analog[0].unit = SR_UNIT_FAHRENHEIT; + analog[0].meaning->unit = SR_UNIT_FAHRENHEIT; else - analog[0].unit = SR_UNIT_CELSIUS; + analog[0].meaning->unit = SR_UNIT_CELSIUS; } /* when MIN MAX and AVG are displayed at the same time, remove them */ @@ -129,14 +140,14 @@ static void brymen_bm86x_parse(unsigned char *buf, float *floatval, buf[1] &= ~0xE0; /* AC/DC/Auto flags */ - if (buf[1] & 0x10) analog[0].mqflags |= SR_MQFLAG_DC; - if (buf[2] & 0x01) analog[0].mqflags |= SR_MQFLAG_AC; - if (buf[1] & 0x01) analog[0].mqflags |= SR_MQFLAG_AUTORANGE; - if (buf[1] & 0x08) analog[0].mqflags |= SR_MQFLAG_HOLD; - if (buf[1] & 0x20) analog[0].mqflags |= SR_MQFLAG_MAX; - if (buf[1] & 0x40) analog[0].mqflags |= SR_MQFLAG_MIN; - if (buf[1] & 0x80) analog[0].mqflags |= SR_MQFLAG_AVG; - if (buf[3] & 0x01) analog[0].mqflags |= SR_MQFLAG_RELATIVE; + if (buf[1] & 0x10) analog[0].meaning->mqflags |= SR_MQFLAG_DC; + if (buf[2] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AC; + if (buf[1] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_AUTORANGE; + if (buf[1] & 0x08) analog[0].meaning->mqflags |= SR_MQFLAG_HOLD; + if (buf[1] & 0x20) analog[0].meaning->mqflags |= SR_MQFLAG_MAX; + if (buf[1] & 0x40) analog[0].meaning->mqflags |= SR_MQFLAG_MIN; + if (buf[1] & 0x80) analog[0].meaning->mqflags |= SR_MQFLAG_AVG; + if (buf[3] & 0x01) analog[0].meaning->mqflags |= SR_MQFLAG_RELATIVE; /* when dBm is displayed, remove the m suffix so that it is not considered as the 10e-3 SI prefix */ @@ -144,46 +155,52 @@ static void brymen_bm86x_parse(unsigned char *buf, float *floatval, buf[15] &= ~0x04; /* SI prefix */ - if (buf[14] & 0x40) floatval[0] *= 1e-9; /* n */ - if (buf[15] & 0x08) floatval[0] *= 1e-6; /* µ */ - if (buf[15] & 0x04) floatval[0] *= 1e-3; /* m */ - if (buf[15] & 0x40) floatval[0] *= 1e3; /* k */ - if (buf[15] & 0x20) floatval[0] *= 1e6; /* M */ + if (buf[14] & 0x40) { floatval[0] *= 1e-9; digits[0] += 9; } /* n */ + if (buf[15] & 0x08) { floatval[0] *= 1e-6; digits[0] += 6; } /* µ */ + if (buf[15] & 0x04) { floatval[0] *= 1e-3; digits[0] += 3; } /* m */ + if (buf[15] & 0x40) { floatval[0] *= 1e3; digits[0] -= 3; } /* k */ + if (buf[15] & 0x20) { floatval[0] *= 1e6; digits[0] -= 6; } /* M */ if (over_limit) floatval[0] = INFINITY; + + analog[0].encoding->digits = digits[0]; + analog[0].spec->spec_digits = digits[0]; } /* secondary display */ if (ret2 == SR_OK) { /* SI unit */ if (buf[14] & 0x08) { - analog[1].mq = SR_MQ_VOLTAGE; - analog[1].unit = SR_UNIT_VOLT; + analog[1].meaning->mq = SR_MQ_VOLTAGE; + analog[1].meaning->unit = SR_UNIT_VOLT; } else if (buf[9] & 0x04) { - analog[1].mq = SR_MQ_CURRENT; - analog[1].unit = SR_UNIT_AMPERE; + analog[1].meaning->mq = SR_MQ_CURRENT; + analog[1].meaning->unit = SR_UNIT_AMPERE; } else if (buf[9] & 0x08) { - analog[1].mq = SR_MQ_CURRENT; - analog[1].unit = SR_UNIT_PERCENTAGE; + analog[1].meaning->mq = SR_MQ_CURRENT; + analog[1].meaning->unit = SR_UNIT_PERCENTAGE; } else if (buf[14] & 0x04) { - analog[1].mq = SR_MQ_FREQUENCY; - analog[1].unit = SR_UNIT_HERTZ; + analog[1].meaning->mq = SR_MQ_FREQUENCY; + analog[1].meaning->unit = SR_UNIT_HERTZ; } else if (buf[9] & 0x40) { - analog[1].mq = SR_MQ_TEMPERATURE; + analog[1].meaning->mq = SR_MQ_TEMPERATURE; if (temp_unit == 'F') - analog[1].unit = SR_UNIT_FAHRENHEIT; + analog[1].meaning->unit = SR_UNIT_FAHRENHEIT; else - analog[1].unit = SR_UNIT_CELSIUS; + analog[1].meaning->unit = SR_UNIT_CELSIUS; } /* AC flag */ - if (buf[9] & 0x20) analog[1].mqflags |= SR_MQFLAG_AC; + if (buf[9] & 0x20) analog[1].meaning->mqflags |= SR_MQFLAG_AC; /* SI prefix */ - if (buf[ 9] & 0x01) floatval[1] *= 1e-6; /* µ */ - if (buf[ 9] & 0x02) floatval[1] *= 1e-3; /* m */ - if (buf[14] & 0x02) floatval[1] *= 1e3; /* k */ - if (buf[14] & 0x01) floatval[1] *= 1e6; /* M */ + if (buf[ 9] & 0x01) { floatval[1] *= 1e-6; digits[1] += 6; } /* µ */ + if (buf[ 9] & 0x02) { floatval[1] *= 1e-3; digits[1] += 3; } /* m */ + if (buf[14] & 0x02) { floatval[1] *= 1e3; digits[1] -= 3; } /* k */ + if (buf[14] & 0x01) { floatval[1] *= 1e6; digits[1] -= 6; } /* M */ + + analog[1].encoding->digits = digits[1]; + analog[1].spec->spec_digits = digits[1]; } if (buf[9] & 0x80) @@ -196,42 +213,50 @@ static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi, struct dev_context *devc; struct sr_datafeed_packet packet; struct sr_datafeed_analog analog[2]; + struct sr_analog_encoding encoding[2]; + struct sr_analog_meaning meaning[2]; + struct sr_analog_spec spec[2]; + struct sr_channel *channel; + int sent_ch1, sent_ch2; float floatval[2]; devc = sdi->priv; - analog[0].mq = -1; - analog[0].mqflags = 0; - - analog[1].mq = -1; - analog[1].mqflags = 0; + /* Note: digits/spec_digits will be overridden later. */ + sr_analog_init(&analog[0], &encoding[0], &meaning[0], &spec[0], 0); + sr_analog_init(&analog[1], &encoding[1], &meaning[1], &spec[1], 0); brymen_bm86x_parse(buf, floatval, analog); + sent_ch1 = sent_ch2 = 0; - if (analog[0].mq != -1) { + channel = sdi->channels->data; + if (analog[0].meaning->mq != 0 && channel->enabled) { /* Got a measurement. */ + sent_ch1 = 1; analog[0].num_samples = 1; analog[0].data = &floatval[0]; - analog[0].channels = g_slist_append(NULL, sdi->channels->data); + analog[0].meaning->channels = g_slist_append(NULL, channel); packet.type = SR_DF_ANALOG; packet.payload = &analog[0]; sr_session_send(sdi, &packet); - g_slist_free(analog[0].channels); + g_slist_free(analog[0].meaning->channels); } - if (analog[1].mq != -1) { + channel = sdi->channels->next->data; + if (analog[1].meaning->mq != 0 && channel->enabled) { /* Got a measurement. */ + sent_ch2 = 1; analog[1].num_samples = 1; analog[1].data = &floatval[1]; - analog[1].channels = g_slist_append(NULL, sdi->channels->next->data); + analog[1].meaning->channels = g_slist_append(NULL, channel); packet.type = SR_DF_ANALOG; packet.payload = &analog[1]; sr_session_send(sdi, &packet); - g_slist_free(analog[1].channels); + g_slist_free(analog[1].meaning->channels); } - if (analog[0].mq != -1 || analog[1].mq != -1) - devc->num_samples++; + if (sent_ch1 || sent_ch2) + sr_sw_limits_update_samples_read(&devc->sw_limits, 1); } static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi) @@ -244,7 +269,7 @@ static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi) sr_dbg("Sending HID set report."); ret = libusb_control_transfer(usb->devhdl, - LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT, 9, /* bRequest: HID set_report */ @@ -308,7 +333,6 @@ SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data) { struct sr_dev_inst *sdi; struct dev_context *devc; - int64_t time; (void)fd; (void)revents; @@ -328,20 +352,8 @@ SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data) if (brymen_bm86x_read_interrupt(sdi)) return FALSE; - if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { - sr_info("Requested number of samples reached, stopping."); - sdi->driver->dev_acquisition_stop(sdi, cb_data); - return TRUE; - } - - if (devc->limit_msec) { - time = (g_get_monotonic_time() - devc->start_time) / 1000; - if (time > (int64_t)devc->limit_msec) { - sr_info("Requested time limit reached, stopping."); - sdi->driver->dev_acquisition_stop(sdi, cb_data); - return TRUE; - } - } + if (sr_sw_limits_check(&devc->sw_limits)) + sr_dev_acquisition_stop(sdi); return TRUE; }