]>
Commit | Line | Data |
---|---|---|
25e7d9b1 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <stdint.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sigrok.h> | |
25 | ||
26 | /** | |
27 | * Convert a numeric samplerate value to its "natural" string representation. | |
28 | * | |
38ba2522 | 29 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz". |
25e7d9b1 UH |
30 | * |
31 | * @param samplerate The samplerate in Hz. | |
32 | * @return A malloc()ed string representation of the samplerate value, | |
33 | * or NULL upon errors. The caller is responsible to free() the memory. | |
34 | */ | |
a00ba012 | 35 | char *sr_samplerate_string(uint64_t samplerate) |
25e7d9b1 UH |
36 | { |
37 | char *o; | |
38 | int r; | |
39 | ||
40 | o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */ | |
484760d1 | 41 | if (!o) |
25e7d9b1 UH |
42 | return NULL; |
43 | ||
44 | if (samplerate >= GHZ(1)) | |
99c1fc59 | 45 | r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000); |
25e7d9b1 | 46 | else if (samplerate >= MHZ(1)) |
99c1fc59 | 47 | r = snprintf(o, 30, "%" PRIu64 " MHz", samplerate / 1000000); |
25e7d9b1 | 48 | else if (samplerate >= KHZ(1)) |
38ba2522 | 49 | r = snprintf(o, 30, "%" PRIu64 " kHz", samplerate / 1000); |
25e7d9b1 | 50 | else |
99c1fc59 | 51 | r = snprintf(o, 30, "%" PRIu64 " Hz", samplerate); |
25e7d9b1 UH |
52 | |
53 | if (r < 0) { | |
54 | /* Something went wrong... */ | |
55 | free(o); | |
56 | return NULL; | |
57 | } | |
58 | ||
59 | return o; | |
60 | } | |
2a3f9541 | 61 | |
2a3f9541 BV |
62 | /** |
63 | * Convert a numeric samplerate value to the "natural" string representation | |
64 | * of its period. | |
65 | * | |
2507648e | 66 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". |
2a3f9541 BV |
67 | * |
68 | * @param frequency The frequency in Hz. | |
69 | * @return A malloc()ed string representation of the frequency value, | |
70 | * or NULL upon errors. The caller is responsible to free() the memory. | |
71 | */ | |
a00ba012 | 72 | char *sr_period_string(uint64_t frequency) |
2a3f9541 BV |
73 | { |
74 | char *o; | |
75 | int r; | |
76 | ||
77 | o = malloc(30 + 1); /* Enough for a uint64_t as string + " ms". */ | |
484760d1 | 78 | if (!o) |
2a3f9541 BV |
79 | return NULL; |
80 | ||
81 | if (frequency >= GHZ(1)) | |
82 | r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); | |
83 | else if (frequency >= MHZ(1)) | |
2507648e | 84 | r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); |
2a3f9541 BV |
85 | else if (frequency >= KHZ(1)) |
86 | r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); | |
87 | else | |
88 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); | |
89 | ||
90 | if (r < 0) { | |
91 | /* Something went wrong... */ | |
92 | free(o); | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | return o; | |
97 | } | |
40f5ddac BV |
98 | |
99 | char **sr_parse_triggerstring(struct sr_device *device, const char *triggerstring) | |
100 | { | |
101 | GSList *l; | |
102 | struct probe *probe; | |
103 | int max_probes, probenum, i; | |
104 | char **tokens, **triggerlist, *trigger, *tc, *trigger_types; | |
105 | gboolean error; | |
106 | ||
107 | max_probes = g_slist_length(device->probes); | |
108 | error = FALSE; | |
109 | triggerlist = g_malloc0(max_probes * sizeof(char *)); | |
110 | tokens = g_strsplit(triggerstring, ",", max_probes); | |
111 | trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES); | |
112 | if (trigger_types == NULL) | |
113 | return NULL; | |
114 | ||
115 | for (i = 0; tokens[i]; i++) { | |
116 | if (tokens[i][0] < '0' || tokens[i][0] > '9') { | |
117 | /* Named probe */ | |
118 | probenum = 0; | |
119 | for (l = device->probes; l; l = l->next) { | |
120 | probe = (struct probe *)l->data; | |
121 | if (probe->enabled | |
122 | && !strncmp(probe->name, tokens[i], | |
123 | strlen(probe->name))) { | |
124 | probenum = probe->index; | |
125 | break; | |
126 | } | |
127 | } | |
128 | } else { | |
129 | probenum = strtol(tokens[i], NULL, 10); | |
130 | } | |
131 | ||
132 | if (probenum < 1 || probenum > max_probes) { | |
133 | printf("Invalid probe.\n"); | |
134 | error = TRUE; | |
135 | break; | |
136 | } | |
137 | ||
138 | if ((trigger = strchr(tokens[i], '='))) { | |
139 | for (tc = ++trigger; *tc; tc++) { | |
140 | if (strchr(trigger_types, *tc) == NULL) { | |
141 | printf("Unsupported trigger type " | |
142 | "'%c'\n", *tc); | |
143 | error = TRUE; | |
144 | break; | |
145 | } | |
146 | } | |
147 | if (!error) | |
148 | triggerlist[probenum - 1] = g_strdup(trigger); | |
149 | } | |
150 | } | |
151 | g_strfreev(tokens); | |
152 | ||
153 | if (error) { | |
154 | for (i = 0; i < max_probes; i++) | |
155 | if (triggerlist[i]) | |
156 | g_free(triggerlist[i]); | |
157 | g_free(triggerlist); | |
158 | triggerlist = NULL; | |
159 | } | |
160 | ||
161 | return triggerlist; | |
162 | } | |
163 | ||
164 | uint64_t sr_parse_sizestring(const char *sizestring) | |
165 | { | |
166 | int multiplier; | |
167 | uint64_t val; | |
168 | char *s; | |
169 | ||
170 | val = strtoull(sizestring, &s, 10); | |
171 | multiplier = 0; | |
172 | while (s && *s && multiplier == 0) { | |
173 | switch (*s) { | |
174 | case ' ': | |
175 | break; | |
176 | case 'k': | |
177 | case 'K': | |
178 | multiplier = KHZ(1); | |
179 | break; | |
180 | case 'm': | |
181 | case 'M': | |
182 | multiplier = MHZ(1); | |
183 | break; | |
184 | case 'g': | |
185 | case 'G': | |
186 | multiplier = GHZ(1); | |
187 | break; | |
188 | default: | |
189 | val = 0; | |
190 | multiplier = -1; | |
191 | } | |
192 | s++; | |
193 | } | |
194 | if (multiplier > 0) | |
195 | val *= multiplier; | |
196 | ||
197 | return val; | |
198 | } | |
199 | ||
200 | uint64_t sr_parse_timestring(const char *timestring) | |
201 | { | |
202 | uint64_t time_msec; | |
203 | char *s; | |
204 | ||
205 | time_msec = strtoull(timestring, &s, 10); | |
206 | if (time_msec == 0 && s == timestring) | |
207 | return 0; | |
208 | ||
209 | if (s && *s) { | |
210 | while (*s == ' ') | |
211 | s++; | |
212 | if (!strcmp(s, "s")) | |
213 | time_msec *= 1000; | |
214 | else if (!strcmp(s, "ms")) | |
215 | ; /* redundant */ | |
216 | else | |
217 | return 0; | |
218 | } | |
219 | ||
220 | return time_msec; | |
221 | } | |
222 |