move number display into function

This commit is contained in:
interfisch 2021-03-05 20:00:16 +01:00
parent 925e82d628
commit 81821be685
1 changed files with 72 additions and 69 deletions

View File

@ -77,6 +77,7 @@ float getWeightSpread();
void sendWeight(float w);
bool cmdHandler(const HomieRange& range, const String& value);
void powerOff();
void displayNumber(float numberdisplay);
void setup() {
@ -207,75 +208,7 @@ void loopHandler() {
if (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL) {
last_displayupdate=loopmillis;
uint8_t displayresolution=3; //how many digits after dot
float numberdisplay=weight_current;
bool _negative=false;
if (numberdisplay<0) {
numberdisplay*=-1;
_negative=true;
}
if ((numberdisplay>999.9) | (displayresolution==0)) {
display.showNumberDec((int)(numberdisplay+0.5), false); //just diplay number
}else{
uint8_t d1=0;
uint8_t d2=0;
uint8_t d3=0;
uint8_t d4=0;
if(numberdisplay<10 && displayresolution>=3) { // 5.241, 0.005 etc.
int _number=(int)(numberdisplay*1000+0.5); //in 1000th kg rounded
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[1] = display.encodeDigit(d3);
display_data[0] = display.encodeDigit(d4); //leftmost digit
display_data[0] |= SEG_DP; //add decimal point after left most digit
}else if(numberdisplay<100 && displayresolution>=2) { //10.24, 99.20
int _number=(int)(numberdisplay*100+0.5); //in 100th kg rounded
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[1] = display.encodeDigit(d3);
display_data[1] |= SEG_DP; //add decimal point after second digit from the left
display_data[0] = display.encodeDigit(d4); //leftmost digit
if (d4==0) { //number smaller than 1000
display_data[0]={0}; //turn off left segment
}
}else if (numberdisplay<1000 && displayresolution>=1) //100.0, 999.9
{
int _number=(int)(numberdisplay*10+0.5); //in 10th kg rounded
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[2] |= SEG_DP; //add decimal point after second digit from the right
display_data[1] = display.encodeDigit(d3);
display_data[0] = display.encodeDigit(d4); //leftmost digit
if (d4==0) { //number smaller than 1000
display_data[0]={0}; //turn off left segment
if (d3==0) { //number smaller than 100
display_data[1]={0}; //turn off 2nd from left segment
}
}
}
if (_negative) { //show negative number by using rightmost dot
display_data[3] |= SEG_DP;
}
Serial.println();
display.setSegments(display_data);
}
displayNumber(weight_current);
}
/*
@ -388,4 +321,74 @@ void powerOff() {
Serial.flush();
delay(100);
digitalWrite(PIN_SELFENABLE, LOW);
}
void displayNumber(float numberdisplay) {
uint8_t displayresolution=3; //how many digits after dot
bool _negative=false;
if (numberdisplay<0) {
numberdisplay*=-1;
_negative=true;
}
if ((numberdisplay>999.9) | (displayresolution==0)) {
display.showNumberDec((int)(numberdisplay+0.5), false); //just diplay number
}else{
uint8_t d1=0;
uint8_t d2=0;
uint8_t d3=0;
uint8_t d4=0;
if(numberdisplay<10 && displayresolution>=3) { // 5.241, 0.005 etc.
int _number=(int)(numberdisplay*1000+0.5); //in 1000th kg rounded
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[1] = display.encodeDigit(d3);
display_data[0] = display.encodeDigit(d4); //leftmost digit
display_data[0] |= SEG_DP; //add decimal point after left most digit
}else if(numberdisplay<100 && displayresolution>=2) { //10.24, 99.20
int _number=(int)(numberdisplay*100+0.5); //in 100th kg rounded
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[1] = display.encodeDigit(d3);
display_data[1] |= SEG_DP; //add decimal point after second digit from the left
display_data[0] = display.encodeDigit(d4); //leftmost digit
if (d4==0) { //number smaller than 1000
display_data[0]={0}; //turn off left segment
}
}else if (numberdisplay<1000 && displayresolution>=1) //100.0, 999.9
{
int _number=(int)(numberdisplay*10+0.5); //in 10th kg rounded
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[2] |= SEG_DP; //add decimal point after second digit from the right
display_data[1] = display.encodeDigit(d3);
display_data[0] = display.encodeDigit(d4); //leftmost digit
if (d4==0) { //number smaller than 1000
display_data[0]={0}; //turn off left segment
if (d3==0) { //number smaller than 100
display_data[1]={0}; //turn off 2nd from left segment
}
}
}
if (_negative) { //show negative number by using rightmost dot
display_data[3] |= SEG_DP;
}
Serial.println();
display.setSegments(display_data);
}
}