sr_period_string() cannot handle frequencies below 1Hz as it takes
a uint64_t. Add a new sr_period_string_f() function to keep the API
compatible.

Signed-off-by: Sven Schnelle <sv...@stackframe.org>
---
 include/libsigrok/proto.h |  1 +
 src/strutil.c             | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h
index 05e78864..517b5868 100644
--- a/include/libsigrok/proto.h
+++ b/include/libsigrok/proto.h
@@ -227,6 +227,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_f(float frequency, int precision);
 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/strutil.c b/src/strutil.c
index 4b5b9ec7..7aaac9cd 100644
--- a/src/strutil.c
+++ b/src/strutil.c
@@ -386,6 +386,49 @@ SR_API char *sr_period_string(uint64_t frequency)
        return o;
 }
 
+
+/**
+ * Convert a numeric frequency value to the "natural" string representation
+ * of its period.
+ *
+ * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
+ *
+ * @param frequency The frequency in Hz.
+ *
+ * @return A newly allocated string representation of the frequency value,
+ *         or NULL upon errors. The caller is responsible to g_free() the
+ *         memory.
+ *
+ * @since 0.5.0
+ */
+SR_API char *sr_period_string_f(float frequency, int precision)
+{
+       char *o;
+       int r;
+
+       /* Allocate enough for a uint64_t as string + " ms". */
+       o = g_malloc0(30 + 1);
+
+       if (frequency > SR_GHZ(1))
+               r = snprintf(o, 30, "%.*f ps", precision, 1000000000000.0 / 
frequency);
+       else if (frequency > SR_MHZ(1))
+               r = snprintf(o, 30, "%.*f ns", precision, 1000000000.0 / 
frequency);
+       else if (frequency > SR_KHZ(1))
+               r = snprintf(o, 30, "%.*f us", precision, 1000000.0 / 
frequency);
+       else if (frequency > 1)
+               r = snprintf(o, 30, "%.*f ms", precision, 1000.0 / frequency);
+       else
+               r = snprintf(o, 30, "%.*f s", precision, 1.0 / frequency);
+
+       if (r < 0) {
+               /* Something went wrong... */
+               g_free(o);
+               return NULL;
+       }
+
+       return o;
+}
+
 /**
  * Convert a numeric voltage value to the "natural" string representation
  * of its voltage value. The voltage is specified as a rational number's
-- 
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to