1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-15 06:53:07 +01:00

Add moving average for battery values in OBPSensorTask

This commit is contained in:
norbert-walter
2022-03-25 18:19:14 +01:00
parent 05f08e1699
commit 7958810e7d
7 changed files with 284 additions and 34 deletions

View File

@@ -6,6 +6,7 @@
class PageBattery : public Page
{
bool keylock = false; // Keylock
int average = 0; // Average type [0...3], 0=off, 1=10s, 2=60s, 3=300s
public:
PageBattery(CommonData &comon){
@@ -13,7 +14,15 @@ class PageBattery : public Page
}
virtual int handleKey(int key){
if(key == 11){ // Code for keylock
// Change average
if(key == 1){
average ++;
average = average % 4; // Modulo 4
return 0; // Commit the key
}
// Code for keylock
if(key == 11){
keylock = !keylock; // Toggle keylock
return 0; // Commit the key
}
@@ -23,7 +32,7 @@ class PageBattery : public Page
virtual void displayPage(CommonData &commonData, PageData &pageData){
GwConfigHandler *config = commonData.config;
GwLog *logger=commonData.logger;
// Old values for hold function
double value1 = 0;
static String svalue1old = "";
@@ -47,13 +56,27 @@ class PageBattery : public Page
// Get voltage value
String name1 = "BatVolt"; // Value name
if(String(powsensor1) == "INA219" || String(powsensor1) == "INA226"){
value1 = commonData.data.batteryVoltage; // Value as double in SI unit
// Switch average values
switch (average) {
case 0:
value1 = commonData.data.batteryVoltage; // Real data
break;
case 1:
value1 = commonData.data.batteryVoltage10; // Average 10s
break;
case 2:
value1 = commonData.data.batteryVoltage60; // Average 60s
break;
case 3:
value1 = commonData.data.batteryVoltage300; // Average 300s
break;
default:
value1 = commonData.data.batteryVoltage; // Default
break;
}
}
else{
if(simulation == false){
value1 = 0; // No sensor data
}
else{
if(simulation == true){
value1 = 12 + float(random(0, 5)) / 10; // Simulation data
}
}
@@ -63,13 +86,26 @@ class PageBattery : public Page
// Get current value
String name2 = "BatCurr"; // Value name
if(String(powsensor1) == "INA219" || String(powsensor1) == "INA226"){
value2 = commonData.data.batteryCurrent; // Value as double in SI unit
switch (average) {
case 0:
value2 = commonData.data.batteryCurrent; // Real data
break;
case 1:
value2 = commonData.data.batteryCurrent10; // Average 10s
break;
case 2:
value2 = commonData.data.batteryCurrent60; // Average 60s
break;
case 3:
value2 = commonData.data.batteryCurrent300; // Average 300s
break;
default:
value2 = commonData.data.batteryCurrent; // Default
break;
}
}
else{
if(simulation == false){
value2 = 0; // No sensor data
}
else{
if(simulation == true){
value2 = 8 + float(random(0, 10)) / 10; // Simulation data
}
}
@@ -79,13 +115,26 @@ class PageBattery : public Page
// Get power value
String name3 = "BatPow"; // Value name
if(String(powsensor1) == "INA219" || String(powsensor1) == "INA226"){
value3 = commonData.data.batteryPower; // Value as double in SI unit
switch (average) {
case 0:
value3 = commonData.data.batteryPower; // Real data
break;
case 1:
value3 = commonData.data.batteryPower10; // Average 10s
break;
case 2:
value3 = commonData.data.batteryPower60; // Average 60s
break;
case 3:
value3 = commonData.data.batteryPower300; // Average 300s
break;
default:
value3 = commonData.data.batteryPower; // Default
break;
}
}
else{
if(simulation == false){
value3 = 0; // No sensor data
}
else{
if(simulation == true){
value3 = value1 * value2; // Simulation data
}
}
@@ -99,7 +148,7 @@ class PageBattery : public Page
}
// Logging boat values
LOG_DEBUG(GwLog::LOG,"Drawing at PageBattery, %s: %f, %s: %f, %s: %f", name1, value1, name2, value2, name3, value3);
LOG_DEBUG(GwLog::LOG,"Drawing at PageBattery, %s: %f, %s: %f, %s: %f, Avg: %d", name1, value1, name2, value2, name3, value3, average);
// Draw page
//***********************************************************
@@ -120,6 +169,52 @@ class PageBattery : public Page
}
// Clear display by call in obp60task.cpp in main loop
// Show average settings
display.setTextColor(textcolor);
display.setFont(&Ubuntu_Bold8pt7b);
switch (average) {
case 0:
display.setCursor(60, 90);
display.print("Avg: 1s");
display.setCursor(60, 180);
display.print("Avg: 1s");
display.setCursor(60, 270);
display.print("Avg: 1s");
break;
case 1:
display.setCursor(60, 90);
display.print("Avg: 10s");
display.setCursor(60, 180);
display.print("Avg: 10s");
display.setCursor(60, 270);
display.print("Avg: 10s");
break;
case 2:
display.setCursor(60, 90);
display.print("Avg: 60s");
display.setCursor(60, 180);
display.print("Avg: 60s");
display.setCursor(60, 270);
display.print("Avg: 60s");
break;
case 3:
display.setCursor(60, 90);
display.print("Avg: 300s");
display.setCursor(60, 180);
display.print("Avg: 300s");
display.setCursor(60, 270);
display.print("Avg: 300s");
break;
default:
display.setCursor(60, 90);
display.print("Avg: 1s");
display.setCursor(60, 180);
display.print("Avg: 1s");
display.setCursor(60, 270);
display.print("Avg: 1s");
break;
}
// ############### Value 1 ################
// Show name
@@ -134,13 +229,17 @@ class PageBattery : public Page
display.setCursor(20, 90);
display.print(unit1); // Unit
// Show value
display.setFont(&DSEG7Classic_BoldItalic30pt7b);
display.setCursor(180, 90);
// Show bus data
display.print(value1,2); // Real value as formated string
if(String(powsensor1) != "off"){
display.print(value1,2); // Real value as formated string
}
else{
display.print("---"); // No sensor data (sensor is off)
}
// ############### Horizontal Line ################
@@ -166,7 +265,12 @@ class PageBattery : public Page
display.setCursor(180, 180);
// Show bus data
display.print(value2,1); // Real value as formated string
if(String(powsensor1) != "off"){
display.print(value2,1); // Real value as formated string
}
else{
display.print("---"); // No sensor data (sensor is off)
}
// ############### Horizontal Line ################
@@ -192,7 +296,12 @@ class PageBattery : public Page
display.setCursor(180, 270);
// Show bus data
display.print(value3,1); // Real value as formated string
if(String(powsensor1) != "off"){
display.print(value3,1); // Real value as formated string
}
else{
display.print("---"); // No sensor data (sensor is off)
}
// ############### Key Layout ################
@@ -200,8 +309,10 @@ class PageBattery : public Page
// Key Layout
display.setTextColor(textcolor);
display.setFont(&Ubuntu_Bold8pt7b);
display.setCursor(130, 290);
if(keylock == false){
display.setCursor(10, 290);
display.print("[AVG]");
display.setCursor(130, 290);
display.print("[ <<<< " + String(commonData.data.actpage) + "/" + String(commonData.data.maxpage) + " >>>> ]");
if(String(backlightMode) == "Control by Key"){ // Key for illumination
display.setCursor(343, 290);
@@ -209,6 +320,7 @@ class PageBattery : public Page
}
}
else{
display.setCursor(130, 290);
display.print(" [ Keylock active ]");
}