Skip to content

Commit

Permalink
Fixes for leading zeros for values <0.1 (#8)
Browse files Browse the repository at this point in the history
* Changed the snprintf from "%6li" to "%06li" to print leading zeros

* added horrible fix for the trailing zeros issue

* added debug print, fixed leading zeros error, changed defaults handling

* Preparing v2.0.3 with fixes for trailing zeros
  • Loading branch information
valerionew committed Jun 11, 2020
1 parent b811bea commit 437a86b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 99 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=HT1621
version=2.0.2
version=2.0.3
author=valerio\new (5N44P), anxzhu
maintainer=valerio\new (5N44P)
sentence=A library for HT1621 based 7-seg LCDs.
Expand Down
124 changes: 30 additions & 94 deletions src/HT1621.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
Copyright 2016-2018 anxzhu (github.com/anxzhu)
Copyright 2018 Valerio Nappi (github.com/5N44P) (changes)
Copyright 2018-2020 Valerio Nappi (github.com/5N44P) (changes)
Based on segment-lcd-with-ht1621 from anxzhu (2016-2018)
(https://github.com/anxzhu/segment-lcd-with-ht1621)
Expand Down Expand Up @@ -179,74 +179,6 @@ void HT1621::clear(){

}

// old code for reference

/*void HT1621::dispnum(float num){ //传入显示的数据,最高位为小数点和电量显示,显示数据为0.001-99999.9
//
floatToString(_buffer,num,4);
char temp;
//unsigned char lednum[10]={0x7D,0x60,0x3E,0x7A,0x63,0x5B,0x5F,0x70,0x7F,0x7B};//显示 0 1 2 3 4 5 6 7 8 9
unsigned int i;
for(i=0;i<7;i++){
if(temp=='0'){
_buffer[i]|=0x7D;
}
else if (temp=='1'){
_buffer[i]|=0x60;
}
else if (temp=='2'){
_buffer[i]|=0x3e;
}
else if (temp=='3'){
_buffer[i]|=0x7a;
}
else if (temp=='4'){
_buffer[i]|=0x63;
}
else if (temp=='5'){
_buffer[i]|=0x5b;
}
else if( temp=='6'){
_buffer[i]|=0x5f;
}
else if (temp=='7'){
_buffer[i]|=0x70;
}
else if (temp=='8'){
_buffer[i]|=0x7f;
}
else if (temp=='9'){
_buffer[i]|=0x7b;
}
else if (temp=='.'){
_buffer[i]|=0xff;
}
else if (temp=='-'){
_buffer[i]|=0x02;
}
}
int dpposition;
//find the position of the decimal point (0xff) in the buffer
dpposition = strchr(_buffer, 0xff)-_buffer;
_buffer[dpposition] = 0x80 | _buffer[dpposition+1];
for(int i=BUFFERSIZE; i<dpposition-1; i--){
_buffer[i] = _buffer[i+1];
}
update();
} */

void HT1621::update(){ // takes the buffer and puts it straight into the driver
// the buffer is backwards with respect to the lcd. could be improved
wrone(0, _buffer[5]);
Expand All @@ -258,14 +190,29 @@ void HT1621::update(){ // takes the buffer and puts it straight into the driver

}

void HT1621::print(long num){

void HT1621::print(long num, char*flags,int precision){
if(num > 999999) // basic checks
num = 999999; // clip into 999999
if(num < -99999) // basic checks
num = -99999; // clip into -99999

char localbuffer[7]; //buffer to work with in the function
snprintf(localbuffer,7, "%6li", num); // convert the decimal into string
char localbuffer[7]; //buffer to work within the function
snprintf(localbuffer,7, flags, num); // convert the decimal into string

// horrible handling but should get us working. needs refactor in next major
if(precision > 0 && num < pow(10, precision)) {
for(int i = 0; i<(5-precision); i++){
localbuffer[i] = ' ';
}
}

#ifdef HTDEBUG
Serial.print("\n\n");
for(int jdbg = 0; jdbg < 6; jdbg++){
Serial.print(localbuffer[jdbg]);
}
#endif

for(int i=0; i<6; i++){
_buffer[i] &= 0x80; // mask the first bit, used by batter and decimal point
Expand Down Expand Up @@ -303,20 +250,17 @@ void HT1621::print(long num){
case '-':
_buffer[i] |= 0x02;
break;
default: // do nothing, blank digit!
break;
}
}

update();

}

void HT1621::print(float num){
// could be smarter and actually check how many
// non zeros we have in the decimals
print(num, 3);
}

void HT1621::print(float num, int precision){
void HT1621::print(double num, int precision){
if(num > 999999) // basic checks
num = 999999; // clip into 999999
if(num < -99999) // basic checks
Expand All @@ -328,12 +272,19 @@ void HT1621::print(float num, int precision){
if(precision < 0)
precision = 0; // negative precision?!

char * flags = "%6li";

if(precision > 0 && num < 1){
flags = "%06li";
}

long ingegerpart;
ingegerpart = ((long)(num*pow(10,precision)));

print(ingegerpart); // draw the integerized number
print(ingegerpart,flags,precision); // draw the integerized number
setdecimalseparator(precision); // draw the decimal point


update();
}

Expand All @@ -352,19 +303,4 @@ void HT1621::setdecimalseparator(int decimaldigits) {
// the last three are for the decimal point
_buffer[6-decimaldigits] |= 0x80;

// refactoring, old code for reference
/*switch(decimaldigits){
case 1:
_buffer[5] |= 0x80;
break;
case 2:
_buffer[4] |= 0x80;
break;
case 3:
_buffer[3] |= 0x80;
break;
case 0:
default:
break;
}*/
}
10 changes: 6 additions & 4 deletions src/HT1621.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
Copyright 2016-2018 anxzhu (github.com/anxzhu)
Copyright 2018 Valerio Nappi (github.com/5N44P) (changes)
Copyright 2018-2020 Valerio Nappi (github.com/5N44P) (changes)
Based on segment-lcd-with-ht1621 from anxzhu (2016-2018)
(https://github.com/anxzhu/segment-lcd-with-ht1621)
Expand Down Expand Up @@ -44,6 +44,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define WDTDIS1 0X0A //0b1000 0000 1010 禁止看门狗
#define BUFFERSIZE 12

// #define HTDEBUG


class HT1621
{
public:
Expand All @@ -54,9 +57,8 @@ class HT1621
void backlight();
void noBacklight();
void setBatteryLevel(int level);
void print(long num);
void print(float num);
void print(float num, int precision);
void print(long num, char*flags="%6li", int precision = 0);
void print(double num, int precision = 3);
void display();
void noDisplay();
private:
Expand Down

0 comments on commit 437a86b

Please sign in to comment.