Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
theelims committed Dec 20, 2024
1 parent 861bb49 commit e1dc49d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 19 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ All notable changes to this project will be documented in this file.

## WIP

### Added

- Added compatibility with ESP32-C6
- Added Arduino Log Colors
- Possibility to add a loop callback to ESP32-Sveltekit to leverage its loop threat. Meant to include custom services so no separate task is needed for them.
- Change wake-up pin in SleepService during runtime. It is also possible to use the internal pull-up or pull-down resistors now.

### Changed

- Updated platform espressif32 to 6.8.1

### Fixed

- Ensure thread safety for client subscriptions [#58](https://github.com/theelims/ESP32-sveltekit/pull/58)
- Isolate non-returning functions in new tasks [#62](https://github.com/theelims/ESP32-sveltekit/pull/62)

## [0.5.0] - 2024-05-06

Expand Down
16 changes: 16 additions & 0 deletions docs/statefulservice.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ esp32sveltekit.setMDNSAppName("ESP32 SvelteKit Demo App");

making the entry a little bit more verbose. This must be called before `esp32sveltekit.begin();`. If you want to advertise further services just include `#include <ESPmNDS.h>` and use `MDNS.addService()` regularly.

### Use ESP32-SvelteKit loop() Function

Under some circumstances custom services might want to do something periodically. One solution would be to use a dedicated task or RTOS timer for this. Or you can leverage the ESP32-SvelteKit loop-function and have it executed as a callback every 20ms.

```cpp
esp32sveltekit.addLoopFunction(callback)
```

### Factory Reset

A factory reset can not only be evoked from the API, but also by calling
Expand Down Expand Up @@ -513,6 +521,14 @@ The settings wakeup pin definition and the signal polarity need to be defined in
-D WAKEUP_SIGNAL=0 ; 1 for wakeup on HIGH, 0 for wakeup on LOW
```

In addition it is possible to change this as well at runtime by calling:

```cpp
esp32sveltekit.getSleepService()->setWakeUpPin(int pin, bool level, pinTermination termination = pinTermination::FLOATING);
```
With this function it is also possible to configure the internal pull-up or pull-down resistor for this RTC pin. Albeit this might increase the deep sleep current slightly.
A callback function can be attached and triggers when the ESP32 is requested to go into deep sleep. This allows you to safely deal with the power down event. Like persisting software state by writing to the flash, tiding up or notify a remote server about the immanent disappearance.
```cpp
Expand Down
22 changes: 22 additions & 0 deletions lib/framework/BatteryService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ BatteryService::BatteryService(EventSocket *socket) : _socket(socket)
{
}

void BatteryService::updateSOC(float stateOfCharge)
{
_lastSOC = (int)round(stateOfCharge);
batteryEvent();
}

void BatteryService::setCharging(boolean isCharging)
{
_isCharging = isCharging;
batteryEvent();
}

boolean BatteryService::isCharging()
{
return _isCharging;
}

boolean BatteryService::getSOC()
{
return _lastSOC;
}

void BatteryService::begin()
{
_socket->registerEvent(EVENT_BATTERY);
Expand Down
21 changes: 9 additions & 12 deletions lib/framework/BatteryService.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ class BatteryService

void begin();

void updateSOC(float stateOfCharge)
{
_lastSOC = (int)round(stateOfCharge);
batteryEvent();
}

void setCharging(boolean isCharging)
{
_isCharging = isCharging;
batteryEvent();
}
void updateSOC(float stateOfCharge);

void setCharging(boolean isCharging);

boolean isCharging();

boolean getSOC();

private:
void batteryEvent();
EventSocket *_socket;
int _lastSOC = 100;
boolean _isCharging = false;

void batteryEvent();
};
6 changes: 6 additions & 0 deletions lib/framework/ESP32SvelteKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ void ESP32SvelteKit::_loop()
#if FT_ENABLED(FT_MQTT)
_mqttSettingsService.loop(); // 5 seconds
#endif
// iterate over all loop functions
for (auto &function : _loopFunctions)
{
function();
}

vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
11 changes: 11 additions & 0 deletions lib/framework/ESP32SvelteKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <WiFiStatus.h>
#include <ESPFS.h>
#include <PsychicHttp.h>
#include <vector>

#ifdef EMBED_WWW
#include <WWWData.h>
Expand All @@ -64,6 +65,9 @@
#define ESP32SVELTEKIT_RUNNING_CORE -1
#endif

// define callback function to include into the main loop
typedef std::function<void()> loopCallback;

class ESP32SvelteKit
{
public:
Expand Down Expand Up @@ -166,6 +170,11 @@ class ESP32SvelteKit
_apSettingsService.recoveryMode();
}

void addLoopFunction(loopCallback function)
{
_loopFunctions.push_back(function);
}

private:
PsychicHttpServer *_server;
unsigned int _numberEndpoints;
Expand Down Expand Up @@ -213,6 +222,8 @@ class ESP32SvelteKit
protected:
static void _loopImpl(void *_this) { static_cast<ESP32SvelteKit *>(_this)->_loop(); }
void _loop();

std::vector<loopCallback> _loopFunctions;
};

#endif
39 changes: 32 additions & 7 deletions lib/framework/SleepService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

#include <SleepService.h>

// Definition of static member variable
// Definition of static member variables
void (*SleepService::_callbackSleep)() = nullptr;
u_int64_t _wakeUpPin = WAKEUP_PIN_NUMBER;
bool _wakeUpSignal = WAKEUP_SIGNAL;
pinTermination _wakeUpTermination = pinTermination::FLOATING;

SleepService::SleepService(PsychicHttpServer *server,
SecurityManager *securityManager) : _server(server),
Expand Down Expand Up @@ -71,25 +74,47 @@ void SleepService::sleepNow()
WiFi.disconnect(true);
delay(500);

// Prepare ESP for sleep
uint64_t bitmask = (uint64_t)1 << (WAKEUP_PIN_NUMBER);
ESP_LOGD("SleepService", "Enabling GPIO wakeup on pin GPIO%d\n", _wakeUpPin);

// special treatment for ESP32-C3 because of the RISC-V architecture
#ifdef CONFIG_IDF_TARGET_ESP32C3
esp_deep_sleep_enable_gpio_wakeup(bitmask, (esp_deepsleep_gpio_wake_up_mode_t)WAKEUP_SIGNAL);
esp_deep_sleep_enable_gpio_wakeup(BIT(_wakeUpPin), (esp_deepsleep_gpio_wake_up_mode_t)_wakeUpSignal);
#else
esp_sleep_enable_ext1_wakeup(bitmask, (esp_sleep_ext1_wakeup_mode_t)WAKEUP_SIGNAL);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_sleep_enable_ext1_wakeup(BIT(_wakeUpPin), (esp_sleep_ext1_wakeup_mode_t)_wakeUpSignal);

switch (_wakeUpTermination)
{
case pinTermination::PULL_UP:
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
rtc_gpio_pullup_dis((gpio_num_t)_wakeUpPin);
rtc_gpio_pulldown_en((gpio_num_t)_wakeUpPin);
break;
case pinTermination::PULL_DOWN:
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
rtc_gpio_pullup_en((gpio_num_t)_wakeUpPin);
rtc_gpio_pulldown_dis((gpio_num_t)_wakeUpPin);
break;
default:
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
}
#endif

#ifdef SERIAL_INFO
Serial.println("Good by!");
#endif

xTaskCreate(
[](void *pvParams) {
[](void *pvParams)
{
delay(200);
esp_deep_sleep_start();
},
"Sleep task", 4096, nullptr, 10, nullptr);
}

void SleepService::setWakeUpPin(int pin, bool level, pinTermination termination)
{
_wakeUpPin = (u_int64_t)pin;
_wakeUpSignal = level;
_wakeUpTermination = termination;
}
10 changes: 10 additions & 0 deletions lib/framework/SleepService.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <PsychicHttp.h>
#include <SecurityManager.h>
#include "driver/rtc_io.h"

#define SLEEP_SERVICE_PATH "/rest/sleep"

Expand All @@ -29,6 +30,13 @@
#define WAKEUP_SIGNAL 0
#endif

enum class pinTermination
{
FLOATING,
PULL_UP,
PULL_DOWN
};

class SleepService
{
public:
Expand All @@ -43,6 +51,8 @@ class SleepService
_callbackSleep = callbackSleep;
}

void setWakeUpPin(int pin, bool level, pinTermination termination = pinTermination::FLOATING);

private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ build_flags =
; D E B U G B U I L D F L A G S
; ===============================
; These build flags are only for debugging purposes and should not be used in production
-D CONFIG_ARDUHAL_LOG_COLORS

; Uncomment to show log messages from the ESP Arduino Core and ESP32-SvelteKit
-D CORE_DEBUG_LEVEL=4
Expand Down

0 comments on commit e1dc49d

Please sign in to comment.