]> sigrok.org Git - libsigrok.git/blob - output/float.c
5c9631f4b2844007b90f094523a769a30b8f8656
[libsigrok.git] / output / float.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <glib.h>
23 #include "config.h"
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26
27 struct context {
28         unsigned int num_enabled_probes;
29         GPtrArray *probelist;
30 };
31
32 static int init(struct sr_output *o)
33 {
34         struct context *ctx;
35         struct sr_probe *probe;
36         GSList *l;
37
38         if (!o)
39                 return SR_ERR_ARG;
40
41         if (!o->sdi)
42                 return SR_ERR_ARG;
43
44         if (!o->sdi->driver)
45                 return SR_ERR_ARG;
46
47         if (!(ctx = g_try_malloc0(sizeof(struct context))))
48                 return SR_ERR_MALLOC;
49
50         o->internal = ctx;
51
52         /* Get the number of probes and their names. */
53         ctx->probelist = g_ptr_array_new();
54         for (l = o->sdi->probes; l; l = l->next) {
55                 probe = l->data;
56                 if (!probe || !probe->enabled)
57                         continue;
58                 g_ptr_array_add(ctx->probelist, probe->name);
59                 ctx->num_enabled_probes++;
60         }
61
62         return SR_OK;
63 }
64
65 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
66                  uint64_t *length_out)
67 {
68         struct context *ctx;
69
70         if (!o)
71                 return SR_ERR_ARG;
72
73         if (!(ctx = o->internal))
74                 return SR_ERR_ARG;
75
76         if (!data_out)
77                 return SR_ERR_ARG;
78
79         switch (event_type) {
80         case SR_DF_FRAME_BEGIN:
81                 *data_out = (uint8_t *)g_strdup("FRAME-BEGIN\n");
82                 *length_out = 12;
83                 break;
84         case SR_DF_FRAME_END:
85                 *data_out = (uint8_t *)g_strdup("FRAME-END\n");
86                 *length_out = 10;
87                 break;
88         case SR_DF_END:
89                 *data_out = NULL;
90                 *length_out = 0;
91                 g_ptr_array_free(ctx->probelist, TRUE);
92                 g_free(o->internal);
93                 o->internal = NULL;
94                 break;
95         default:
96                 /* Ignore everything else. */
97                 *data_out = NULL;
98                 *length_out = 0;
99                 break;
100         }
101
102         return SR_OK;
103 }
104
105 static int data(struct sr_output *o, const uint8_t *data_in,
106                 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
107 {
108         struct context *ctx;
109         GString *outstr;
110         float *fdata;
111         uint64_t max, i;
112         unsigned int j;
113
114         if (!o)
115                 return SR_ERR_ARG;
116
117         if (!(ctx = o->internal))
118                 return SR_ERR_ARG;
119
120         if (!data_in || !data_out)
121                 return SR_ERR_ARG;
122
123         outstr = g_string_sized_new(512);
124
125         fdata = (float *)data_in;
126         max = length_in / sizeof(float);
127         for (i = 0; i < max;) {
128                 for (j = 0; j < ctx->num_enabled_probes; j++) {
129                         g_string_append_printf(outstr, "%s: %.12f\n",
130                                         (char *)g_ptr_array_index(ctx->probelist, j),
131                                         fdata[i++]);
132                 }
133         }
134
135         *data_out = (uint8_t *)outstr->str;
136         *length_out = outstr->len;
137         g_string_free(outstr, FALSE);
138
139         return SR_OK;
140 }
141
142 SR_PRIV struct sr_output_format output_float = {
143         .id = "float",
144         .description = "Floating point",
145         .df_type = SR_DF_ANALOG,
146         .init = init,
147         .data = data,
148         .event = event,
149 };