X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fhardware%2Fkern-scale%2Fprotocol.c;h=722e06256ee3a177464e404409205f2d3be359f8;hb=7dd1dd9f7d4d2cb17d3099453827db28c7e4262c;hp=51ad9be19b938c7d603eff2e19a7b1cc46182cda;hpb=9380ec2f05e67518b1b23057749df3684a0cf05e;p=libsigrok.git diff --git a/src/hardware/kern-scale/protocol.c b/src/hardware/kern-scale/protocol.c index 51ad9be1..722e0625 100644 --- a/src/hardware/kern-scale/protocol.c +++ b/src/hardware/kern-scale/protocol.c @@ -14,16 +14,99 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ +#include +#include +#include +#include +#include +#include +#include "libsigrok-internal.h" #include "protocol.h" +static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, + void *info) +{ + struct scale_info *scale; + float floatval; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + struct sr_analog_encoding encoding; + struct sr_analog_meaning meaning; + struct sr_analog_spec spec; + struct dev_context *devc; + + scale = (struct scale_info *)sdi->driver; + + devc = sdi->priv; + + /* Note: digits/spec_digits will be overridden later. */ + sr_analog_init(&analog, &encoding, &meaning, &spec, 0); + + analog.meaning->channels = sdi->channels; + analog.num_samples = 1; + analog.meaning->mq = 0; + + scale->packet_parse(buf, &floatval, &analog, info); + analog.data = &floatval; + + if (analog.meaning->mq != 0) { + /* Got a measurement. */ + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + sr_session_send(sdi, &packet); + sr_sw_limits_update_samples_read(&devc->limits, 1); + } +} + +static void handle_new_data(struct sr_dev_inst *sdi, void *info) +{ + struct scale_info *scale; + struct dev_context *devc; + int len, offset; + struct sr_serial_dev_inst *serial; + + scale = (struct scale_info *)sdi->driver; + + devc = sdi->priv; + serial = sdi->conn; + + /* Try to get as much data as the buffer can hold. */ + len = SCALE_BUFSIZE - devc->buflen; + len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); + if (len == 0) + return; /* No new bytes, nothing to do. */ + if (len < 0) { + sr_err("Serial port read error: %d.", len); + return; + } + devc->buflen += len; + + /* Now look for packets in that data. */ + offset = 0; + while ((devc->buflen - offset) >= scale->packet_size) { + if (scale->packet_valid(devc->buf + offset)) { + handle_packet(devc->buf + offset, sdi, info); + offset += scale->packet_size; + } else { + offset++; + } + } + + /* If we have any data left, move it to the beginning of our buffer. */ + if (offset < devc->buflen) + memmove(devc->buf, devc->buf + offset, devc->buflen - offset); + devc->buflen -= offset; +} + SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; + struct sr_dev_inst *sdi; struct dev_context *devc; + struct scale_info *scale; + void *info; (void)fd; @@ -33,8 +116,17 @@ SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data) if (!(devc = sdi->priv)) return TRUE; + scale = (struct scale_info *)sdi->driver; + if (revents == G_IO_IN) { + /* Serial data arrived. */ + info = g_malloc(scale->info_size); + handle_new_data(sdi, info); + g_free(info); } + if (sr_sw_limits_check(&devc->limits)) + sr_dev_acquisition_stop(sdi); + return TRUE; }