Skip to content

Commit

Permalink
Merge pull request #191 from Coscolin/s3-background-brightness
Browse files Browse the repository at this point in the history
Implement brightness regulation for Lilygo T-Display S3 board
  • Loading branch information
melkati authored Mar 14, 2024
2 parents 82d3df3 + ef30148 commit 465df08
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
5 changes: 5 additions & 0 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
#ifdef TTGO_TDISPLAY || 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
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
18 changes: 13 additions & 5 deletions CO2_Gadget_TFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,16 @@ void setDisplayBrightness(uint16_t 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 @@ -274,6 +278,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 Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ build_flags =
-DRED_PIN=01 ; GPIO to go HIGH on red color range
-DBLUE_PIN=03 ; GPIO to go HIGH on orange color range
-DSUPPORT_TFT
-DLILYGO_T_DISPLAY_S3
-DLV_LVGL_H_INCLUDE_SIMPLE
; -DARDUINO_USB_CDC_ON_BOOT=1
; -DARDUINO_USB_MODE=1
Expand Down

0 comments on commit 465df08

Please sign in to comment.