summaryrefslogtreecommitdiff
path: root/quantum/quantum.c
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2021-11-22 13:13:22 -0800
committerGitHub <noreply@github.com>2021-11-23 08:13:22 +1100
commit32a87d3517f086f6c76e9f3416a4b8fcff5ae626 (patch)
tree948c54fbac14626098f7ba36c31dc583bfc2cdb8 /quantum/quantum.c
parent3f656d54d3a60e0b1c932ea8412966c0b9c497a0 (diff)
downloadqmk_firmware-32a87d3517f086f6c76e9f3416a4b8fcff5ae626.tar.gz
qmk_firmware-32a87d3517f086f6c76e9f3416a4b8fcff5ae626.zip
Add uint to char functions (#15244)
* Add uint to char functions * appease the all mighty lint * Further appease Lint * Update functions * Add doxygen comment * Update quantum/quantum.c Co-authored-by: Nick Brassel <nick@tzarc.org> * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * Add declaration for get_numeric_string * fix formatting and bug Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'quantum/quantum.c')
-rw-r--r--quantum/quantum.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/quantum/quantum.c b/quantum/quantum.c
index c7a3bb1970..ba3ae03457 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -572,3 +572,59 @@ __attribute__((weak)) void suspend_wakeup_init_quantum(void) {
#endif
suspend_wakeup_init_kb();
}
+
+/** \brief converts unsigned integers into char arrays
+ *
+ * Takes an unsigned integer and converts that value into an equivalent char array
+ * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
+ */
+
+const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad) {
+ buf[buf_len - 1] = '\0';
+ for (size_t i = 0; i < buf_len - 1; ++i) {
+ char c = '0' + curr_num % 10;
+ buf[buf_len - 2 - i] = (c == '0' && i == 0) ? '0' : (curr_num > 0 ? c : curr_pad);
+ curr_num /= 10;
+ }
+ return buf;
+}
+
+/** \brief converts uint8_t into char array
+ *
+ * Takes an uint8_t, and uses an internal static buffer to render that value into a char array
+ * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
+ *
+ * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
+ * contents. Use the result immediately, instead of caching it.
+ */
+const char *get_u8_str(uint8_t curr_num, char curr_pad) {
+ static char buf[4] = {0};
+ static uint8_t last_num = 0xFF;
+ static char last_pad = '\0';
+ if (last_num == curr_num && last_pad == curr_pad) {
+ return buf;
+ }
+ last_num = curr_num;
+ last_pad = curr_pad;
+ return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
+}
+
+/** \brief converts uint16_t into char array
+ *
+ * Takes an uint16_t, and uses an internal static buffer to render that value into a char array
+ * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
+ *
+ * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
+ * contents. Use the result immediately, instead of caching it.
+ */
+const char *get_u16_str(uint16_t curr_num, char curr_pad) {
+ static char buf[6] = {0};
+ static uint16_t last_num = 0xFF;
+ static char last_pad = '\0';
+ if (last_num == curr_num && last_pad == curr_pad) {
+ return buf;
+ }
+ last_num = curr_num;
+ last_pad = curr_pad;
+ return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
+}