Closed bepitama closed 4 years ago
Thanks for this addition, is really appreciated. I will look at it in depth probably this weekend.
tip
if you want to post code you can simply add 4 spaces in front
// -000..-999
void HT16K33::displayNegInt(int n)
{
uint8_t x[4], h, l;
etc
or use triple backquote + language => see https://guides.github.com/features/mastering-markdown/ to get syntax highlighting
// -000..-999
void HT16K33::displayNegInt(int n)
{
uint8_t x[4], h, l;
etc
A straightforward first implementation for displayInt() that supports negative numbers
// -999..9999
void HT16K33::displayInt(int n)
{
uint8_t x[4], h, l;
bool neg = (n < 0);
if (neg) n = -n;
h = n / 100;
l = n - h * 100;
x[0] = h / 10;
x[1] = h - x[0] * 10;
x[2] = l / 10;
x[3] = l - x[2] * 10;
if (neg) x[0] = HT16K33_MINUS;
display(x);
}
would display the minus sign always in first position and it would give leading zero's for number under the 100.
and it would fail to obey the _leadingZeroPlaces flag
back to drawing board.
@bepitama can you verify this displayInt() works for you?
(updated code as leading zeros was still not ok)
// -999..9999
// DIV10 & DIV100 optimize?
void HT16K33::displayInt(int n)
{
uint8_t x[4], h, l;
bool neg = (n < 0);
if (neg) n = -n;
h = n / 100;
l = n - h * 100;
x[0] = h / 10;
x[1] = h - x[0] * 10;
x[2] = l / 10;
x[3] = l - x[2] * 10;
if (neg)
{
if (_leadingZeroPlaces > 0)
{
int i = 0; // N == 3 DIGITS
if (n < 100) i = 1; // N == 2 DIGITS
if (n < 10) i = 2; // N == 1 DIGIT
x[i] = HT16K33_MINUS;
while(--i >= 0) x[i] = HT16K33_SPACE;
}
else
{
x[0] = HT16K33_MINUS;
}
}
display(x);
}
update - tested it here (not extensively) and it looks good (for now) - see develop branch
I corrected a few bugs and the result is this:// -999..9999void HT16K33::displayInt(int n){ uint8_t x[4], h, l; bool neg = (n < 0); if (neg) n = -n; h = n / 100; l = n - h 100; x[0] = h / 10; x[1] = h - x[0] 10; x[2] = l / 10; x[3] = l - x[2] * 10; Serial.println(_leadingZeroPlaces); if (neg && (_leadingZeroPlaces > 0)) { x[0] = HT16K33_MINUS; } else if (neg) // no leading 0's { int i = 0; // N == 3 DIGITS if (n < 100) i = 1; // N == 2 DIGITSif (n < 10) i = 2; // N == 1 DIGIT do { x[i] = HT16K33_SPACE; } while (--i >= 0); x[i] = HT16K33_MINUS; } display(x);}I don't understand how the _leadingZeroPlaces variable works. I tried to print it on the series trying to display the numbers 9, 99, 999, 9999, -9, -99, -999 and always 3. Giuseppe
----Messaggio originale----
Da: notifications@github.com
Data: 9-ott-2020 14.38
A: "RobTillaart/HT16K33"HT16K33@noreply.github.com
Cc: "Giuseppe Tamanini"bepitama@alice.it, "Mention"mention@noreply.github.com
Ogg: Re: [RobTillaart/HT16K33] display negative numbers (#4)
@bepitama can you verify this displayInt() works for you?
// -999..9999voidHT16K33::displayInt(int n) { uint8_t x[4], h, l; bool neg = (n < 0); if (neg) n = -n; h = n / 100; l = n - h 100; x[0] = h / 10; x[1] = h - x[0] 10; x[2] = l / 10; x[3] = l - x[2] * 10;
if (neg && (_leadingZeroPlaces > 0)) x[0] = HT16K33_MINUS; elseif (neg) // no leading 0's { int i = 0; // N == 3 DIGITSif (n < 100) i = 1; // N == 2 DIGITSif (n < 10) i = 2; // N == 1 DIGIT x[i] = HT16K33_MINUS; while(--i >= 0) x[i] = HT16K33_SPACE; } display(x); }
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
Hard to read,
The _leadingZeroPlaces is used to print 0042 in stead of 42 or 03 instead of ___3 (think underscore as space) The name is badly chosen and I made an issue #5 for it It indicates the max number of leading zero's that are to be replaced by spaces.
The default value of _leadingZeroPlaces == 3, so the display will at least display 1 digit in case the value == 0.
The idea is to fill the array with the digits and then replace the leading zero's with a space. As the last 0 may not be replaced the maximum number of zero's to be replaced is 3.
It is needed if you couple two displays side by side to print e.g. 110042, one display will display 11 the other 0042 Another usage is when displaying HEX numbers and one wants the leading zero's 0F or 00FF displayed
For one display you do not need to do anything with it normally.
A look at your proposed code and I think it fails for 3 digit negative numbers.
...
else if (neg) // no leading 0's
{
int i = 0; // N == 3 DIGITS
if (n < 100) i = 1; // N == 2 DIGITS
if (n < 10) i = 2; // N == 1 DIGIT
do
{
x[i] = HT16K33_SPACE;
} while (--i >= 0);
x[i] = HT16K33_MINUS;
}
display(x);
i becomes 0 the two if statements fail as n > 100 (as we inverted n) the do while loop is executed once so x[0] becomes a SPACE i becomes -1 then you set x[i] to minus ==> out of array boundaries.
Question is where do you want to display the minus sign (e.g. -9)
Will try to do more elaborate tests this weekend.
tests show displayInt() does show integers from -999 to 9999 now correctly. merged develop branch
It works perfectly. I'm so sorry. I got the code from the email (where it unformatted) instead of the Github Issue page. You can publish it. Thank you
Giuseppe
No problem, already published. Next job will be to implement setDigits() as a better alternative for suppress leading zeros()
I am making a scale that also displays negative numbers. I added the displayNegInt command to the library. Sure you could write in a more optimized way but it works. You decide whether or not to add it to the library. Thank you Giuseppe Tamanini from Italy
// -000..-999 void HT16K33::displayNegInt(int n) { uint8_t x[4], h, l; n = abs(n); h = n / 100; l = n - h 100; if (n > 99) { x[0] = HT16K33_MINUS; x[1] = h; x[2] = l / 10; x[3] = n - h 100 - x[2] 10; } else if (n > 9) { x[0] = HT16K33_SPACE; x[1] = HT16K33_MINUS; x[2] = l / 10; x[3] = n - x[2] 10; } else { x[0] = HT16K33_SPACE; x[1] = HT16K33_SPACE; x[2] = HT16K33_MINUS; x[3] = n; }
display(x); }