]> sigrok.org Git - libsigrok.git/blame - output/common.c
MinGW/Windows: Serial port portability fixes.
[libsigrok.git] / output / common.c
CommitLineData
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 */
35char *sigrok_samplerate_string(uint64_t samplerate)
36{
37 char *o;
38 int r;
39
40 o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */
41 if (o == NULL)
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
BV
61
62
63/**
64 * Convert a numeric samplerate value to the "natural" string representation
65 * of its period.
66 *
67 * E.g. a value of 3000000 would be converted to "3 µs", 20000 to "50 ms".
68 *
69 * @param frequency The frequency in Hz.
70 * @return A malloc()ed string representation of the frequency value,
71 * or NULL upon errors. The caller is responsible to free() the memory.
72 */
73char *sigrok_period_string(uint64_t frequency)
74{
75 char *o;
76 int r;
77
78 o = malloc(30 + 1); /* Enough for a uint64_t as string + " ms". */
79 if (o == NULL)
80 return NULL;
81
82 if (frequency >= GHZ(1))
83 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
84 else if (frequency >= MHZ(1))
85 r = snprintf(o, 30, "%" PRIu64 " µs", frequency / 1000000);
86 else if (frequency >= KHZ(1))
87 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
88 else
89 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
90
91 if (r < 0) {
92 /* Something went wrong... */
93 free(o);
94 return NULL;
95 }
96
97 return o;
98}
99