Skip to content

Commit

Permalink
Fix leading zero being displayed (#190)
Browse files Browse the repository at this point in the history
* Rewrote the digit drawing algorithm

* Ensure that digits are rendered only when in 1s place, highlighted, or meets min digit value
Otherwise, blanked.

* Signed power value
  • Loading branch information
kohrar authored Jul 12, 2020
1 parent 75a59d5 commit ed4e00e
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions opendps/uui_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,51 +234,56 @@ static void number_draw(ui_item_t *_item)
xpos -= number_draw_width(_item);

/** Start printing from left to right */
for (uint8_t place = item->num_digits; place > 0; place--) {
/* Example value of 1000 with 5,2:
01000 . 00 num_digits = 5, num_decimals = 2
54321 values place
65432 . 10 cur_digit
*/

/** Digits before the decimal point */
for (uint32_t i = item->num_digits - 1; i < item->num_digits; --i) {
// current digit
cur_digit = place + item->num_decimals - 1;

// this place value (1 = 1, 2 = 10, 3 = 100, etc., for si_prefix = 0)
int32_t power = my_pow(10, (item->si_prefix * -1) + (place - 1));

uint8_t digit = (item->value / power) % 10;

// digit selected
bool highlight = _item->has_focus && item->cur_digit == cur_digit;
uint8_t digit = item->value / my_pow(10, (item->si_prefix * -1) + i) % 10;

if (!digit /** If current digit is a 0 */
&& !_item->has_focus /** and its not in focus (selected) */
&& cur_digit != item->num_decimals /** to prevent 0.123 becoming .123 */
&& my_pow(10, cur_digit) > (uint32_t) item->value) /** to prevent 4023 becoming 4 23 */
{
/** To prevent from printing 00.123 */
/** Black out any 0 that has previously been printed */
if (spacing > 1)
{
tft_fill(xpos, _item->y, digit_w, h, BLACK);
tft_rect(xpos-1, _item->y-1, digit_w+1, h+1, BLACK); /** Remove any potential frame highlight */
// Draw background either black, or a highlighted box
if (spacing > 1) {
if (highlight) {
tft_rect(xpos-1, _item->y-1, digit_w+1, h+1, WHITE);
} else {
tft_rect(xpos-1, _item->y-1, digit_w+1, h+1, BLACK);
}
else
tft_fill(xpos, _item->y, digit_w, h, BLACK); /** Black out any 0 that has previously been printed */
}
else
{
if (spacing > 1) /** Dont frame tiny fonts */
{
if (highlight) /** Draw an extra pixel wide border around the highlighted item */
tft_rect(xpos-1, _item->y-1, digit_w+1, h+1, WHITE);
else
tft_rect(xpos-1, _item->y-1, digit_w+1, h+1, BLACK);
}

// Draw the digit, Only if:
// value >= this place's min value (ie. digit's power)
// in one's place (ensuring 0.xxx has leading 0)
// or item has focus (ensures all digits are drawn when focused)
if (item->value >= power || place == 1 || _item->has_focus) {
// ASCII '0' plus digit value for digit ascii offset
tft_putch(item->font_size, '0' + digit, xpos, _item->y, digit_w, h, color, highlight);
} else {
tft_fill(xpos, _item->y, digit_w, h, BLACK);
}

cur_digit--;
// next digit position
xpos += digit_w + spacing;
}

/** Draw the decimal point if there is decimal places */
if (item->num_decimals)
{
/** Draw the decimal point if there are decimal places */
if (item->num_decimals) {
tft_putch(item->font_size, '.', xpos, _item->y, dot_width, h, color, false);
xpos += dot_width + spacing;
}

/** Digits after the decimal point */
cur_digit = item->num_decimals - 1;
for (uint32_t i = 0; i < item->num_decimals; ++i) {
bool highlight = _item->has_focus && item->cur_digit == cur_digit;
uint8_t digit = item->value / my_pow(10, (item->si_prefix * -1) -1 - i) % 10;
Expand Down

0 comments on commit ed4e00e

Please sign in to comment.