Skip to content

Commit 32a87d3

Browse files
drashnatzarc
andauthored
Add uint to char functions (qmk#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 <[email protected]> * Apply suggestions from code review Co-authored-by: Nick Brassel <[email protected]> * Add declaration for get_numeric_string * fix formatting and bug Co-authored-by: Nick Brassel <[email protected]>
1 parent 3f656d5 commit 32a87d3

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

quantum/quantum.c

+56
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,59 @@ __attribute__((weak)) void suspend_wakeup_init_quantum(void) {
572572
#endif
573573
suspend_wakeup_init_kb();
574574
}
575+
576+
/** \brief converts unsigned integers into char arrays
577+
*
578+
* Takes an unsigned integer and converts that value into an equivalent char array
579+
* A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
580+
*/
581+
582+
const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad) {
583+
buf[buf_len - 1] = '\0';
584+
for (size_t i = 0; i < buf_len - 1; ++i) {
585+
char c = '0' + curr_num % 10;
586+
buf[buf_len - 2 - i] = (c == '0' && i == 0) ? '0' : (curr_num > 0 ? c : curr_pad);
587+
curr_num /= 10;
588+
}
589+
return buf;
590+
}
591+
592+
/** \brief converts uint8_t into char array
593+
*
594+
* Takes an uint8_t, and uses an internal static buffer to render that value into a char array
595+
* A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
596+
*
597+
* NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
598+
* contents. Use the result immediately, instead of caching it.
599+
*/
600+
const char *get_u8_str(uint8_t curr_num, char curr_pad) {
601+
static char buf[4] = {0};
602+
static uint8_t last_num = 0xFF;
603+
static char last_pad = '\0';
604+
if (last_num == curr_num && last_pad == curr_pad) {
605+
return buf;
606+
}
607+
last_num = curr_num;
608+
last_pad = curr_pad;
609+
return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
610+
}
611+
612+
/** \brief converts uint16_t into char array
613+
*
614+
* Takes an uint16_t, and uses an internal static buffer to render that value into a char array
615+
* A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
616+
*
617+
* NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
618+
* contents. Use the result immediately, instead of caching it.
619+
*/
620+
const char *get_u16_str(uint16_t curr_num, char curr_pad) {
621+
static char buf[6] = {0};
622+
static uint16_t last_num = 0xFF;
623+
static char last_pad = '\0';
624+
if (last_num == curr_num && last_pad == curr_pad) {
625+
return buf;
626+
}
627+
last_num = curr_num;
628+
last_pad = curr_pad;
629+
return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
630+
}

quantum/quantum.h

+4
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,7 @@ void led_set_user(uint8_t usb_led);
245245
void led_set_kb(uint8_t usb_led);
246246
bool led_update_user(led_t led_state);
247247
bool led_update_kb(led_t led_state);
248+
249+
const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad);
250+
const char *get_u8_str(uint8_t curr_num, char curr_pad);
251+
const char *get_u16_str(uint16_t curr_num, char curr_pad);

0 commit comments

Comments
 (0)