Skip to content

Commit

Permalink
[LPM] Add capability to deactivate the function at build time (#1449)
Browse files Browse the repository at this point in the history
To enable boards with the constant power supply to don't build this function.
Change the default behavior for the mode not to be built by default.
Only the boards with batteries are keeping it
  • Loading branch information
1technophile authored Feb 10, 2023
1 parent 7c0fafd commit 5bcf214
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
6 changes: 5 additions & 1 deletion environments.ini
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ build_flags =
'-DLED_SEND_RECEIVE_ON=0'
'-DGateway_Name="OpenMQTTGateway_LOLIN32LITE_BLE"'
;; Low power parameters, uncomment and fill credentials
; '-DDEFAULT_LOW_POWER_MODE=2'
'-DDEFAULT_LOW_POWER_MODE=2'
; '-DTimeBtwRead=155000'
; '-DScan_duration=2000'
; '-DAttemptBLEConnect=false'
Expand Down Expand Up @@ -460,6 +460,7 @@ build_flags =
'-DLED_SEND_RECEIVE_ON=1'
'-DTRIGGER_GPIO=35'
'-DIR_EMITTER_GPIO=17'
'-DDEFAULT_LOW_POWER_MODE=0'
'-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_BLE_IR"'
board_upload.speed = 921600
custom_description = Expandable module with BLE gateway, display, and little IR emitter
Expand All @@ -484,6 +485,7 @@ build_flags =
'-DTRIGGER_GPIO=37'
'-DSLEEP_BUTTON=38'
'-DINPUT_GPIO=39'
'-DDEFAULT_LOW_POWER_MODE=0'
'-DGateway_Name="OpenMQTTGateway_ESP32_M5STACK_BLE"'
board_upload.speed = 921600
custom_description = Expandable module with BLE gateway and display
Expand Down Expand Up @@ -533,6 +535,7 @@ build_flags =
'-DTRIGGER_GPIO=39'
'-DIR_EMITTER_INVERTED=true'
'-DIR_EMITTER_GPIO=9'
'-DDEFAULT_LOW_POWER_MODE=0'
'-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_C_BLE_IR"'
board_upload.speed = 1500000
custom_description = Expandable module with BLE gateway, display, and little IR emitter
Expand Down Expand Up @@ -563,6 +566,7 @@ build_flags =
'-DTRIGGER_GPIO=39'
'-DIR_EMITTER_INVERTED=true'
'-DIR_EMITTER_GPIO=9'
'-DDEFAULT_LOW_POWER_MODE=0'
'-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_CP_BLE_IR"'
board_upload.speed = 1500000
custom_description = Expandable module with BLE gateway, display, and little IR emitter
Expand Down
3 changes: 2 additions & 1 deletion main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,13 @@ static_assert(MQTT_SECURE_SELF_SIGNED_INDEX_DEFAULT < (sizeof(certs_array) / siz
#endif

/*------------------DEEP SLEEP parameters ------------------*/
//DEFAULT_LOW_POWER_MODE -1 to normal mode, low power mode can't be used on this build
//DEFAULT_LOW_POWER_MODE 0 to normal mode (no power consumption optimisations)
//DEFAULT_LOW_POWER_MODE 1 to activate deep sleep
//DEFAULT_LOW_POWER_MODE 2 to activate deep sleep (LCD is turned OFF)
#ifdef ESP32
# ifndef DEFAULT_LOW_POWER_MODE
# define DEFAULT_LOW_POWER_MODE 0
# define DEFAULT_LOW_POWER_MODE -1
# endif
int lowpowermode = DEFAULT_LOW_POWER_MODE;
#endif
Expand Down
31 changes: 19 additions & 12 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -741,12 +741,13 @@ void coreTask(void* pvParameters) {
BLEconnect();
}
dumpDevices();
Log.trace(F("CoreTask stack free: %u" CR), uxTaskGetStackHighWaterMark(xCoreTaskHandle));
xSemaphoreGive(semaphoreBLEOperation);
} else {
Log.error(F("Failed to start scan - BLE busy" CR));
}
}
if (lowpowermode) {
if (lowpowermode > 0) {
lowPowerESP32();
int scan = atomic_exchange_explicit(&forceBTScan, 0, ::memory_order_seq_cst); // is this enough, it will wait the full deepsleep...
if (scan == 1) BTforceScan();
Expand All @@ -764,51 +765,57 @@ void coreTask(void* pvParameters) {
}
}

# if DEFAULT_LOW_POWER_MODE != -1
void lowPowerESP32() { // low power mode
Log.trace(F("Going to deep sleep for: %l s" CR), (TimeBtwRead / 1000));
deepSleep(TimeBtwRead * 1000);
}

void deepSleep(uint64_t time_in_us) {
# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH)
# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH)
sleepScreen();
esp_sleep_enable_ext0_wakeup((gpio_num_t)SLEEP_BUTTON, LOW);
# endif
# endif

Log.trace(F("Deactivating ESP32 components" CR));
if (BLEDevice::getInitialized()) BLEDevice::deinit(true);
esp_bt_mem_release(ESP_BT_MODE_BTDM);
// Ignore the deprecated warning, this call is necessary here.
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
adc_power_off();
# pragma GCC diagnostic pop
# pragma GCC diagnostic pop
esp_wifi_stop();
esp_deep_sleep(time_in_us);
}
# else
void lowPowerESP32() {}
# endif

void changelowpowermode(int newLowPowerMode) {
# if DEFAULT_LOW_POWER_MODE != -1
Log.notice(F("Changing LOW POWER mode to: %d" CR), newLowPowerMode);
# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH)
# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH)
if (lowpowermode == 2) {
# ifdef ZboardM5STACK
# ifdef ZboardM5STACK
M5.Lcd.wakeup();
# endif
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH)
# endif
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH)
M5.Axp.SetLDO2(true);
M5.Lcd.begin();
# endif
# endif
}
char lpm[2];
sprintf(lpm, "%d", newLowPowerMode);
M5Print("Changing LOW POWER mode to:", lpm, "");
# endif
# endif
lowpowermode = newLowPowerMode;
preferences.begin(Gateway_Short_Name, false);
preferences.putUInt("lowpowermode", lowpowermode);
preferences.end();
// Publish the states to update the controller switch status
stateMeasures();
# endif
}

void setupBT() {
Expand Down
2 changes: 2 additions & 0 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ void pubMqttDiscovery() {
stateClassNone, //State Class
"false", "true" //state_off, state_on
);
# if DEFAULT_LOW_POWER_MODE != -1
createDiscovery("switch", //set Type
subjectSYStoMQTT, "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.lowpowermode | bool }}", //set availability_topic,device_class,value_template,
Expand All @@ -1039,6 +1040,7 @@ void pubMqttDiscovery() {
stateClassNone, //State Class
"false", "true" //state_off, state_on
);
# endif
# endif
# endif
}
Expand Down

0 comments on commit 5bcf214

Please sign in to comment.