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