]> sigrok.org Git - libsigrok.git/blobdiff - src/lcr/es51919.c
Use frames to group a single measurement result together.
[libsigrok.git] / src / lcr / es51919.c
index ecfcd0ed7a5a0f703f83a139e3f5420fcf9c2537..b1b788531a4077c2d7aedccad5c54e16b55e24f0 100644 (file)
@@ -41,11 +41,7 @@ static struct dev_buffer *dev_buffer_new(size_t size)
 {
        struct dev_buffer *dbuf;
 
-       if (!(dbuf = g_try_malloc(sizeof(struct dev_buffer) + size))) {
-               sr_err("Dev buffer malloc failed (size=%zu).", size);
-               return NULL;
-       }
-
+       dbuf = g_malloc0(sizeof(struct dev_buffer) + size);
        dbuf->size = size;
        dbuf->len = 0;
        dbuf->offset = 0;
@@ -669,7 +665,7 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
        struct dev_context *devc;
        unsigned int val;
        float floatval;
-       int count;
+       gboolean frame;
 
        devc = sdi->priv;
 
@@ -705,34 +701,50 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt)
                        return;
        }
 
-       count = 0;
+       frame = FALSE;
 
        memset(&analog, 0, sizeof(analog));
 
        analog.num_samples = 1;
        analog.data = &floatval;
 
-       packet.type = SR_DF_ANALOG;
-       packet.payload = &analog;
-
        analog.channels = g_slist_append(NULL, sdi->channels->data);
 
        parse_measurement(pkt, &floatval, &analog, 0);
        if (analog.mq >= 0) {
-               if (sr_session_send(devc->cb_data, &packet) == SR_OK)
-                       count++;
+               if (!frame) {
+                       packet.type = SR_DF_FRAME_BEGIN;
+                       sr_session_send(devc->cb_data, &packet);
+                       frame = TRUE;
+               }
+
+               packet.type = SR_DF_ANALOG;
+               packet.payload = &analog;
+
+               sr_session_send(devc->cb_data, &packet);
        }
 
        analog.channels = g_slist_append(NULL, sdi->channels->next->data);
 
        parse_measurement(pkt, &floatval, &analog, 1);
        if (analog.mq >= 0) {
-               if (sr_session_send(devc->cb_data, &packet) == SR_OK)
-                       count++;
+               if (!frame) {
+                       packet.type = SR_DF_FRAME_BEGIN;
+                       sr_session_send(devc->cb_data, &packet);
+                       frame = TRUE;
+               }
+
+               packet.type = SR_DF_ANALOG;
+               packet.payload = &analog;
+
+               sr_session_send(devc->cb_data, &packet);
        }
 
-       if (count > 0)
+       if (frame) {
+               packet.type = SR_DF_FRAME_END;
+               sr_session_send(devc->cb_data, &packet);
                dev_sample_counter_inc(&devc->sample_count);
+       }
 }
 
 static int handle_new_data(struct sr_dev_inst *sdi)
@@ -779,13 +791,11 @@ static int receive_data(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-static int add_channel(struct sr_dev_inst *sdi, const char *name)
+static int add_channel(struct sr_dev_inst *sdi, int idx, const char *name)
 {
        struct sr_channel *ch;
 
-       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, name)))
-               return SR_ERR;
-
+       ch = sr_channel_new(idx, SR_CHANNEL_ANALOG, TRUE, name);
        sdi->channels = g_slist_append(sdi->channels, ch);
 
        return SR_OK;
@@ -801,7 +811,7 @@ static int setup_channels(struct sr_dev_inst *sdi)
        ret = SR_ERR_BUG;
 
        for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
-               ret = add_channel(sdi, channel_names[i]);
+               ret = add_channel(sdi, i, channel_names[i]);
                if (ret != SR_OK)
                        break;
        }
@@ -847,18 +857,10 @@ SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
        sdi->status = SR_ST_INACTIVE;
        sdi->vendor = g_strdup(vendor);
        sdi->model = g_strdup(model);
-
-       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
-               sr_err("Device context malloc failed.");
-               goto scan_cleanup;
-       }
-
-       if (!(devc->buf = dev_buffer_new(PACKET_SIZE * 8)))
-               goto scan_cleanup;
-
+       devc = g_malloc0(sizeof(struct dev_context));
+       devc->buf = dev_buffer_new(PACKET_SIZE * 8);
        sdi->inst_type = SR_INST_SERIAL;
        sdi->conn = serial;
-
        sdi->priv = devc;
 
        if (setup_channels(sdi) != SR_OK)