commit 6a05e7e00bdb89f742dc13ecdaa0e8b487464d33 Author: dragonchaser Date: Tue Jul 8 17:06:03 2025 +0200 initial commit Signed-off-by: dragonchaser diff --git a/btcontrol.ino b/btcontrol.ino new file mode 100644 index 0000000..b8178c5 --- /dev/null +++ b/btcontrol.ino @@ -0,0 +1,104 @@ +#include + +#include +#include + +#include + +#include + +#define BLE_ACTIVE_SCAN false +#define BLE_SCAN_INTERVAL 100 +#define BLE_WINDOW 99 // less or equal setInterval value + +#define NUM_LEDS 7 +#define DATA_PIN 1 + +#define BRIGHTNESS 96 +#define FRAMES_PER_SECOND 24 + +#define MAX_VANISH_COUNTER 50 + +CRGB leds[NUM_LEDS]; + +int scanTime = 1; //In seconds + +BLEScan *pBLEScan; + +std::map deviceDatabase; + +std::mutex callBackMutex; + +class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { + void onResult(BLEAdvertisedDevice advertisedDevice) { + String addr = advertisedDevice.getAddress().toString(); + String color = String("0x"+addr.substring(9,11)+addr.substring(12,14)+addr.substring(15,17)); + color.toUpperCase(); + color[1] = 'x'; + deviceDatabase[color] = MAX_VANISH_COUNTER; + Serial.printf("Address: %s RSSI: %d TX Power: %d Calculated color: %s \n", addr.c_str(), advertisedDevice.getRSSI(), advertisedDevice.getTXPower(), color.c_str()); + for(int i=0; i keys; + for (auto &itr : deviceDatabase) { + if (color != itr.first) { + itr.second--; + } + if(itr.second == 0) { + keys.push_back(itr.first); + } + } + for (const auto &itr : keys) { + deviceDatabase.erase(itr); + Serial.printf("Have not seen %s for %d ticks, removing!\n", itr, MAX_VANISH_COUNTER); + } + } +}; + +void setup() { + FastLED.delay(1000/FRAMES_PER_SECOND); + FastLED.addLeds(leds, NUM_LEDS); + for(int i=0; isetAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks()); + pBLEScan->setActiveScan(BLE_ACTIVE_SCAN); //active scan uses more power, but get results faster + pBLEScan->setInterval(BLE_SCAN_INTERVAL); + pBLEScan->setWindow(BLE_WINDOW); // less or equal setInterval value +} + +void loop() { + { + std::lock_guard lck(callBackMutex); + + BLEScanResults *foundDevices = pBLEScan->start(scanTime, false); + pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory + } + FastLED.show(); + FastLED.delay(1000/FRAMES_PER_SECOND); +} \ No newline at end of file