Rework sr_period_string

This commit is contained in:
Soeren Apel 2017-02-25 22:19:42 +01:00 committed by Uwe Hermann
parent ae87e02fad
commit 6984cfb245
7 changed files with 58 additions and 39 deletions

View File

@ -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_si_string_u64(uint64_t x, const char *unit);
SR_API char *sr_samplerate_string(uint64_t samplerate); 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 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 int sr_parse_sizestring(const char *sizestring, uint64_t *size);
SR_API uint64_t sr_parse_timestring(const char *timestring); SR_API uint64_t sr_parse_timestring(const char *timestring);

View File

@ -364,9 +364,8 @@ static void scope_state_dump(const struct scope_config *config,
state->digital_pods[i] ? "On" : "Off"); state->digital_pods[i] ? "On" : "Off");
} }
/* FIXME: this is wrong for TB > 1 second */ tmp = sr_period_string((*config->timebases)[state->timebase][0],
tmp = sr_period_string((*config->timebases)[state->timebase][1] / (*config->timebases)[state->timebase][1]);
(*config->timebases)[state->timebase][0]);
sr_info("Current timebase: %s", tmp); sr_info("Current timebase: %s", tmp);
g_free(tmp); g_free(tmp);

View File

@ -236,8 +236,8 @@ static void scope_state_dump(const struct scope_config *config,
tmp, state->analog_channels[i].vertical_offset); tmp, state->analog_channels[i].vertical_offset);
} }
tmp = sr_period_string(((float)config->timebases[state->timebase].q) / tmp = sr_period_string(config->timebases[state->timebase].p,
((float)config->timebases[state->timebase].p)); config->timebases[state->timebase].q);
sr_info("Current timebase: %s", tmp); sr_info("Current timebase: %s", tmp);
g_free(tmp); g_free(tmp);

View File

@ -305,8 +305,8 @@ static void scope_state_dump(const struct scope_config *config,
state->pod_states[i] ? "On" : "Off"); state->pod_states[i] ? "On" : "Off");
} }
tmp = sr_period_string(dlm_timebases[state->timebase][1] / tmp = sr_period_string(dlm_timebases[state->timebase][0],
dlm_timebases[state->timebase][0]); dlm_timebases[state->timebase][1]);
sr_info("Current timebase: %s", tmp); sr_info("Current timebase: %s", tmp);
g_free(tmp); g_free(tmp);

View File

@ -129,7 +129,7 @@ static GString *gen_header(const struct sr_output *o)
ctx->period = SR_MHZ(1); ctx->period = SR_MHZ(1);
else else
ctx->period = SR_KHZ(1); 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_string_append_printf(header, "$timescale %s $end\n", frequency_s);
g_free(frequency_s); g_free(frequency_s);

View File

@ -360,22 +360,37 @@ SR_API char *sr_samplerate_string(uint64_t samplerate)
* *
* @since 0.1.0 * @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; 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); o = g_malloc0(30 + 1);
if (frequency >= SR_GHZ(1)) if (freq > SR_GHZ(1)) {
r = snprintf(o, 30, "%lld ps", 1000000000000ull / frequency); v = (double)v_p / v_q * 1000000000000.0;
else if (frequency >= SR_MHZ(1)) prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
r = snprintf(o, 30, "%lld ns", 1000000000ull / frequency); r = snprintf(o, 30, "%.*f ps", prec, v);
else if (frequency >= SR_KHZ(1)) } else if (freq > SR_MHZ(1)) {
r = snprintf(o, 30, "%lld us", 1000000ull / frequency); v = (double)v_p / v_q * 1000000000.0;
else prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3;
r = snprintf(o, 30, "%lld ms", 1000ull / frequency); 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) { if (r < 0) {
/* Something went wrong... */ /* Something went wrong... */

View File

@ -33,11 +33,11 @@ static void test_samplerate(uint64_t samplerate, const char *expected)
g_free(s); 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; char *s;
s = sr_period_string(frequency); s = sr_period_string(v_p, v_q);
fail_unless(s != NULL); fail_unless(s != NULL);
fail_unless(!strcmp(s, expected), fail_unless(!strcmp(s, expected),
"Invalid result for '%s': %s.", expected, s); "Invalid result for '%s': %s.", expected, s);
@ -179,33 +179,38 @@ END_TEST
START_TEST(test_hz_period) START_TEST(test_hz_period)
{ {
test_period(1, "1000 ms"); test_period(1, 1, "1 s");
test_period(5, "200 ms"); test_period(1, 5, "200 ms");
test_period(72, "13 ms"); test_period(1, 72, "13.889 ms");
test_period(388, "2 ms"); test_period(1, 388, "2.577 ms");
test_period(10, 1000, "10 ms");
/* Again, but now using SR_HZ(). */ /* Again, but now using SR_HZ(). */
test_period(SR_HZ(1), "1000 ms"); test_period(1, SR_HZ(1), "1 s");
test_period(SR_HZ(5), "200 ms"); test_period(1, SR_HZ(5), "200 ms");
test_period(SR_HZ(72), "13 ms"); test_period(1, SR_HZ(72), "13.889 ms");
test_period(SR_HZ(388), "2 ms"); test_period(1, SR_HZ(388), "2.577 ms");
test_period(10, SR_HZ(100), "100 ms");
} }
END_TEST END_TEST
START_TEST(test_ghz_period) START_TEST(test_ghz_period)
{ {
/* Note: Numbers > 2^32 need a ULL suffix. */ /* Note: Numbers > 2^32 need a ULL suffix. */
test_period(1, 1000000000, "1 ns");
test_period(1000000000, "1000 ps"); test_period(1, 5000000000ULL, "200 ps");
test_period(5000000000ULL, "200 ps"); test_period(1, 72000000000ULL, "13.889 ps");
test_period(72000000000ULL, "13 ps"); test_period(1, 388000000000ULL, "2.577 ps");
test_period(388000000000ULL, "2 ps"); test_period(10, 1000000000000, "10 ps");
test_period(200, 1000000000000ULL, "200 ps");
/* Again, but now using SR_GHZ(). */ /* Again, but now using SR_GHZ(). */
test_period(SR_GHZ(1), "1000 ps"); test_period(1, SR_GHZ(1), "1 ns");
test_period(SR_GHZ(5), "200 ps"); test_period(1, SR_GHZ(5), "200 ps");
test_period(SR_GHZ(72), "13 ps"); test_period(1, SR_GHZ(72), "13.889 ps");
test_period(SR_GHZ(388), "2 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 END_TEST