Publish keypad code and rolling log

This commit is contained in:
iranl
2024-05-30 19:36:23 +02:00
parent ff741f7989
commit 58ec4b345c
17 changed files with 213 additions and 131 deletions

View File

@@ -22,11 +22,14 @@ Scanner::Scanner(int reservedSubscribers) {
void Scanner::initialize(const std::string& deviceName, const bool wantDuplicates, const uint16_t interval, const uint16_t window) {
if (!BLEDevice::getInitialized()) {
if (wantDuplicates) {
// reduce memory footprint, cache is not used anyway
NimBLEDevice::setScanDuplicateCacheSize(10);
}
BLEDevice::init(deviceName);
}
bleScan = BLEDevice::getScan();
bleScan->setAdvertisedDeviceCallbacks(this, wantDuplicates);
bleScan->setActiveScan(true);
bleScan->setInterval(interval);
bleScan->setWindow(window);
}
@@ -36,15 +39,20 @@ void Scanner::update() {
return;
}
bleScan->clearResults();
if (scanDuration == 0) {
// Avoid unbridled growth of results vector
bleScan->setMaxResults(0);
} else {
log_w("Ble scanner max results not 0. Be aware of memory issue due to unbridled growth of results vector");
}
bool result = bleScan->start(scanDuration, nullptr, false);
if (!result) {
scanErrors++;
if (scanErrors % 100 == 0) {
log_w("BLE Scan error (100x)");
}
}
// if (!result) {
// scanErrors++;
// if (scanErrors % 100 == 0) {
// log_w("BLE Scan error (100x)");
// }
// }
}
void Scanner::enableScanning(bool enable) {

View File

@@ -16,6 +16,10 @@
#include <NimBLEDevice.h>
#include "BleInterfaces.h"
// Access to a globally available instance of BleScanner, created when first used
// Note that BLESCANNER.initialize() has to be called somewhere
#define BLESCANNER BleScanner::Scanner::instance()
namespace BleScanner {
class Scanner : public Publisher, BLEAdvertisedDeviceCallbacks {
@@ -23,6 +27,11 @@ class Scanner : public Publisher, BLEAdvertisedDeviceCallbacks {
Scanner(int reservedSubscribers = 10);
~Scanner() = default;
static Scanner& instance() {
static Scanner* scanner = new Scanner(); // only initialized once on first call
return *scanner;
}
/**
* @brief Initializes the BLE scanner
*
@@ -77,7 +86,7 @@ class Scanner : public Publisher, BLEAdvertisedDeviceCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
private:
uint32_t scanDuration = 3;
uint32_t scanDuration = 0; //default indefinite scanning time
BLEScan* bleScan = nullptr;
std::vector<Subscriber*> subscribers;
uint16_t scanErrors = 0;