]> sigrok.org Git - libsigrok.git/commitdiff
sr: new output format 'float', just floating point values
authorBert Vermeulen <redacted>
Tue, 19 Jun 2012 00:05:28 +0000 (02:05 +0200)
committerBert Vermeulen <redacted>
Tue, 19 Jun 2012 00:05:28 +0000 (02:05 +0200)
Also outputs FRAME-BEGIN and FRAME-END, if present in the stream.

output/Makefile.am
output/float.c [new file with mode: 0644]
output/output.c

index 871140056ad4cf5576a107c611c63739eb949407..166fe3ca9edadfe820a09ab782806e0fd60b7472 100644 (file)
@@ -29,10 +29,9 @@ libsigrokoutput_la_SOURCES = \
        gnuplot.c \
        chronovu_la8.c \
        csv.c \
+       float.c \
        output.c
 
-# Temporarily disabled: output_analog.c
-
 libsigrokoutput_la_CFLAGS = \
        -I$(top_srcdir)
 
diff --git a/output/float.c b/output/float.c
new file mode 100644 (file)
index 0000000..850b5d4
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * This file is part of the sigrok project.
+ *
+ * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include "config.h"
+#include "sigrok.h"
+#include "sigrok-internal.h"
+
+struct context {
+       unsigned int num_enabled_probes;
+       GPtrArray *probelist;
+};
+
+static int init(struct sr_output *o)
+{
+       struct context *ctx;
+       struct sr_probe *probe;
+       GSList *l;
+
+       if (!o)
+               return SR_ERR_ARG;
+
+       if (!o->dev)
+               return SR_ERR_ARG;
+
+       if (!o->dev->driver)
+               return SR_ERR_ARG;
+
+       if (!(ctx = g_try_malloc0(sizeof(struct context))))
+               return SR_ERR_MALLOC;
+
+       o->internal = ctx;
+
+       /* Get the number of probes and their names. */
+       ctx->probelist = g_ptr_array_new();
+       for (l = o->dev->probes; l; l = l->next) {
+               probe = l->data;
+               if (!probe || !probe->enabled)
+                       continue;
+               g_ptr_array_add(ctx->probelist, probe->name);
+               ctx->num_enabled_probes++;
+       }
+
+       return SR_OK;
+}
+
+static int event(struct sr_output *o, int event_type, uint8_t **data_out,
+                uint64_t *length_out)
+{
+       struct context *ctx;
+
+       if (!o)
+               return SR_ERR_ARG;
+
+       if (!(ctx = o->internal))
+               return SR_ERR_ARG;
+
+       if (!data_out)
+               return SR_ERR_ARG;
+
+       switch (event_type) {
+       case SR_DF_FRAME_BEGIN:
+               *data_out = (uint8_t *)g_strdup("FRAME-BEGIN\n");
+               *length_out = 12;
+               break;
+       case SR_DF_FRAME_END:
+               *data_out = (uint8_t *)g_strdup("FRAME-END\n");
+               *length_out = 10;
+               break;
+       case SR_DF_END:
+               *data_out = NULL;
+               *length_out = 0;
+               g_ptr_array_free(ctx->probelist, TRUE);
+               g_free(o->internal);
+               o->internal = NULL;
+               break;
+       default:
+               /* Ignore everything else. */
+               *data_out = NULL;
+               *length_out = 0;
+               break;
+       }
+
+       return SR_OK;
+}
+
+static int data(struct sr_output *o, const uint8_t *data_in,
+               uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
+{
+       struct context *ctx;
+       GString *outstr;
+       float *fdata;
+       uint64_t max, i;
+       unsigned int j;
+
+       if (!o)
+               return SR_ERR_ARG;
+
+       if (!(ctx = o->internal))
+               return SR_ERR_ARG;
+
+       if (!data_in || !data_out)
+               return SR_ERR_ARG;
+
+       outstr = g_string_sized_new(512);
+
+       fdata = (float *)data_in;
+       max = length_in / sizeof(float);
+       for (i = 0; i < max;) {
+               for (j = 0; j < ctx->num_enabled_probes; j++) {
+                       g_string_append_printf(outstr, "%s: %f\n",
+                                       (char *)g_ptr_array_index(ctx->probelist, j),
+                                       fdata[i++]);
+               }
+       }
+
+       *data_out = (uint8_t *)outstr->str;
+       *length_out = outstr->len;
+       g_string_free(outstr, FALSE);
+
+       return SR_OK;
+}
+
+SR_PRIV struct sr_output_format output_float = {
+       .id = "float",
+       .description = "Floating point",
+       .df_type = SR_DF_ANALOG,
+       .init = init,
+       .data = data,
+       .event = event,
+};
index 6631855159444330beafa9364fe7cd797f18bebf..a7e26d9c13d34bf83aff9a01ad6a37dfa0f36164 100644 (file)
@@ -29,7 +29,7 @@ extern SR_PRIV struct sr_output_format output_ols;
 extern SR_PRIV struct sr_output_format output_gnuplot;
 extern SR_PRIV struct sr_output_format output_chronovu_la8;
 extern SR_PRIV struct sr_output_format output_csv;
-/* extern SR_PRIV struct sr_output_format output_analog_bits; */
+extern SR_PRIV struct sr_output_format output_float;
 /* extern SR_PRIV struct sr_output_format output_analog_gnuplot; */
 
 static struct sr_output_format *output_module_list[] = {
@@ -42,7 +42,7 @@ static struct sr_output_format *output_module_list[] = {
        &output_gnuplot,
        &output_chronovu_la8,
        &output_csv,
-       /* &output_analog_bits, */
+       &output_float,
        /* &output_analog_gnuplot, */
        NULL,
 };