From 437a86bf6ffbaa84116b786f0764aa04cd9bd5cd Mon Sep 17 00:00:00 2001 From: "valerio\\new" Date: Thu, 11 Jun 2020 11:56:49 +0200 Subject: [PATCH] Fixes for leading zeros for values <0.1 (#8) * 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 --- library.properties | 2 +- src/HT1621.cpp | 124 +++++++++++---------------------------------- src/HT1621.h | 10 ++-- 3 files changed, 37 insertions(+), 99 deletions(-) diff --git a/library.properties b/library.properties index 8f6a3b2..201d3b3 100644 --- a/library.properties +++ b/library.properties @@ -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. diff --git a/src/HT1621.cpp b/src/HT1621.cpp index a8eaa8d..38aa3c9 100644 --- a/src/HT1621.cpp +++ b/src/HT1621.cpp @@ -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) @@ -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 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 @@ -303,6 +250,8 @@ void HT1621::print(long num){ case '-': _buffer[i] |= 0x02; break; + default: // do nothing, blank digit! + break; } } @@ -310,13 +259,8 @@ void HT1621::print(long num){ } -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 @@ -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(); } @@ -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; - }*/ } diff --git a/src/HT1621.h b/src/HT1621.h index 807b24c..76936be 100644 --- a/src/HT1621.h +++ b/src/HT1621.h @@ -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) @@ -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: @@ -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: