Skip to content

Commit

Permalink
Add thresholds to update display, BLE, MQTT and ESPNow (WIP) #212
Browse files Browse the repository at this point in the history
  • Loading branch information
melkati committed May 5, 2024
1 parent bbea926 commit 3e1be56
Show file tree
Hide file tree
Showing 10 changed files with 846 additions and 44 deletions.
4 changes: 3 additions & 1 deletion CO2_Gadget_EINK.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ void testRedrawValues(bool randomNumbers = false) {
#ifdef EINKBOARDGDEM0213B74
void displayShowValues(bool forceRedraw = false) {
static uint32_t lastDisplayUpdate = 0;
if (!passMeasurementThresholds(DISPLAY_SHOW)) return;
if (isDownloadingBLE) return; // Do not update display while downloading BLE data to MyAmbiance
// Return if last update less than 15 seconds ago
if (!forceRedraw && (millis() - lastDisplayUpdate < 10000)) {
Expand Down Expand Up @@ -697,7 +698,8 @@ void displayShowValues(bool forceRedraw = false) {
#else // ALL OTHER NOT EINKBOARDGDEM0213B74 BOARDS

void displayShowValues(bool forceRedraw = false) {
static uint32_t lastDisplayUpdate = 0;
static uint32_t lastDisplayUpdate = 0;
if (!passMeasurementThresholds(DISPLAY_SHOW)) return;
if (isDownloadingBLE) return; // Do not update display while downloading BLE data to MyAmbiance
// Return if last update less than 15 seconds ago
if (!forceRedraw && (millis() - lastDisplayUpdate < 15000)) {
Expand Down
80 changes: 43 additions & 37 deletions CO2_Gadget_OLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// clang-format on
// #include <U8x8lib.h>
#include <U8g2lib.h>

#include "bootlogo.h"
#include "icons.h"
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE); // Frame Buffer: clearBuffer/sendBuffer. More RAM usage, Faster
Expand All @@ -25,25 +26,29 @@ int displayHeight = 64;
#define MENUFONT u8g2_font_5x8_mf

void setDisplayBrightness(uint32_t newBrightness) {
Serial.printf("-->[OLED] Setting display brightness value at %d\n", newBrightness);
u8g2.setContrast(newBrightness);
actualDisplayBrightness = newBrightness;
Serial.printf("-->[OLED] Setting display brightness value at %d\n", newBrightness);
u8g2.setContrast(newBrightness);
actualDisplayBrightness = newBrightness;
}

void displaySleep(bool value = true) {
// u8g2.setPowerSave(number); // Turn off the display
}

void turnOffDisplay() {
setDisplayBrightness(0); // Turn off the display
setDisplayBrightness(0); // Turn off the display
}

void displaySplashScreen() {
u8g2.clearDisplay();
u8g2.firstPage();
do {
// u8g2.drawXBMP(30, 0, 59, 20, eMarieteLogo);
// u8g2.drawXBM(7, 23, 46, 36, CO2Logo);
// u8g2.drawXBM(60, 32, 61, 23, GadgetLogo);
u8g2.drawXBM(0, 0, 128, 64, splash);
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
u8g2.clearDisplay();
u8g2.firstPage();
do {
// u8g2.drawXBMP(30, 0, 59, 20, eMarieteLogo);
// u8g2.drawXBM(7, 23, 46, 36, CO2Logo);
// u8g2.drawXBM(60, 32, 61, 23, GadgetLogo);
u8g2.drawXBM(0, 0, 128, 64, splash);
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
}

/***************************************************************************************
Expand All @@ -54,37 +59,38 @@ void displaySplashScreen() {
// notificationText = string to display.
// notificationTypes one of enum notificationTypes notifyNothing, notifyInfo, notifyWarning, notifyError
bool displayNotification(String notificationText, notificationTypes notificationType) {
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
}

bool displayNotification(String notificationText, String notificationText2, notificationTypes notificationType) {
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
uint16_t textColor, boxColor, backgroundColor, boxMarging = 15;
return true;
}

void initDisplay(bool fastMode = false) { // fastMode not used in OLED display. Just for compatibility with TFT and other displays.
Serial.printf("-->[OLED] Initialized: \t#%s#\n",
((u8g2.begin()) ? "OK" : "Failed"));
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB12_tr);
u8g2.drawStr(0, 15, " eMariete.com");
u8g2.drawStr(0, 33, " CO2 Gadget");
u8g2.drawStr(0, 51, " Air Quality");
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
if (displayReverse) {
u8g2.setDisplayRotation(U8G2_R2);
} else {
u8g2.setDisplayRotation(U8G2_R0);
}
setDisplayBrightness(DisplayBrightness);
displaySplashScreen();
delay(1000);
void initDisplay(bool fastMode = false) { // fastMode not used in OLED display. Just for compatibility with TFT and other displays.
Serial.printf("-->[OLED] Initialized: \t#%s#\n",
((u8g2.begin()) ? "OK" : "Failed"));
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB12_tr);
u8g2.drawStr(0, 15, " eMariete.com");
u8g2.drawStr(0, 33, " CO2 Gadget");
u8g2.drawStr(0, 51, " Air Quality");
} while (u8g2.nextPage());
u8g2.setFont(MENUFONT);
if (displayReverse) {
u8g2.setDisplayRotation(U8G2_R2);
} else {
u8g2.setDisplayRotation(U8G2_R0);
}
setDisplayBrightness(DisplayBrightness);
displaySplashScreen();
delay(1000);
}

void displayShowValues(bool forceRedraw = false) {
void displayShowValues(bool forceRedraw = false) {
if (!passMeasurementThresholds(DISPLAY_SHOW)) return;
if ((co2 == 0) || (co2 > 9999)) return;
String co2Str = String(co2);
if (co2Str.length() < 4) {
Expand Down
16 changes: 10 additions & 6 deletions CO2_Gadget_Sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct SensorConfig {
SensorConfig configs[NUM_OUTPUTS] = {
// DISPLAY_SHOW
{
.enabled = false,
.enabled = true,
.co2ThresholdAbsolute = 20,
.tempThresholdAbsolute = 0.5f,
.humThresholdAbsolute = 1.0f,
Expand Down Expand Up @@ -101,12 +101,18 @@ void updateLastMeasurementValues(OutputType outputType) {
bool checkThresholdsAndMaybeUpdate(OutputType outputType) {
const SensorConfig& config = configs[outputType];
if (!config.enabled) return true;

// Comprobación de división por cero
bool co2DivideByZero = config.previousCO2Value == 0;
bool tempDivideByZero = config.previousTemperatureValue == 0;
bool humDivideByZero = config.previousHumidityValue == 0;

if (abs(co2 - config.previousCO2Value) >= config.co2ThresholdAbsolute ||
abs(temp - config.previousTemperatureValue) >= config.tempThresholdAbsolute ||
abs(hum - config.previousHumidityValue) >= config.humThresholdAbsolute ||
(config.previousCO2Value != 0 && abs(co2 - config.previousCO2Value) >= config.previousCO2Value * config.co2ThresholdPercentage / 100) ||
(config.previousTemperatureValue != 0 && abs(temp - config.previousTemperatureValue) >= config.previousTemperatureValue * config.tempThresholdPercentage / 100) ||
(config.previousHumidityValue != 0 && abs(hum - config.previousHumidityValue) >= config.previousHumidityValue * config.humThresholdPercentage / 100)) {
(co2DivideByZero || abs(co2 - config.previousCO2Value) >= config.previousCO2Value * config.co2ThresholdPercentage / 100) ||
(tempDivideByZero || abs(temp - config.previousTemperatureValue) >= config.previousTemperatureValue * config.tempThresholdPercentage / 100) ||
(humDivideByZero || abs(hum - config.previousHumidityValue) >= config.previousHumidityValue * config.humThresholdPercentage / 100)) {
updateLastMeasurementValues(outputType);
return true;
}
Expand Down Expand Up @@ -154,8 +160,6 @@ void printSensorsDetected() {

void onSensorDataOk() {
previousCO2Value = co2;
previousTemperatureValue = temp;
previousHumidityValue = hum;
co2 = sensors.getCO2();
hum = sensors.getHumidity();
if (hum == 0.0) hum = sensors.getCO2humi();
Expand Down
1 change: 1 addition & 0 deletions CO2_Gadget_TFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ void showCO2units(int32_t posX, int32_t posY, bool forceRedraw) {
}

void displayShowValues(bool forceRedraw = false) {
if (!passMeasurementThresholds(DISPLAY_SHOW)) return;
uint8_t currentDatum = tft.getTextDatum();
tft.unloadFont();
if (forceRedraw) {
Expand Down
7 changes: 7 additions & 0 deletions CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,13 @@ void initWebServer() {
request->send(response);
});

server.on("/low_power.html", HTTP_GET, [](AsyncWebServerRequest *request) {
/** GZIPPED CONTENT ***/
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/low_power.gz", "text/html");
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});

server.on("/main.js", HTTP_GET, [](AsyncWebServerRequest *request) {
/** GZIPPED CONTENT ***/
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/main.js.gz", "application/javascript");
Expand Down
1 change: 1 addition & 0 deletions data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<a href="index.html" class="active">Home</a>
<a href="preferences.html">Preferences</a>
<a href="status.html">Status</a>
<a href="low_power.html">Thresholds</a>
</div>
<div class="iconDarkLight" id="iconBulb">
<!-- <input type="checkbox"> -->
Expand Down
Loading

0 comments on commit 3e1be56

Please sign in to comment.