Skip to content

Commit

Permalink
v1.2.10
Browse files Browse the repository at this point in the history
- Replaced blocking code with non-blocking in *NTPClient.cpp*
- Fix *partitions.csv*
Scheduler API:
- Partial implementation of `$$.net` helper class (http get and ping)
- Implemented `$$.onNext` and `$$.onPrevious`
- Implemented `$$.boundModules.command`
- Implemented `$$.boundModules.isOn/isOff/temperature/luminance/humidity`
  • Loading branch information
genemars committed Apr 30, 2024
1 parent fd33893 commit 99ccf03
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 193 deletions.
24 changes: 17 additions & 7 deletions examples/color-light/color-light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Adafruit_NeoPixel pixels(num, pin, NEO_RGB + NEO_KHZ800);
#endif

bool changed = false;
unsigned long lastRefreshTs = 0;

void statusLedCallback(bool isLedOn) {
if (isLedOn) {
Expand All @@ -56,8 +57,6 @@ void statusLedCallback(bool isLedOn) {
}

void setup() {
statusLED.begin();

homeGenie = HomeGenie::getInstance();

if (!Config::isDeviceConfigured()) {
Expand All @@ -76,6 +75,14 @@ void setup() {
}
#endif
changed = true;
if (millis() - lastRefreshTs > 50) { // force 20fps max
statusLED.show();
#ifdef LED_ARRAY_COUNT
pixels.show();
#endif
lastRefreshTs = millis();
changed = false;
}
});
homeGenie->addAPIHandler(colorLight);

Expand All @@ -86,6 +93,11 @@ void setup() {
cl->onSetColor([i](float r, float g, float b) {
pixels.setPixelColor(i, r, g, b);
changed = true;
if (millis() - lastRefreshTs > 50) { // force 20fps max
pixels.show();
lastRefreshTs = millis();
changed = false;
}
});
homeGenie->addAPIHandler(cl);
}
Expand All @@ -97,21 +109,19 @@ void setup() {

}

statusLED.begin();
homeGenie->begin();
}

unsigned long ts = 0;

void loop()
{
homeGenie->loop();

if (changed) { //&& millis()-ts > 50) { // force 20fps max
if (changed) { // trailing fx
changed = false;
statusLED.show();
#ifdef LED_ARRAY_COUNT
pixels.show();
#endif
ts = millis();
lastRefreshTs = millis();
}
}
10 changes: 1 addition & 9 deletions lib/ESP32_BleSerial/src/BleSerial.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#include "BleSerial.h"
using namespace std;

bool BleSerial::connected()
bool BleSerial::connected() const
{
return Server->getConnectedCount() > 0;
}

void BleSerial::onConnect(BLEServer *pServer)
{
bleConnected = true;
if (enableLed)
digitalWrite(ledPin, HIGH);
}

void BleSerial::onDisconnect(BLEServer *pServer)
{
bleConnected = false;
if (enableLed)
digitalWrite(ledPin, LOW);
Server->startAdvertising();
Expand Down Expand Up @@ -125,7 +123,6 @@ void BleSerial::flush()
TxCharacteristic->setValue(this->transmitBuffer, this->transmitBufferLength);
this->transmitBufferLength = 0;
}
this->lastFlushTime = millis();
TxCharacteristic->notify(true);
}

Expand All @@ -139,7 +136,6 @@ void BleSerial::begin(const char *name, bool enable_led, int led_pin)
pinMode(ledPin, OUTPUT);
}

ConnectedDeviceCount = 0;
BLEDevice::init(name);

Server = BLEDevice::createServer();
Expand Down Expand Up @@ -193,7 +189,3 @@ void BleSerial::SetupSerialService()
RxCharacteristic->setCallbacks(this);
SerialService->start();
}

BleSerial::BleSerial()
{
}
36 changes: 15 additions & 21 deletions lib/ESP32_BleSerial/src/BleSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include <Arduino.h>

#include <BLEDevice.h>
//#include <BLEUtils.h>
//#include <BLEServer.h>
#include <BLE2902.h>
#include "ByteRingBuffer.h"

Expand All @@ -15,23 +13,25 @@
class BleSerial : public BLECharacteristicCallbacks, public BLEServerCallbacks, public Stream
{
public:
BleSerial();
BleSerial() = default;
BleSerial(BleSerial const &other) = delete; // disable copy constructor
void operator=(BleSerial const &other) = delete; // disable assign constructor

void begin(const char *name, bool enable_led = false, int led_pin = 13);
void end();
void onWrite(BLECharacteristic *pCharacteristic);
int available();
int read();
size_t readBytes(uint8_t *buffer, size_t bufferSize);
int peek();
size_t write(uint8_t byte);
void flush();
size_t write(const uint8_t *buffer, size_t bufferSize);
static void end();
void onWrite(BLECharacteristic *pCharacteristic) override;
int available() override;
int read() override;
size_t readBytes(uint8_t *buffer, size_t bufferSize) override;
int peek() override;
size_t write(uint8_t byte) override;
void flush() override;
size_t write(const uint8_t *buffer, size_t bufferSize) override;
size_t print(const char *value);
void onConnect(BLEServer *pServer);
void onDisconnect(BLEServer *pServer);
void onConnect(BLEServer *pServer) override;
void onDisconnect(BLEServer *pServer) override;

bool connected();
bool connected() const;

BLEServer *Server;

Expand All @@ -49,25 +49,19 @@ class BleSerial : public BLECharacteristicCallbacks, public BLEServerCallbacks,
int ledPin = 13;
protected:
size_t transmitBufferLength;
bool bleConnected;

private:
BleSerial(BleSerial const &other) = delete; // disable copy constructor
void operator=(BleSerial const &other) = delete; // disable assign constructor

ByteRingBuffer<RX_BUFFER_SIZE> receiveBuffer;
size_t numAvailableLines;

unsigned long long lastFlushTime;
uint8_t transmitBuffer[BLE_BUFFER_SIZE];

int ConnectedDeviceCount;
void SetupSerialService();

uint16_t peerMTU;
uint16_t maxTransferSize = BLE_BUFFER_SIZE;

bool checkMTU();
/*
Bluetooth LE GATT UUIDs for the Nordic UART profile
Change UUID here if required
Expand Down
20 changes: 13 additions & 7 deletions lib/NTPClient-master/NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,30 @@ bool NTPClient::forceUpdate() {
this->sendNTPPacket();

// Wait till data is there or timeout...
byte timeout = 0;
unsigned int startCheckMs = millis();
int cb = 0;
do {
delay ( 10 );

cb = this->_udp->parsePacket();

if(cb > 0)
{
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
if(!this->isValid(this->_packetBuffer))
cb = 0;
}

if (timeout > 100) return false; // timeout after 1000 ms
timeout++;
if (cb == 0) {
if (millis() - startCheckMs > 1000) {
// timeout after 1000 ms
return false;
}
unsigned int delayStartMs = millis();
while (millis() - delayStartMs < 50) {
yield();
}
}
} while (cb == 0);

this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
this->_lastUpdate = millis();

unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
Expand Down
2 changes: 1 addition & 1 deletion lib/duktape-2.7.0/src/duk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3085,7 +3085,7 @@ typedef struct duk_hthread duk_context;
#define DUK_USE_VALSTACK_LIMIT 1000000L
#define DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT 2
#define DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT 4
#undef DUK_USE_VALSTACK_UNSAFE
//#define DUK_USE_VALSTACK_UNSAFE
//#define DUK_USE_VERBOSE_ERRORS
//#define DUK_USE_VERBOSE_EXECUTOR_ERRORS
//#define DUK_USE_VOLUNTARY_GC
Expand Down
35 changes: 4 additions & 31 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ framework = arduino
board = esp32dev
board_build.filesystem = littlefs
board_build.flash_size = 4MB
board_build.partitions = ./src/partitions_ota.csv
board_build.partitions = ./src/partitions.csv
lib_deps =
[email protected]
thijse/[email protected]
Expand All @@ -29,7 +29,8 @@ lib_deps =
hideakitai/[email protected]
adafruit/[email protected]
[email protected]
lovyan03/LovyanGFX@^1.1.12
lovyan03/[email protected]
dvarrel/[email protected]


[env:default]
Expand All @@ -53,7 +54,7 @@ build_flags = ${env.build_flags} -D MINI_ESP32 -D CONFIG_ServiceButtonPin=16 -D
[env:sonoff]
platform = [email protected]
build_flags = ${env.build_flags} -D DISABLE_UI -D CONFIG_ServiceButtonPin=0 -D CONFIG_StatusLedPin=13 -D CONFIG_GPIO_OUT={14,27} -D CONFIG_GPIO_IN={32,33}

board_build.partitions = no_ota.csv

#------------------[ Examples ]------------------

Expand Down Expand Up @@ -133,10 +134,6 @@ lib_ignore =
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/ir-transceiver>
lib_deps = ${env.lib_deps}
Expand All @@ -157,10 +154,6 @@ lib_deps = ${env.lib_deps}
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D CONFIG_StatusLedPin=-1 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<lib/jerryscript/amalgam/*> +<examples/color-light>
lib_deps = ${env.lib_deps}
Expand All @@ -170,10 +163,6 @@ lib_deps = ${env.lib_deps}
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D CONFIG_StatusLedPin=-1 -D LED_ARRAY_COUNT=25 -D LED_ARRAY_PIN=8 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
lib_deps = ${env.lib_deps}
Expand All @@ -183,10 +172,6 @@ lib_deps = ${env.lib_deps}
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D CONFIG_StatusLedPin=-1 -D LED_ARRAY_COUNT=64 -D LED_ARRAY_PIN=5 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
lib_deps = ${env.lib_deps}
Expand All @@ -196,10 +181,6 @@ lib_deps = ${env.lib_deps}
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D CONFIG_StatusLedPin=-1 -D LED_ARRAY_COUNT=90 -D LED_ARRAY_PIN=5 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
lib_deps = ${env.lib_deps}
Expand All @@ -218,10 +199,6 @@ lib_deps = ${env.lib_deps}
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D CONFIG_StatusLedPin=-1 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/shutter>
lib_deps = ${env.lib_deps}
Expand All @@ -239,9 +216,5 @@ build_src_filter = +<src> -<src/main.cpp> +<examples/playground>
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/playground>
2 changes: 1 addition & 1 deletion src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "defs.h"

#include <Arduino.h>
#ifdef CONFIG_CREATE_AUTOMATION_TASK
#ifdef CONFIG_AUTOMATION_SPAWN_FREERTOS_TASK
#include <FreeRTOSConfig.h>
#endif
#ifdef ESP32
Expand Down
3 changes: 2 additions & 1 deletion src/HomeGenie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace Service {
// set scheduler callback
Automation::Scheduler::setListener(this);

#ifdef CONFIG_CREATE_AUTOMATION_TASK
#ifdef CONFIG_AUTOMATION_SPAWN_FREERTOS_TASK
/*
xTaskCreate(
reinterpret_cast<TaskFunction_t>(Scheduler::loop),
Expand Down Expand Up @@ -216,6 +216,7 @@ namespace Service {
handled = handled || handler->handleRequest(request, responseCallback);
}
//if (handled) break;
yield();
}
return handled;
}
Expand Down
4 changes: 2 additions & 2 deletions src/automation/ProgramEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Automation {
std::function<void(void*, const char* , ResponseCallback*)> ProgramEngine::apiRequest = nullptr;

ProgramEngine::ProgramEngine() {
#ifndef CONFIG_CREATE_AUTOMATION_TASK
#ifndef CONFIG_AUTOMATION_SPAWN_FREERTOS_TASK
setLoopInterval(100);
#endif
};
Expand All @@ -50,7 +50,7 @@ namespace Automation {
ProgramEngine::apiRequest = std::move(ar);
}

#ifdef CONFIG_CREATE_AUTOMATION_TASK
#ifdef CONFIG_AUTOMATION_SPAWN_FREERTOS_TASK
[[noreturn]] void ProgramEngine::worker() {
for(;;) {
auto jobs = &ProgramEngine::scheduleList;
Expand Down
Loading

0 comments on commit 99ccf03

Please sign in to comment.