Skip to content

Commit 476722c

Browse files
committed
Updated to scale the energy values
The Session and Lifetime values will now automatically scale as the number increases from Wh->kWh->mWh and so on. Also right justify the the values to look a bit nearer . Fixes Display session energy as kW with one decimal place #264
1 parent 80f66b4 commit 476722c

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

src/lcd.cpp

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,16 @@ void LcdTask::displayInfoLine(LcdInfoLine line, unsigned long &nextUpdate)
498498
{
499499
case LcdInfoLine::EnergySession:
500500
// Energy 1,018Wh
501-
displayNumberValue(1, "Energy", _evse->getSessionEnergy(), 2, "Wh");
501+
displayScaledNumberValue(1, "Energy", _evse->getSessionEnergy(), 1, "Wh");
502502
_updateInfoLine = false;
503503
break;
504504

505-
case LcdInfoLine::EnergyTotal:
505+
case LcdInfoLine::EnergyTotal: {
506506
// Lifetime 2313kWh
507-
displayNumberValue(1, "Lifetime", _evse->getTotalEnergy(), 0, "kWh");
507+
double totalEnergy = _evse->getTotalEnergy() * 1000;
508+
displayScaledNumberValue(1, "Lifetime", totalEnergy, 0, "Wh");
508509
_updateInfoLine = false;
509-
break;
510+
} break;
510511

511512
case LcdInfoLine::Temperature:
512513
// EVSE Temp 30.5C
@@ -534,8 +535,8 @@ void LcdTask::displayInfoLine(LcdInfoLine line, unsigned long &nextUpdate)
534535
gettimeofday(&local_time, NULL);
535536
struct tm timeinfo;
536537
localtime_r(&local_time.tv_sec, &timeinfo);
537-
strftime(temp, sizeof(temp), "Date %d/%m/%Y", &timeinfo);
538-
showText(0, 1, temp, true);
538+
strftime(temp, sizeof(temp), "%d/%m/%Y", &timeinfo);
539+
displayNameValue(1, "Date", temp);
539540
_updateInfoLine = false;
540541
} break;
541542

@@ -593,7 +594,7 @@ void LcdTask::displayInfoLine(LcdInfoLine line, unsigned long &nextUpdate)
593594
displayStopWatchTime("Left", delay);
594595
nextUpdate = 1000;
595596
} else {
596-
showText(0, 1, "Left --:--:--", true);
597+
displayNameValue(1, "Left", "--:--:--");
597598
_updateInfoLine = false;
598599
}
599600
} break;
@@ -607,11 +608,36 @@ void LcdTask::displayInfoLine(LcdInfoLine line, unsigned long &nextUpdate)
607608
}
608609
}
609610

611+
void LcdTask::displayScaledNumberValue(int line, const char *name, double value, int precision, const char *unit)
612+
{
613+
static const char *mod[] = {
614+
"",
615+
"k",
616+
"m",
617+
"g",
618+
"t",
619+
"p"
620+
};
621+
622+
int index = 0;
623+
while (value > 1000 && index < ARRAY_ITEMS(mod))
624+
{
625+
value /= 1000;
626+
index++;
627+
}
628+
629+
char newUnit[20];
630+
sprintf(newUnit, "%s%s", mod[index], unit);
631+
632+
displayNumberValue(line, name, value, precision, newUnit);
633+
}
634+
610635
void LcdTask::displayNumberValue(int line, const char *name, double value, int precision, const char *unit)
611636
{
612-
char temp[20];
613-
sprintf(temp, "%s %.*f%s", name, precision, value, unit);
614-
showText(0, line, temp, true);
637+
char number[20];
638+
snprintf(number, sizeof(number), "%.*f%s", precision, value, unit);
639+
640+
displayNameValue(line, name, number);
615641
}
616642

617643
void LcdTask::displayInfoEventTime(const char *name, Scheduler::EventInstance &event)
@@ -632,7 +658,7 @@ void LcdTask::displayInfoEventTime(const char *name, Scheduler::EventInstance &e
632658
} else {
633659
sprintf(temp, "%s --:--", name);
634660
}
635-
showText(0, 1, temp, true);
661+
displayNameValue(1, name, temp);
636662
}
637663

638664
void LcdTask::displayStopWatchTime(const char *name, uint32_t time)
@@ -641,8 +667,28 @@ void LcdTask::displayStopWatchTime(const char *name, uint32_t time)
641667
int hour = time / 3600;
642668
int min = (time / 60) % 60;
643669
int sec = time % 60;
644-
sprintf(temp, "%s %d:%02d:%02d", name, hour, min, sec);
645-
showText(0, 1, temp, true);
670+
sprintf(temp, "%d:%02d:%02d", hour, min, sec);
671+
displayNameValue(1, name, temp);
672+
}
673+
674+
void LcdTask::displayNameValue(int line, const char *name, const char *value)
675+
{
676+
int nameLen = strlen(name);
677+
int valueLen = strlen(value) + 1;
678+
if(nameLen + valueLen > LCD_MAX_LEN) {
679+
nameLen = LCD_MAX_LEN - valueLen;
680+
} else {
681+
valueLen = LCD_MAX_LEN - nameLen;
682+
}
683+
684+
DBUGVAR(nameLen);
685+
DBUGVAR(name);
686+
DBUGVAR(valueLen);
687+
DBUGVAR(value);
688+
689+
char temp[20];
690+
snprintf(temp, sizeof(temp), "%.*s%*s", nameLen, name, valueLen, value);
691+
showText(0, line, temp, true);
646692
}
647693

648694
void LcdTask::showText(int x, int y, const char *msg, bool clear)

src/lcd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ class LcdTask : public MicroTasks::Task
133133
void displayStateLine(uint8_t EvseState, unsigned long &nextUpdate);
134134
void displayInfoLine(LcdInfoLine info, unsigned long &nextUpdate);
135135
void displayNumberValue(int line, const char *name, double value, int precision, const char *unit);
136+
void displayScaledNumberValue(int line, const char *name, double value, int precision, const char *unit);
136137
void displayInfoEventTime(const char *name, Scheduler::EventInstance &event);
138+
void displayNameValue(int line, const char *name, const char *value);
137139
void displayStopWatchTime(const char *name, uint32_t time);
138140
protected:
139141
void setup();

0 commit comments

Comments
 (0)