Skip to content

Commit

Permalink
Add uint to char functions (#15244)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
drashna and tzarc authored Nov 22, 2021
1 parent 3f656d5 commit 32a87d3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
56 changes: 56 additions & 0 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,7 @@ void led_set_user(uint8_t usb_led);
void led_set_kb(uint8_t usb_led);
bool led_update_user(led_t led_state);
bool led_update_kb(led_t led_state);

const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad);
const char *get_u8_str(uint8_t curr_num, char curr_pad);
const char *get_u16_str(uint16_t curr_num, char curr_pad);

0 comments on commit 32a87d3

Please sign in to comment.