2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
27 * Convert a numeric samplerate value to its "natural" string representation.
29 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz".
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.
35 char *sr_samplerate_string(uint64_t samplerate)
40 o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */
44 if (samplerate >= GHZ(1))
45 r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000);
46 else if (samplerate >= MHZ(1))
47 r = snprintf(o, 30, "%" PRIu64 " MHz", samplerate / 1000000);
48 else if (samplerate >= KHZ(1))
49 r = snprintf(o, 30, "%" PRIu64 " kHz", samplerate / 1000);
51 r = snprintf(o, 30, "%" PRIu64 " Hz", samplerate);
54 /* Something went wrong... */
63 * Convert a numeric frequency value to the "natural" string representation
66 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
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.
72 char *sr_period_string(uint64_t frequency)
77 o = malloc(30 + 1); /* Enough for a uint64_t as string + " ms". */
81 if (frequency >= GHZ(1))
82 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
83 else if (frequency >= MHZ(1))
84 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
85 else if (frequency >= KHZ(1))
86 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
88 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
91 /* Something went wrong... */
103 * @param triggerstring TODO
106 char **sr_parse_triggerstring(struct sr_device *device,
107 const char *triggerstring)
110 struct sr_probe *probe;
111 int max_probes, probenum, i;
112 char **tokens, **triggerlist, *trigger, *tc, *trigger_types;
115 max_probes = g_slist_length(device->probes);
117 triggerlist = g_malloc0(max_probes * sizeof(char *));
118 tokens = g_strsplit(triggerstring, ",", max_probes);
119 trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES);
120 if (trigger_types == NULL)
123 for (i = 0; tokens[i]; i++) {
124 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
127 for (l = device->probes; l; l = l->next) {
128 probe = (struct sr_probe *)l->data;
130 && !strncmp(probe->name, tokens[i],
131 strlen(probe->name))) {
132 probenum = probe->index;
137 probenum = strtol(tokens[i], NULL, 10);
140 if (probenum < 1 || probenum > max_probes) {
141 printf("Invalid probe.\n");
146 if ((trigger = strchr(tokens[i], '='))) {
147 for (tc = ++trigger; *tc; tc++) {
148 if (strchr(trigger_types, *tc) == NULL) {
149 printf("Unsupported trigger type "
156 triggerlist[probenum - 1] = g_strdup(trigger);
162 for (i = 0; i < max_probes; i++)
164 g_free(triggerlist[i]);
173 * Convert a "natural" string representation of a size value to uint64_t.
175 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
176 * of "15M" would be converted to 15000000.
178 * Value representations other than decimal (such as hex or octal) are not
179 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
180 * Spaces (but not other whitespace) between value and suffix are allowed.
182 * @param sizestring A string containing a (decimal) size value.
183 * @return The string's size value as uint64_t.
185 * TODO: Error handling.
187 uint64_t sr_parse_sizestring(const char *sizestring)
193 val = strtoull(sizestring, &s, 10);
195 while (s && *s && multiplier == 0) {
224 * Convert a "natural" string representation of a time value to an
225 * uint64_t value in milliseconds.
227 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
228 * of "15ms" would be converted to 15.
230 * Value representations other than decimal (such as hex or octal) are not
231 * supported. Only lower-case "s" and "ms" time suffixes are supported.
232 * Spaces (but not other whitespace) between value and suffix are allowed.
234 * @param timestring A string containing a (decimal) time value.
235 * @return The string's time value as uint64_t, in milliseconds.
237 * TODO: Error handling.
238 * TODO: Add support for "m" (minutes) and others.
240 * TODO: Allow both lower-case and upper-case.
242 uint64_t sr_parse_timestring(const char *timestring)
247 time_msec = strtoull(timestring, &s, 10);
248 if (time_msec == 0 && s == timestring)
256 else if (!strcmp(s, "ms"))