aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoeren Apel <soeren@apelpie.net>2017-02-25 22:19:42 +0100
committerUwe Hermann <uwe@hermann-uwe.de>2017-03-07 17:13:46 +0100
commit6984cfb245811df0f691928a6e4224d4f7ac5786 (patch)
treed393b17d5cbea1ac997ce3008dbde67de5ad5d84
parentae87e02fad06324de80c24997fc8589780ba3b03 (diff)
Rework sr_period_string
-rw-r--r--include/libsigrok/proto.h2
-rw-r--r--src/hardware/hameg-hmo/protocol.c5
-rw-r--r--src/hardware/lecroy-xstream/protocol.c4
-rw-r--r--src/hardware/yokogawa-dlm/protocol.c4
-rw-r--r--src/output/vcd.c2
-rw-r--r--src/strutil.c37
-rw-r--r--tests/strutil.c43
7 files changed, 58 insertions, 39 deletions
diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h
index 05e7886..01792a6 100644
--- a/include/libsigrok/proto.h
+++ b/include/libsigrok/proto.h
@@ -226,7 +226,7 @@ SR_API int sr_resource_set_hooks(struct sr_context *ctx,
SR_API char *sr_si_string_u64(uint64_t x, const char *unit);
SR_API char *sr_samplerate_string(uint64_t samplerate);
-SR_API char *sr_period_string(uint64_t frequency);
+SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q);
SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q);
SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size);
SR_API uint64_t sr_parse_timestring(const char *timestring);
diff --git a/src/hardware/hameg-hmo/protocol.c b/src/hardware/hameg-hmo/protocol.c
index b0cdfe7..5b30648 100644
--- a/src/hardware/hameg-hmo/protocol.c
+++ b/src/hardware/hameg-hmo/protocol.c
@@ -364,9 +364,8 @@ static void scope_state_dump(const struct scope_config *config,
state->digital_pods[i] ? "On" : "Off");
}
- /* FIXME: this is wrong for TB > 1 second */
- tmp = sr_period_string((*config->timebases)[state->timebase][1] /
- (*config->timebases)[state->timebase][0]);
+ tmp = sr_period_string((*config->timebases)[state->timebase][0],
+ (*config->timebases)[state->timebase][1]);
sr_info("Current timebase: %s", tmp);
g_free(tmp);
diff --git a/src/hardware/lecroy-xstream/protocol.c b/src/hardware/lecroy-xstream/protocol.c
index 066bed2..7369108 100644
--- a/src/hardware/lecroy-xstream/protocol.c
+++ b/src/hardware/lecroy-xstream/protocol.c
@@ -236,8 +236,8 @@ static void scope_state_dump(const struct scope_config *config,
tmp, state->analog_channels[i].vertical_offset);
}
- tmp = sr_period_string(((float)config->timebases[state->timebase].q) /
- ((float)config->timebases[state->timebase].p));
+ tmp = sr_period_string(config->timebases[state->timebase].p,
+ config->timebases[state->timebase].q);
sr_info("Current timebase: %s", tmp);
g_free(tmp);
diff --git a/src/hardware/yokogawa-dlm/protocol.c b/src/hardware/yokogawa-dlm/protocol.c
index 0886689..cf885f7 100644
--- a/src/hardware/yokogawa-dlm/protocol.c
+++ b/src/hardware/yokogawa-dlm/protocol.c
@@ -305,8 +305,8 @@ static void scope_state_dump(const struct scope_config *config,
state->pod_states[i] ? "On" : "Off");
}
- tmp = sr_period_string(dlm_timebases[state->timebase][1] /
- dlm_timebases[state->timebase][0]);
+ tmp = sr_period_string(dlm_timebases[state->timebase][0],
+ dlm_timebases[state->timebase][1]);
sr_info("Current timebase: %s", tmp);
g_free(tmp);
diff --git a/src/output/vcd.c b/src/output/vcd.c
index ba06c5a..b97a4c3 100644
--- a/src/output/vcd.c
+++ b/src/output/vcd.c
@@ -129,7 +129,7 @@ static GString *gen_header(const struct sr_output *o)
ctx->period = SR_MHZ(1);
else
ctx->period = SR_KHZ(1);
- frequency_s = sr_period_string(ctx->period);
+ frequency_s = sr_period_string(1, ctx->period);
g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
g_free(frequency_s);
diff --git a/src/strutil.c b/src/strutil.c
index 4b5b9ec..3b0afad 100644
--- a/src/strutil.c
+++ b/src/strutil.c
@@ -360,22 +360,37 @@ SR_API char *sr_samplerate_string(uint64_t samplerate)
*
* @since 0.1.0
*/
-SR_API char *sr_period_string(uint64_t frequency)
+SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q)
{
+ double freq, v;
char *o;
- int r;
+ int prec, r;
+
+ freq = 1 / ((double)v_p / v_q);
- /* Allocate enough for a uint64_t as string + " ms". */
o = g_malloc0(30 + 1);
- if (frequency >= SR_GHZ(1))
- r = snprintf(o, 30, "%lld ps", 1000000000000ull / frequency);
- else if (frequency >= SR_MHZ(1))
- r = snprintf(o, 30, "%lld ns", 1000000000ull / frequency);
- else if (frequency >= SR_KHZ(1))
- r = snprintf(o, 30, "%lld us", 1000000ull / frequency);
- else
- r = snprintf(o, 30, "%lld ms", 1000ull / frequency);
+ if (freq > SR_GHZ(1)) {
+ v = (double)v_p / v_q * 1000000000000.0;
+ prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
+ r = snprintf(o, 30, "%.*f ps", prec, v);
+ } else if (freq > SR_MHZ(1)) {
+ v = (double)v_p / v_q * 1000000000.0;
+ prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
+ r = snprintf(o, 30, "%.*f ns", prec, v);
+ } else if (freq > SR_KHZ(1)) {
+ v = (double)v_p / v_q * 1000000.0;
+ prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
+ r = snprintf(o, 30, "%.*f us", prec, v);
+ } else if (freq > 1) {
+ v = (double)v_p / v_q * 1000.0;
+ prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
+ r = snprintf(o, 30, "%.*f ms", prec, v);
+ } else {
+ v = (double)v_p / v_q;
+ prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
+ r = snprintf(o, 30, "%.*f s", prec, v);
+ }
if (r < 0) {
/* Something went wrong... */
diff --git a/tests/strutil.c b/tests/strutil.c
index 86f25f3..a36b6a2 100644
--- a/tests/strutil.c
+++ b/tests/strutil.c
@@ -33,11 +33,11 @@ static void test_samplerate(uint64_t samplerate, const char *expected)
g_free(s);
}
-static void test_period(uint64_t frequency, const char *expected)
+static void test_period(uint64_t v_p, uint64_t v_q, const char *expected)
{
char *s;
- s = sr_period_string(frequency);
+ s = sr_period_string(v_p, v_q);
fail_unless(s != NULL);
fail_unless(!strcmp(s, expected),
"Invalid result for '%s': %s.", expected, s);
@@ -179,33 +179,38 @@ END_TEST
START_TEST(test_hz_period)
{
- test_period(1, "1000 ms");
- test_period(5, "200 ms");
- test_period(72, "13 ms");
- test_period(388, "2 ms");
+ test_period(1, 1, "1 s");
+ test_period(1, 5, "200 ms");
+ test_period(1, 72, "13.889 ms");
+ test_period(1, 388, "2.577 ms");
+ test_period(10, 1000, "10 ms");
/* Again, but now using SR_HZ(). */
- test_period(SR_HZ(1), "1000 ms");
- test_period(SR_HZ(5), "200 ms");
- test_period(SR_HZ(72), "13 ms");
- test_period(SR_HZ(388), "2 ms");
+ test_period(1, SR_HZ(1), "1 s");
+ test_period(1, SR_HZ(5), "200 ms");
+ test_period(1, SR_HZ(72), "13.889 ms");
+ test_period(1, SR_HZ(388), "2.577 ms");
+ test_period(10, SR_HZ(100), "100 ms");
}
END_TEST
START_TEST(test_ghz_period)
{
/* Note: Numbers > 2^32 need a ULL suffix. */
-
- test_period(1000000000, "1000 ps");
- test_period(5000000000ULL, "200 ps");
- test_period(72000000000ULL, "13 ps");
- test_period(388000000000ULL, "2 ps");
+ test_period(1, 1000000000, "1 ns");
+ test_period(1, 5000000000ULL, "200 ps");
+ test_period(1, 72000000000ULL, "13.889 ps");
+ test_period(1, 388000000000ULL, "2.577 ps");
+ test_period(10, 1000000000000, "10 ps");
+ test_period(200, 1000000000000ULL, "200 ps");
/* Again, but now using SR_GHZ(). */
- test_period(SR_GHZ(1), "1000 ps");
- test_period(SR_GHZ(5), "200 ps");
- test_period(SR_GHZ(72), "13 ps");
- test_period(SR_GHZ(388), "2 ps");
+ test_period(1, SR_GHZ(1), "1 ns");
+ test_period(1, SR_GHZ(5), "200 ps");
+ test_period(1, SR_GHZ(72), "13.889 ps");
+ test_period(1, SR_GHZ(388), "2.577 ps");
+ test_period(10, SR_GHZ(1), "10 ns");
+ test_period(200, SR_GHZ(1000), "200 ps");
}
END_TEST