From: Soeren Apel Date: Sat, 25 Feb 2017 21:19:42 +0000 (+0100) Subject: Rework sr_period_string X-Git-Tag: libsigrok-0.5.0~100 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=6984cfb245811df0f691928a6e4224d4f7ac5786;ds=sidebyside Rework sr_period_string --- diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h index 05e78864..01792a62 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 b0cdfe77..5b30648f 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 066bed2f..73691081 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 08866898..cf885f7a 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 ba06c5ae..b97a4c38 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 4b5b9ec7..3b0afade 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 86f25f39..a36b6a2d 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