Files
btproximitylights/btcontrol.ino
dragonchaser 2bd89fcade code cleanup and refactoring
Signed-off-by: dragonchaser <christian@boltares.de>
2025-07-11 15:03:33 +02:00

211 lines
6.0 KiB
C++

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <FastLED.h>
#include <TimerEvent.h>
#include <mutex>
// Methods
CRGB BlendCRGB(CRGB a, CRGB b, uint8_t blendAmount);
uint64_t StrToHex(const char* str);
void CleanDatabase();
void FillLEDsFromPaletteColors( uint8_t colorIndex);
void UpdatePalette();
// Bluetooth configuration
#define BLE_ACTIVE_SCAN false
#define BLE_SCAN_INTERVAL 100
#define BLE_SCAN_TIME 1 // in seconds
#define BLE_WINDOW 99 // less or equal setInterval value
// LED configuration
#define LED_TYPE WS2812B
#define DATA_PIN 1
#define NUM_LEDS 7
#define COLOR_ORDER RGB
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 24
// DATA maintenance configuration
#define UPDATE_INTERVAL 10000 // how often do we update de database?
#define MAX_VANISH_COUNTER 3 // if updateinterval is 10000 => 30s
#define MAX_MONITOR_DEVICES 10
// VARIABLES
BLEScan *pBLEScan;
CRGB leds[NUM_LEDS];
CRGBPalette256 pal;
std::map<String,uint8_t> deviceDatabase;
std::mutex databaseMutex;
std::mutex blendMutex;
volatile uint8_t startindex;
volatile uint8_t stepSegments;
TimerEvent updateTimer;
class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
String addr = advertisedDevice.getAddress().toString();
String color = String(addr.substring(9,11)+addr.substring(12,14)+addr.substring(15,17));
color.toUpperCase();
if (deviceDatabase.contains(color) || deviceDatabase.size() + 1 < MAX_MONITOR_DEVICES) {
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());
UpdatePalette();
}
};
void setup() {
Serial.begin(115200);
UpdatePalette();
FastLED.setBrightness(BRIGHTNESS);
FastLED.delay(1000/FRAMES_PER_SECOND);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
fill_solid(leds, NUM_LEDS, CRGB::Red );
FastLED.show();
FastLED.delay(1000);
fill_solid(leds, NUM_LEDS, CRGB::Green );
FastLED.show();
FastLED.delay(1000);
fill_solid(leds, NUM_LEDS, CRGB::Blue );
FastLED.show();
FastLED.delay(1000);
fill_solid(leds, NUM_LEDS, CRGB::Black );
FastLED.show();
startindex = 0;
updateTimer.set(UPDATE_INTERVAL, CleanDatabase);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(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() {
updateTimer.update();
{
std::lock_guard<std::mutex> lock(databaseMutex);
BLEScanResults *foundDevices = pBLEScan->start(BLE_SCAN_TIME, false);
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
}
FillLEDsFromPaletteColors(startindex++);
FastLED.show();
FastLED.delay(1000/FRAMES_PER_SECOND);
}
// Blends two CRGB colors using a uint8_t blend amount (0-255)
CRGB BlendCRGB(CRGB a, CRGB b, uint8_t blendAmount) {
CRGB result;
result.r = lerp8by8(a.r, b.r, blendAmount);
result.g = lerp8by8(a.g, b.g, blendAmount);
result.b = lerp8by8(a.b, b.b, blendAmount);
return result;
}
// Converts a string to a 64bit HEX value
uint64_t StrToHex(const char* str) {
return (uint64_t) strtoull(str, 0, 16);
}
// Cleans the database, updates VANISH_TICKERS
void CleanDatabase() {
std::lock_guard<std::mutex> lock(databaseMutex);
std::vector<String> keys;
for (auto &itr : deviceDatabase) {
Serial.printf("Decrement: %s -> %d\n", itr.first, itr.second);
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);
}
if (!keys.empty()) {
// there are changes to the database, we need to update the palette
UpdatePalette();
}
}
// Renderes Colors from the calculated palette to the led strip
void FillLEDsFromPaletteColors( uint8_t colorIndex) {
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; ++i) {
leds[i] = ColorFromPalette( pal, colorIndex, brightness, LINEARBLEND);
colorIndex += stepSegments;
}
}
// Updates the palette with gradients calculated from the lower 24 bits of the btle address
void UpdatePalette() {
std::lock_guard<std::mutex> lock(blendMutex);
uint8_t numColors = 2;
String colors[MAX_MONITOR_DEVICES];
switch (deviceDatabase.size()) {
case 0:
numColors = 2;
colors[0] = "000000";
colors[1] = "000000";
case 1:
numColors = 2;
colors[0] = deviceDatabase.begin()->first.substring(2,8);
colors[1] = deviceDatabase.begin()->first.substring(2,8);
break;
default:
numColors = deviceDatabase.size();
int pos = 0 ;
for (auto itr: deviceDatabase) {
colors[pos++] = itr.first.substring(2,8);
}
break;
}
uint8_t segmentSize = 255 / (numColors - 1); // Divide the palette into segments
if(numColors < 1) {
Serial.println("Less than 2 colors? Something is fucky?");
}
for (uint8_t i = 0; i < numColors - 1; i++) {
CRGB startColor = CRGB(StrToHex(colors[i].c_str()));
CRGB endColor = CRGB(StrToHex(colors[i + 1].c_str()));
for (uint8_t j = 0; j < segmentSize; j++) {
uint16_t index = i * segmentSize + j;
if (index >= 256) break;
uint8_t blendAmount = (j * 255) / segmentSize; // Integer blend factor
pal[index] = BlendCRGB(startColor, endColor, blendAmount);
if (index % 16 == 0) yield(); // Prevent watchdog reset
}
}
stepSegments = segmentSize;
// Make sure the last color slot is correctly set
pal[255] = CRGB(StrToHex(colors[numColors - 1].c_str()));
}