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