Skip to content

Commit

Permalink
CO2 Gadget Beta v0.12.061-development
Browse files Browse the repository at this point in the history
Refactor code to skip certain functionality when downloading BLE
Add sampleInterval variable and add to preferences
  • Loading branch information
melkati committed May 5, 2024
1 parent e4a6b4e commit 9fd1f80
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ bool inMenu = false;
bool shouldWakeUpDisplay = false;
bool shouldRedrawDisplay = false;
uint16_t measurementInterval = 10;
uint16_t sampleInterval = 60;
bool bleInitialized = false;
int8_t selectedCO2Sensor = -1;
bool outputsModeRelay = false;
Expand Down Expand Up @@ -311,6 +312,7 @@ void wakeUpDisplay() {
}

void processPendingCommands() {
if (isDownloadingBLE) return;
if (pendingCalibration == true) {
if (calibrationValue != 0) {
printf("-->[MAIN] Calibrating CO2 sensor at %d PPM\n", calibrationValue);
Expand Down Expand Up @@ -398,13 +400,15 @@ void outputsRGBLeds() {
}

void outputsLoop() {
if (isDownloadingBLE) return;
outputsRelays();
outputsRGBLeds();
neopixelLoop();
buzzerLoop();
}

void readingsLoop() {
if (isDownloadingBLE) return;
if (esp_timer_get_time() - lastReadingsCommunicationTime >= startCheckingAfterUs) {
if (newReadingsAvailable) {
lastReadingsCommunicationTime = esp_timer_get_time();
Expand All @@ -430,6 +434,7 @@ void readingsLoop() {

void adjustBrightnessLoop() {
#if defined(SUPPORT_OLED) || defined(SUPPORT_TFT)
if (isDownloadingBLE) return;

if (shouldWakeUpDisplay) {
wakeUpDisplay();
Expand Down Expand Up @@ -508,6 +513,7 @@ void setCpuFrequencyAndReinitSerial(int16_t newCpuFrequency) {
}

void utilityLoop() {
if (isDownloadingBLE) return;
int16_t actualCPUFrequency = getCpuFrequencyMhz();
const int16_t highCpuFrequency = 240;
const int16_t lowCpuFrequency = 80;
Expand Down
19 changes: 14 additions & 5 deletions CO2_Gadget_BLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ DataProvider provider(lib, DataType::T_RH_CO2_ALT, true, true, true, &wifi);

void setBLEHistoryInterval(uint64_t interval) {
#ifdef SUPPORT_BLE
provider.setHistoryInterval(interval * 1000);
if (provider.getHistoryInterval() != interval * 1000) {
#ifdef DEBUG_BLE
Serial.flush();
Serial.println("-->[BLE ] Actual history interval: " + String(provider.getHistoryInterval() / 1000) + " seconds");
Serial.println("-->[BLE ] Setting history interval to: " + String(interval) + " seconds");
delay(20);
#endif
provider.setHistoryInterval(interval * 1000);
}
#endif
}

Expand All @@ -23,15 +31,15 @@ void initBLE() {
if (activeBLE) {
if (bleInitialized) {
Serial.print(
"-->[SBLE] Sensirion Gadget BLE Lib already initialized with deviceId = ");
"-->[BLE ] Sensirion Gadget BLE Lib already initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());
return;
} else {
setBLEHistoryInterval(60);
setBLEHistoryInterval(sampleInterval);
provider.begin();
Serial.print("-->[SBLE] Sensirion Gadget BLE Lib initialized with deviceId = ");
Serial.print("-->[BLE ] Sensirion Gadget BLE Lib initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());
Serial.print("-->[SBLE] History interval set to: ");
Serial.print("-->[BLE ] History interval set to: ");
Serial.print(provider.getHistoryInterval() / 1000);
Serial.println(" seconds");
// Set initial battery level
Expand Down Expand Up @@ -128,6 +136,7 @@ void BLELoop() {
}
provider.handleDownload();
isDownloadingBLE = provider.isDownloading();
if (isDownloadingBLE) return;
// delay(3);
if (provider.wifiChanged()) handleBLEwifiChanged();
if (provider.historyIntervalChanged()) {
Expand Down
1 change: 1 addition & 0 deletions CO2_Gadget_Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void readBatteryVoltage() {
}

void batteryLoop() {
if (isDownloadingBLE) return;
readBatteryVoltage();
if (!inMenu) {
if (abs(lastBatteryVoltage - batteryVoltage) >= 0.1) { // If battery voltage changed by at least 0.1V, update battery level
Expand Down
1 change: 1 addition & 0 deletions CO2_Gadget_Improv.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void initImprov() {
}

void improvLoop() {
if (isDownloadingBLE) return;
if (inMenu) { // If in menu ignore Improv functionality
#ifdef DEBUG_IMPROV_WIFI
static unsigned long lastPrintTime = 0;
Expand Down
1 change: 1 addition & 0 deletions CO2_Gadget_MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ void publishMQTT() {

void mqttClientLoop() {
#ifdef SUPPORT_MQTT
if (isDownloadingBLE) return;
if (troubledMQTT && (millis() - timeTroubledMQTT >= timeToRetryTroubledMQTT * 1000)) {
troubledMQTT = false;
timeTroubledMQTT = 0;
Expand Down
6 changes: 6 additions & 0 deletions CO2_Gadget_Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ void printPreferences() {
Serial.println("-->[PREF] displayReverse is:\t#" + String(displayReverse ? "Reversed" : "Normal") + "# (" + String(displayReverse) + ")");
Serial.println("-->[PREF] showFahrenheit is:\t#" + String(showFahrenheit ? "Fahrenheit" : "Celsius") + "#");
Serial.println("-->[PREF] measInterval:\t #" + String(measurementInterval) + "#");
Serial.println("-->[PREF] sampInterval:\t #" + String(sampleInterval) + "#");
Serial.println("-->[PREF] outModeRelay is:\t#" + String(outputsModeRelay ? "Relay" : "RGB LED") + "#");
Serial.println("-->[PREF] channelESPNow:\t #" + String(channelESPNow) + "#");
Serial.println("-->[PREF] boardIdESPNow:\t #" + String(boardIdESPNow) + "#");
Expand Down Expand Up @@ -291,6 +292,7 @@ void initPreferences() {
displayReverse = preferences.getBool("displayReverse", false);
showFahrenheit = preferences.getBool("showFahrenheit", false);
measurementInterval = preferences.getUInt("measInterval", 10);
sampleInterval = preferences.getUInt("sampInterval", 60);
outputsModeRelay = preferences.getBool("outModeRelay", false);
channelESPNow = preferences.getUInt("channelESPNow", ESPNOW_WIFI_CH);
boardIdESPNow = preferences.getUInt("boardIdESPNow", 0);
Expand Down Expand Up @@ -394,6 +396,7 @@ void putPreferences() {
preferences.putBool("displayReverse", displayReverse);
preferences.putBool("showFahrenheit", showFahrenheit);
preferences.putUInt("measInterval", measurementInterval);
preferences.putUInt("sampInterval", sampleInterval);
preferences.putBool("outModeRelay", outputsModeRelay);
preferences.putUInt("channelESPNow", channelESPNow);
preferences.putUInt("boardIdESPNow", boardIdESPNow);
Expand Down Expand Up @@ -490,6 +493,7 @@ String getPreferencesAsJson() {
doc["showCO2"] = preferences.getBool("showCO2", true);
doc["showPM25"] = preferences.getBool("showPM25", true);
doc["measInterval"] = preferences.getInt("measInterval", 10);
doc["sampInterval"] = preferences.getInt("sampInterval", 60);

// Buzzer preferences
doc["toneBzrBeep"] = preferences.getUInt("toneBzrBeep", 1000); // Buzzer frequency
Expand Down Expand Up @@ -570,6 +574,7 @@ String getActualSettingsAsJson() {
doc["showCO2"] = displayShowCO2;
doc["showPM25"] = displayShowPM25;
doc["measInterval"] = measurementInterval;
doc["sampInterval"] = sampleInterval;

// Buzzer preferences
doc["toneBzrBeep"] = toneBuzzerBeep; // Buzzer frequency
Expand Down Expand Up @@ -646,6 +651,7 @@ bool handleSavePreferencesfromJSON(String jsonPreferences) {
displayReverse = JsonDocument["displayReverse"];
showFahrenheit = JsonDocument["showFahrenheit"];
measurementInterval = JsonDocument["measurementInterval"];
sampleInterval = JsonDocument["sampleInterval"];
outputsModeRelay = JsonDocument["outModeRelay"];
channelESPNow = JsonDocument["channelESPNow"];
boardIdESPNow = JsonDocument["boardIdESPNow"];
Expand Down
3 changes: 2 additions & 1 deletion CO2_Gadget_Sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void initSensors() {
sensors.setCO2AltitudeOffset(altitudeMeters);
// sensors.setAutoSelfCalibration(false); // TO-DO: Implement in CanAirIO Sensors Lib

setBLEHistoryInterval(60);
setBLEHistoryInterval(sampleInterval);

Serial.printf("-->[SENS] Selected CO2 Sensor: %d\n", selectedCO2Sensor);

Expand Down Expand Up @@ -152,6 +152,7 @@ void initSensors() {
}

void sensorsLoop() {
if (isDownloadingBLE) return;
if (!buzzerBeeping) {
sensors.loop();
}
Expand Down
2 changes: 2 additions & 0 deletions CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ void initWifi() {
}

void wifiClientLoop() {
if (isDownloadingBLE) return;
if (activeWIFI && troubledWIFI && (millis() - timeTroubledWIFI >= timeToRetryTroubledWIFI * 1000)) {
initWifi();
}
Expand All @@ -1091,6 +1092,7 @@ void wifiClientLoop() {

void OTALoop() {
#ifdef SUPPORT_OTA
if (isDownloadingBLE) return;
if ((activeWIFI) && (activeOTA) && (!troubledWIFI) && (WiFi.status() == WL_CONNECTED)) {
AsyncElegantOTA.loop();
}
Expand Down

0 comments on commit 9fd1f80

Please sign in to comment.