Skip to content

Commit

Permalink
Merge pull request #203 from melkati/development
Browse files Browse the repository at this point in the history
Sync Development branch for new release
  • Loading branch information
melkati committed Apr 10, 2024
2 parents b9b50c3 + 0b58fe4 commit b953a3a
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release3_beta.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Beta Release V2.93

# # # # # # # # # # #
# To create new Beta version and upload so web server, just commit with a message with the format "Beta v*.*.*" (Beta v1.0.1, Beta v1.1.12, Beta v2.1.123, etc)
# To create new Beta version and upload to the web server, just commit with a message with the format "Beta v*.*.*" (Beta v1.0.1, Beta v1.1.12, Beta v2.1.123, etc)
# # # # # # # # # # #

on:
Expand Down
17 changes: 11 additions & 6 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ bool displayShowPM25 = true;
bool debugSensors = false;
bool inMenu = false;
bool shouldWakeUpDisplay = false;
bool shouldRedrawDisplay = false;
uint16_t measurementInterval = 10;
bool bleInitialized = false;
int8_t selectedCO2Sensor = -1;
Expand All @@ -103,7 +104,7 @@ uint16_t batteryFullyChargedMillivolts = 4200; // Voltage of battery when it is

// Variables to control automatic display off to save power
bool workingOnExternalPower = true; // True if working on external power (USB connected)
uint32_t actualDisplayBrightness = 100; // To know if it's on or off
uint32_t actualDisplayBrightness = 0; // To know if it's on or off
bool displayOffOnExternalPower = false;
uint16_t timeToDisplayOff = 0; // Time in seconds to turn off the display to save power.
volatile uint64_t lastTimeButtonPressed = 0; // Last time stamp button up was pressed
Expand Down Expand Up @@ -294,7 +295,8 @@ void wakeUpDisplay() {
if (actualDisplayBrightness == 0) {
#if defined(SUPPORT_OLED) || defined(SUPPORT_TFT)
setDisplayBrightness(DisplayBrightness);
publishMQTTLogData("Display woken up. Setting display brightness to " + String(DisplayBrightness));
// publishMQTTLogData("Display woken up. Setting display brightness to " + String(DisplayBrightness));
// Serial.println("-->[MAIN] Display woken up. Setting display brightness to " + String(DisplayBrightness));
#endif
lastTimeButtonPressed = millis();
}
Expand Down Expand Up @@ -458,7 +460,9 @@ void adjustBrightnessLoop() {
if ((workingOnExternalPower) && (!displayOffOnExternalPower)) {
if (actualDisplayBrightness == 0) {
setDisplayBrightness(DisplayBrightness); // Exception: When USB connected (just connected) & TFT is OFF -> Turn Display ON
publishMQTTLogData("Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// publishMQTTLogData("Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// Serial.println("-->[MAIN] Turning on display on external power. Actual brightness: " + String(actualDisplayBrightness));
// delay(10);
}
return;
}
Expand All @@ -469,12 +473,13 @@ void adjustBrightnessLoop() {
if ((workingOnExternalPower) && (displayOffOnExternalPower)) {
Serial.println("-->[MAIN] Turning off display on external power to save power. Actual brightness: " + String(actualDisplayBrightness));
turnOffDisplay();
publishMQTTLogData("[MAIN] Turning off display on external power to save power. Actual brightness: " + String(actualDisplayBrightness));
// publishMQTTLogData("[MAIN] Turning off display on external power to save power. Actual brightness: " + String(actualDisplayBrightness));
}
if (!workingOnExternalPower) {
Serial.println("-->[MAIN] Turning off display on battery to save power. Actual brightness: " + String(actualDisplayBrightness));
// Serial.println("-->[MAIN] Turning off display on battery to save power. Actual brightness: " + String(actualDisplayBrightness));
turnOffDisplay();
publishMQTTLogData("[MAIN] Turning off display on battery to save power. Actual brightness: " + String(actualDisplayBrightness));
// publishMQTTLogData("[MAIN] Turning off display on battery to save power. Actual brightness: " + String(actualDisplayBrightness));
delay(10);
}
}
#endif
Expand Down
7 changes: 3 additions & 4 deletions CO2_Gadget_Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ void readBatteryVoltage() {
batteryLevel = battery.level(batteryVoltage * 1000);
lastTimeBatteryRead = millis();

// If battery voltage is more than 9% of the fully charged battery voltage (~4.58V) or if battery voltage is less than 1V (no battery connected to sense pin), then assume that the device is working on external power.
workingOnExternalPower = (batteryVoltageNow * 1000 > batteryFullyChargedMillivolts + (batteryFullyChargedMillivolts * 9 / 100)) || (batteryVoltageNow < 1000);
// If battery voltage is more than 6% of the fully charged battery voltage (~4.58V) or if battery voltage is less than 1V (no battery connected to sense pin), then assume that the device is working on external power.
workingOnExternalPower = (batteryVoltageNow * 1000 > batteryFullyChargedMillivolts + (batteryFullyChargedMillivolts * 6 / 100)) || (batteryVoltageNow < 1);
publishMQTTLogData("-->[TFT ] Battery Level: " + String(batteryLevel) + "% Battery voltage: " + String(batteryVoltageNow) + "V External power: " + String(workingOnExternalPower));

// publishMQTTLogData("Battery Level: " + String(batteryLevel) + "% Battery voltage changed from: " + String(lastBatteryVoltage) + "V to " + String(batteryVoltage) + "V");
}
Expand All @@ -66,9 +67,7 @@ void readBatteryVoltage() {
// }

void batteryLoop() {
float batteryVoltageNow = 0;
readBatteryVoltage();
// Serial.printf("-->[BATT] Battery Level: %d%%. Battery voltage: %.4fV\n", batteryLevel, batteryVoltageNow);
if (!inMenu) {
if (abs(lastBatteryVoltage - batteryVoltage) >= 0.1) { // If battery voltage changed by at least 0.1V, update battery level
// Serial.printf("-->[BATT] Battery Level: %d%%. Battery voltage changed from: %.4fV to %.4fV\n", batteryLevel, lastBatteryVoltage, batteryVoltage);
Expand Down
22 changes: 16 additions & 6 deletions CO2_Gadget_Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,12 @@ TOGGLE(displayShowPM25, activeDisplayShowPM25, "PM2.5: ", doNothing, noEvent, wr
,VALUE("Show", true, doDisplayReverse, enterEvent));

MENU(displayConfigMenu, "Display Config", doNothing, noEvent, wrapStyle
#ifdef ARDUINO_LILYGO_T_DISPLAY_S3
,FIELD(DisplayBrightness, "Brightness:", "", 1, 16, 1, 1, doSetDisplayBrightness, anyEvent, wrapStyle)
#endif
#if defined(TTGO_TDISPLAY) || defined(ST7789_240x320)
,FIELD(DisplayBrightness, "Brightness:", "", 10, 255, 10, 10, doSetDisplayBrightness, anyEvent, wrapStyle)
#endif
,FIELD(timeToDisplayOff, "Time To Off:", "", 0, 900, 5, 5, doNothing, noEvent, wrapStyle)
,SUBMENU(activeDisplayOffMenuOnBattery)
,SUBMENU(activeDisplayReverse)
Expand Down Expand Up @@ -1025,7 +1030,7 @@ result idle(menuOut &o, idleEvent e) {
#ifdef DEBUG_ARDUINOMENU
Serial.println("-->[MENU] Event idleStart");
#endif
// setInMenu(false);
setInMenu(false);
// nav.poll();

#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED)
Expand All @@ -1037,7 +1042,8 @@ result idle(menuOut &o, idleEvent e) {
Serial.println("-->[MENU] Event iddling");
#endif
#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED)
displayShowValues(false);
displayShowValues(shouldRedrawDisplay);
shouldRedrawDisplay = false;
#endif
break;
case idleEnd:
Expand All @@ -1064,7 +1070,8 @@ result idle(menuOut &o, idleEvent e) {
void menuLoopTFT() {
#ifdef SUPPORT_TFT
if (millis() < (timeInitializationCompleted + timeToWaitForImprov * 1000)) { // Wait before starting the menu to avoid issues with Improv-WiFi
displayShowValues(false);
displayShowValues(shouldRedrawDisplay);
shouldRedrawDisplay = false;
return;
}

Expand All @@ -1079,7 +1086,8 @@ void menuLoopTFT() {

nav.doInput();
if (nav.sleepTask) {
displayShowValues(false);
displayShowValues(shouldRedrawDisplay);
shouldRedrawDisplay = false;
} else {
if (nav.changed(0)) {
nav.doOutput();
Expand All @@ -1091,12 +1099,14 @@ void menuLoopTFT() {
void menuLoopOLED() {
#ifdef SUPPORT_OLED
if (millis() < (timeInitializationCompleted + timeToWaitForImprov * 1000)) { // Wait before starting the menu to avoid issues with Improv-WiFi
displayShowValues(false);
displayShowValues(shouldRedrawDisplay);
shouldRedrawDisplay = false;
return;
}

if (nav.sleepTask) {
displayShowValues(false);
displayShowValues(shouldRedrawDisplay);
shouldRedrawDisplay = false;
} else {
if (nav.changed(0)) {
u8g2.firstPage();
Expand Down
6 changes: 3 additions & 3 deletions CO2_Gadget_Neopixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ uint32_t green = strip.Color(0, 255, 0);
uint32_t yellow = strip.Color(255, 100, 0);

void setNeopixelBrightness(uint8_t newBrightness) {
Serial.printf("-->[NPIX] Setting neopixel brightness value at %d\n", newBrightness);
strip.setBrightness(newBrightness);
strip.show();
Serial.println("-->[NPIX] Setting neopixel brightness value at: " + String(newBrightness));
strip.setBrightness(newBrightness);
strip.show();
}

void initNeopixel() {
Expand Down
1 change: 1 addition & 0 deletions CO2_Gadget_OLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void initDisplay() {
} else {
u8g2.setDisplayRotation(U8G2_R0);
}
setDisplayBrightness(DisplayBrightness);
displaySplashScreen();
delay(1000);
}
Expand Down
36 changes: 20 additions & 16 deletions CO2_Gadget_Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ void initPreferences() {
autoSelfCalibration = preferences.getBool("autoSelfCal", false);
co2OrangeRange = preferences.getUInt("co2OrangeRange", 700);
co2RedRange = preferences.getUInt("co2RedRange", 1000);
#ifdef LILYGO_T_DISPLAY_S3
DisplayBrightness = preferences.getUInt("DisplayBright", 8);
#else
DisplayBrightness = preferences.getUInt("DisplayBright", 100);
#endif
neopixelBrightness = preferences.getUInt("neopixBright", 50);
selectedNeopixelType = preferences.getUInt("selNeopxType", NEO_GRB + NEO_KHZ800);
activeBLE = preferences.getBool("activeBLE", true);
Expand Down Expand Up @@ -171,7 +175,7 @@ void initPreferences() {
// Retrieve buzzer preferences
toneBuzzerBeep = preferences.getUInt("toneBzrBeep", BUZZER_TONE_MED); // Frequency of the buzzer beep
durationBuzzerBeep = preferences.getUInt("durBzrBeep", DURATION_BEEP_MEDIUM); // Duration of the buzzer beep
timeBetweenBuzzerBeeps = preferences.getUInt("timeBtwnBzr", 65535); // Time between consecutive beeps
timeBetweenBuzzerBeeps = preferences.getUInt("timeBtwnBzr", 65535); // Time between consecutive beeps

rootTopic.trim();
mqttClientId.trim();
Expand All @@ -186,7 +190,7 @@ void initPreferences() {
#ifdef DEBUG_PREFERENCES
printPreferences();
#endif
upgradePreferences();
upgradePreferences();
}

void putPreferences() {
Expand Down Expand Up @@ -251,9 +255,9 @@ void putPreferences() {
preferences.putBool("showPM25", displayShowPM25);

// Buzzer preferences
preferences.putUInt("toneBzrBeep", toneBuzzerBeep); // Buzzer frequency
preferences.putUInt("durBzrBeep", durationBuzzerBeep); // Buzzer duration
preferences.putUInt("timeBtwnBzr", timeBetweenBuzzerBeeps); // Time between beeps
preferences.putUInt("toneBzrBeep", toneBuzzerBeep); // Buzzer frequency
preferences.putUInt("durBzrBeep", durationBuzzerBeep); // Buzzer duration
preferences.putUInt("timeBtwnBzr", timeBetweenBuzzerBeeps); // Time between beeps

preferences.end();

Expand Down Expand Up @@ -315,9 +319,9 @@ String getPreferencesAsJson() {
doc["measInterval"] = preferences.getInt("measInterval", 10);

// Buzzer preferences
doc["toneBzrBeep"] = preferences.getUInt("toneBzrBeep", 1000); // Buzzer frequency
doc["durBzrBeep"] = preferences.getUInt("durBzrBeep", 100); // Buzzer duration
doc["timeBtwnBzr"] = preferences.getUInt("timeBtwnBzr", 65535); // Time between beeps
doc["toneBzrBeep"] = preferences.getUInt("toneBzrBeep", 1000); // Buzzer frequency
doc["durBzrBeep"] = preferences.getUInt("durBzrBeep", 100); // Buzzer duration
doc["timeBtwnBzr"] = preferences.getUInt("timeBtwnBzr", 65535); // Time between beeps

preferences.end();

Expand Down Expand Up @@ -388,9 +392,9 @@ String getActualSettingsAsJson() {
doc["measInterval"] = measurementInterval;

// Buzzer preferences
doc["toneBzrBeep"] = toneBuzzerBeep; // Buzzer frequency
doc["durBzrBeep"] = durationBuzzerBeep; // Buzzer duration
doc["timeBtwnBzr"] = timeBetweenBuzzerBeeps; // Time between beeps
doc["toneBzrBeep"] = toneBuzzerBeep; // Buzzer frequency
doc["durBzrBeep"] = durationBuzzerBeep; // Buzzer duration
doc["timeBtwnBzr"] = timeBetweenBuzzerBeeps; // Time between beeps

String preferencesJson;
serializeJson(doc, preferencesJson);
Expand Down Expand Up @@ -440,12 +444,12 @@ bool handleSavePreferencesfromJSON(String jsonPreferences) {
rootTopic = JsonDocument["rootTopic"].as<String>().c_str();
batteryDischargedMillivolts = JsonDocument["batDischgd"];
batteryFullyChargedMillivolts = JsonDocument["batChargd"];
if (vRef != JsonDocument["vRef"]) { // If battery reference changed, apply it
if (vRef != JsonDocument["vRef"]) { // If battery reference changed, apply it
vRef = JsonDocument["vRef"];
battery.begin(vRef, voltageDividerRatio, &asigmoidal);
readBatteryVoltage();
}
vRef = JsonDocument["vRef"];
vRef = JsonDocument["vRef"];
mqttClientId = JsonDocument["mqttClientId"].as<String>().c_str();
mqttBroker = JsonDocument["mqttBroker"].as<String>().c_str();
mqttUser = JsonDocument["mqttUser"].as<String>().c_str();
Expand Down Expand Up @@ -483,9 +487,9 @@ bool handleSavePreferencesfromJSON(String jsonPreferences) {
displayShowPM25 = JsonDocument["showPM25"];

// Buzzer preferences
toneBuzzerBeep = JsonDocument["toneBzrBeep"]; // Buzzer frequency
durationBuzzerBeep = JsonDocument["durBzrBeep"]; // Buzzer duration
timeBetweenBuzzerBeeps = JsonDocument["timeBtwnBzr"]; // Time between beeps
toneBuzzerBeep = JsonDocument["toneBzrBeep"]; // Buzzer frequency
durationBuzzerBeep = JsonDocument["durBzrBeep"]; // Buzzer duration
timeBetweenBuzzerBeeps = JsonDocument["timeBtwnBzr"]; // Time between beeps

// mqttPass = JsonDocument["mqttPass"].as<String>().c_str();
// wifiPass = JsonDocument["wifiPass"].as<String>().c_str();
Expand Down
33 changes: 23 additions & 10 deletions CO2_Gadget_TFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,26 @@ void setElementLocations() {
void setDisplayBrightness(uint16_t newBrightness) {
#ifdef TTGO_TDISPLAY
if (actualDisplayBrightness != newBrightness) {
// Serial.printf("\n-->[TFT ] DisplayBrightness value at %d\n", DisplayBrightness);
// Serial.printf("-->[TFT ] actualDisplayBrightness value at %d\n", actualDisplayBrightness);
// Serial.printf("-->[TFT ] New display brightness value at %d\n", newBrightness);
// Serial.println("\n-->[TFT ] DisplayBrightness (user setting) value at: " + String(DisplayBrightness));
// Serial.println("-->[TFT ] actualDisplayBrightness value at: " + String(actualDisplayBrightness));
// Serial.println("-->[TFT ] New display brightness value at: " + String(newBrightness));
// delay(20);
analogWrite(TFT_BACKLIGHT, newBrightness);
actualDisplayBrightness = newBrightness;
}
#endif
#ifdef ARDUINO_LILYGO_T_DISPLAY_S3
if (actualDisplayBrightness != newBrightness) {
// Serial.printf("\n-->[TFT ] DisplayBrightness value at %d\n", DisplayBrightness);
// Serial.printf("-->[TFT ] Old actualDisplayBrightness value at %d\n", actualDisplayBrightness);
// Serial.printf("-->[TFT ] New actualDisplayBrightness value at %d\n", newBrightness);
if (newBrightness == 0) {
uint16_t dif, i;
if (newBrightness > actualDisplayBrightness)
dif = 16 - (newBrightness - actualDisplayBrightness);
else
dif = actualDisplayBrightness - newBrightness;

// Serial.printf("-->[TFT ] Change brightness %d levels", dif);
for (i = 1; i <= dif; i++) {
digitalWrite(TFT_BACKLIGHT, LOW);
} else {
delayMicroseconds(20);
digitalWrite(TFT_BACKLIGHT, HIGH);
}
actualDisplayBrightness = newBrightness;
Expand Down Expand Up @@ -265,7 +270,7 @@ void displaySplashScreen() {
void initBacklight() {
#if defined(TTGO_TDISPLAY) || defined(ST7789_240x320)
pinMode(TFT_BACKLIGHT, OUTPUT);
digitalWrite(TFT_BACKLIGHT, 1);
// digitalWrite(TFT_BACKLIGHT, 1); Removed to revert as v.0.12.000 to try to fix #192
setDisplayBrightness(DisplayBrightness);
#endif
#ifdef ARDUINO_LILYGO_T_DISPLAY_S3
Expand All @@ -274,6 +279,10 @@ void initBacklight() {
delay(20);
digitalWrite(TFT_BACKLIGHT, HIGH);
digitalWrite(TFT_POWER_ON_BATTERY, HIGH);
actualDisplayBrightness = 16; // At the beginning brightness is at maximum level
if (DisplayBrightness > 16) // Prevent malfunction if upper values are stored in preferences
DisplayBrightness = 16;
setDisplayBrightness(DisplayBrightness);
#endif
}

Expand All @@ -291,6 +300,7 @@ void initDisplay() {
setElementLocations();
tft.setTextSize(2);
initBacklight();
setDisplayBrightness(DisplayBrightness);
displaySplashScreen(); // Display init and splash screen
delay(2000); // Enjoy the splash screen for 2 seconds
spr.setColorDepth(16);
Expand Down Expand Up @@ -323,6 +333,7 @@ bool displayNotification(String notificationText, notificationTypes notification
tft.drawString(notificationText, tft.width() / 2, tft.height() / 2);
tft.unloadFont();
tft.resetViewport();
shouldRedrawDisplay = true; // Must redraw display to clear the notification
return true;
}

Expand Down Expand Up @@ -354,6 +365,7 @@ bool displayNotification(String notificationText, String notificationText2, noti
tft.drawString(notificationText2, tft.width() / 2 - textWidth2 / 2, tft.height() / 5 * 3 - boxMarging);
tft.unloadFont();
tft.resetViewport();
shouldRedrawDisplay = true; // Must redraw display to clear the notification
return true;
}

Expand Down Expand Up @@ -619,7 +631,8 @@ void displayShowValues(bool forceRedraw = false) {
uint8_t currentDatum = tft.getTextDatum();
tft.unloadFont();
if (forceRedraw) {
// tft.fillScreen(TFT_BLACK);
// Serial.println("-->[TFT ] Displaying values. Force Redraw: " + String(forceRedraw ? "true" : "false"));
tft.fillScreen(TFT_BLACK); // Remove previous remains in the screen
}
showCO2(co2, elementPosition.co2X, elementPosition.co2Y, elementPosition.pixelsToBaseline, forceRedraw);
showCO2units(elementPosition.co2UnitsX, elementPosition.co2UnitsY, forceRedraw);
Expand Down
Loading

0 comments on commit b953a3a

Please sign in to comment.