Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uint to char functions #15244

Merged
merged 9 commits into from
Nov 22, 2021
Merged

Add uint to char functions #15244

merged 9 commits into from
Nov 22, 2021

Conversation

drashna
Copy link
Member

@drashna drashna commented Nov 21, 2021

Description

Adds functions to convert an unsigned int to a char array. Useful for OLEDs and displays, especially on AVR, as things like sprintf take up ~1.5kB of firmware space.

And instead of continually adding the same block of code, add a function into core to do this.

Types of Changes

  • Core
  • Enhancement/optimization
  • Documentation

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@drashna drashna requested review from tzarc and a team November 21, 2021 00:28
@github-actions github-actions bot added the core label Nov 21, 2021
quantum/quantum.c Outdated Show resolved Hide resolved
Comment on lines 513 to 515
buf[3] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[2] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[1] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
buf[3] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[2] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[1] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[3] = (curr_num /= 10) ? '0' + curr_num % 10 : curr_pad;
buf[2] = (curr_num /= 10) ? '0' + curr_num % 10 : curr_pad;
buf[1] = (curr_num /= 10) ? '0' + curr_num % 10 : curr_pad;

The original code would not work properly for a number like 1001 (with at least two consecutive zeros); the whole remaining number needs to be tested when deciding whether the zero needs to be replaced with the padding character.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, went through a number of iterations, the current code should work properly, but please take a look at it.

quantum/quantum.c Outdated Show resolved Hide resolved
quantum/quantum.c Show resolved Hide resolved
quantum/quantum.c Show resolved Hide resolved
@tzarc tzarc mentioned this pull request Nov 22, 2021
8 tasks
@tzarc tzarc changed the base branch from master to develop November 22, 2021 16:42
quantum/quantum.c Outdated Show resolved Hide resolved
Copy link
Member

@tzarc tzarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The static buffer note needed to be placed on the other APIs. My bad.
We should also put the function declarations into quantum.h.

quantum/quantum.c Outdated Show resolved Hide resolved
quantum/quantum.c Outdated Show resolved Hide resolved
quantum/quantum.c Show resolved Hide resolved
drashna and others added 2 commits November 22, 2021 08:55
@tzarc tzarc merged commit 32a87d3 into qmk:develop Nov 22, 2021
@drashna drashna deleted the core/strings branch November 23, 2021 00:07
cadusk pushed a commit to cadusk/qmk_firmware that referenced this pull request Dec 2, 2021
* qmk/develop: (80 commits)
  Remove use of __flash due to LTO issues (qmk#15268)
  Revert "handwired/split89 Layout Macro Refactor (qmk#15210)" (qmk#15284)
  New Keyboard: TGR Jane CE (qmk#14713)
  Portal 66 Layout Macro Refactor (qmk#15255)
  Pluckey: Fix QMK Configurator Implementation (qmk#15254)
  [Tests] Increase QMK test coverage take 2 (qmk#15269)
  Ignore exit codes for formatters (qmk#15276)
  [Keyboard] Disable features on SplitKB boards to fit under size (qmk#15262)
  Ignore exit codes for formatters (qmk#15275)
  Ignore deleted files when formatting codebase (qmk#15274)
  qmk format-python - filter for Python files (qmk#15271)
  Revert "[Tests] Increase QMK test coverage (qmk#13789)"
  [Tests] Increase QMK test coverage (qmk#13789)
  [Docs] Squeezing space out of AVR (qmk#15243)
  Add uint to char functions (qmk#15244)
  [Keyboard] Disable console on Keebio foldkb and iris rev3 (qmk#15260)
  layer_combo → sd_combo (qmk#15266)
  [Keymap] Disable console on Sofle default keymap (qmk#15261)
  [Keyboard] Enable LTO on viktus/sp_mini via keymap (qmk#15263)
  Macros in JSON keymaps (qmk#14374)
  ...
yokada-code added a commit to yokada-code/qmk_firmware that referenced this pull request Aug 18, 2023
* 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>

(Cherry-pick from: 32a87d3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants