Fix to be non blocking without tasks
This commit is contained in:
@@ -150,5 +150,4 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
victron.loop();
|
victron.loop();
|
||||||
delay(100);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,7 +159,17 @@ void setup() {
|
|||||||
Serial.println("\nStarting BLE scan...\n");
|
Serial.println("\nStarting BLE scan...\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint32_t loopCount = 0;
|
||||||
|
static uint32_t lastReport = 0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
victron.loop();
|
victron.loop(); // Non-blocking: returns immediately if scan is running
|
||||||
delay(100);
|
loopCount++;
|
||||||
|
|
||||||
|
uint32_t now = millis();
|
||||||
|
if (now - lastReport >= 10000) {
|
||||||
|
Serial.printf("Loop iterations in last 10s: %lu\n", loopCount);
|
||||||
|
loopCount = 0;
|
||||||
|
lastReport = now;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
victron.loop();
|
victron.loop(); // Blocks for scanDuration seconds
|
||||||
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
if (now - lastSendTime >= SEND_INTERVAL_MS) {
|
if (now - lastSendTime >= SEND_INTERVAL_MS) {
|
||||||
@@ -171,5 +171,4 @@ void loop() {
|
|||||||
blePacketCount, sendCount, sendFailCount, cachedCount);
|
blePacketCount, sendCount, sendFailCount, cachedCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(100);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,8 @@
|
|||||||
|
|
||||||
VictronBLE::VictronBLE()
|
VictronBLE::VictronBLE()
|
||||||
: deviceCount(0), pBLEScan(nullptr), scanCallbackObj(nullptr),
|
: deviceCount(0), pBLEScan(nullptr), scanCallbackObj(nullptr),
|
||||||
callback(nullptr), debugEnabled(false), scanDuration(5), initialized(false) {
|
callback(nullptr), debugEnabled(false), scanDuration(5),
|
||||||
|
minIntervalMs(1000), initialized(false) {
|
||||||
memset(devices, 0, sizeof(devices));
|
memset(devices, 0, sizeof(devices));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,10 +65,18 @@ bool VictronBLE::addDevice(const char* name, const char* mac, const char* hexKey
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scan complete callback — sets flag so loop() restarts
|
||||||
|
static bool s_scanning = false;
|
||||||
|
static void onScanDone(BLEScanResults results) {
|
||||||
|
s_scanning = false;
|
||||||
|
}
|
||||||
|
|
||||||
void VictronBLE::loop() {
|
void VictronBLE::loop() {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
pBLEScan->start(scanDuration, false);
|
if (!s_scanning) {
|
||||||
pBLEScan->clearResults();
|
pBLEScan->clearResults();
|
||||||
|
s_scanning = pBLEScan->start(scanDuration, onScanDone, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BLE scan callback
|
// BLE scan callback
|
||||||
@@ -101,11 +110,26 @@ void VictronBLE::processDevice(BLEAdvertisedDevice& advertisedDevice) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debugEnabled) Serial.printf("[VictronBLE] Processing: %s\n", entry->device.name);
|
// Skip if nonce unchanged (data hasn't changed on the device)
|
||||||
|
if (entry->device.dataValid && mfgData.nonceDataCounter == entry->lastNonce) {
|
||||||
|
// Still update RSSI since we got a packet
|
||||||
|
entry->device.rssi = advertisedDevice.getRSSI();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if minimum interval hasn't elapsed
|
||||||
|
uint32_t now = millis();
|
||||||
|
if (entry->device.dataValid && (now - entry->device.lastUpdate) < minIntervalMs) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (debugEnabled) Serial.printf("[VictronBLE] Processing: %s nonce:0x%04X\n",
|
||||||
|
entry->device.name, mfgData.nonceDataCounter);
|
||||||
|
|
||||||
if (parseAdvertisement(entry, mfgData)) {
|
if (parseAdvertisement(entry, mfgData)) {
|
||||||
|
entry->lastNonce = mfgData.nonceDataCounter;
|
||||||
entry->device.rssi = advertisedDevice.getRSSI();
|
entry->device.rssi = advertisedDevice.getRSSI();
|
||||||
entry->device.lastUpdate = millis();
|
entry->device.lastUpdate = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -204,6 +204,7 @@ public:
|
|||||||
VictronDeviceType type = DEVICE_TYPE_UNKNOWN);
|
VictronDeviceType type = DEVICE_TYPE_UNKNOWN);
|
||||||
void setCallback(VictronCallback cb) { callback = cb; }
|
void setCallback(VictronCallback cb) { callback = cb; }
|
||||||
void setDebug(bool enable) { debugEnabled = enable; }
|
void setDebug(bool enable) { debugEnabled = enable; }
|
||||||
|
void setMinInterval(uint32_t ms) { minIntervalMs = ms; }
|
||||||
size_t getDeviceCount() const { return deviceCount; }
|
size_t getDeviceCount() const { return deviceCount; }
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
@@ -213,6 +214,7 @@ private:
|
|||||||
struct DeviceEntry {
|
struct DeviceEntry {
|
||||||
VictronDevice device;
|
VictronDevice device;
|
||||||
uint8_t key[16];
|
uint8_t key[16];
|
||||||
|
uint16_t lastNonce;
|
||||||
bool active;
|
bool active;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -223,6 +225,7 @@ private:
|
|||||||
VictronCallback callback;
|
VictronCallback callback;
|
||||||
bool debugEnabled;
|
bool debugEnabled;
|
||||||
uint32_t scanDuration;
|
uint32_t scanDuration;
|
||||||
|
uint32_t minIntervalMs;
|
||||||
bool initialized;
|
bool initialized;
|
||||||
|
|
||||||
static bool hexToBytes(const char* hex, uint8_t* out, size_t len);
|
static bool hexToBytes(const char* hex, uint8_t* out, size_t len);
|
||||||
|
|||||||
Reference in New Issue
Block a user