code cleanup and refactoring
Signed-off-by: dragonchaser <christian@boltares.de>
This commit is contained in:
168
btcontrol.ino
168
btcontrol.ino
@@ -1,50 +1,61 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include <BLEDevice.h>
|
#include <BLEDevice.h>
|
||||||
#include <BLEScan.h>
|
#include <BLEScan.h>
|
||||||
|
|
||||||
#include <FastLED.h>
|
#include <FastLED.h>
|
||||||
|
#include <TimerEvent.h>
|
||||||
|
|
||||||
#include <mutex>
|
#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_ACTIVE_SCAN false
|
||||||
#define BLE_SCAN_INTERVAL 100
|
#define BLE_SCAN_INTERVAL 100
|
||||||
|
#define BLE_SCAN_TIME 1 // in seconds
|
||||||
#define BLE_WINDOW 99 // less or equal setInterval value
|
#define BLE_WINDOW 99 // less or equal setInterval value
|
||||||
|
|
||||||
#define NUM_LEDS 7
|
// LED configuration
|
||||||
|
#define LED_TYPE WS2812B
|
||||||
#define DATA_PIN 1
|
#define DATA_PIN 1
|
||||||
|
#define NUM_LEDS 7
|
||||||
|
#define COLOR_ORDER RGB
|
||||||
|
|
||||||
#define BRIGHTNESS 96
|
#define BRIGHTNESS 96
|
||||||
#define FRAMES_PER_SECOND 24
|
#define FRAMES_PER_SECOND 24
|
||||||
|
|
||||||
#define MAX_VANISH_COUNTER 50
|
// DATA maintenance configuration
|
||||||
#define MAX_MONITOR_DEVICES 100
|
#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];
|
CRGB leds[NUM_LEDS];
|
||||||
|
|
||||||
CRGBPalette256 pal;
|
CRGBPalette256 pal;
|
||||||
|
|
||||||
int scanTime = 1; //In seconds
|
|
||||||
|
|
||||||
BLEScan *pBLEScan;
|
|
||||||
|
|
||||||
std::map<String,uint8_t> deviceDatabase;
|
std::map<String,uint8_t> deviceDatabase;
|
||||||
|
|
||||||
std::mutex callBackMutex;
|
std::mutex databaseMutex;
|
||||||
std::mutex blendMutex;
|
std::mutex blendMutex;
|
||||||
|
|
||||||
volatile uint8_t startindex;
|
volatile uint8_t startindex;
|
||||||
volatile uint8_t stepSegments;
|
volatile uint8_t stepSegments;
|
||||||
|
|
||||||
void updatePalette();
|
TimerEvent updateTimer;
|
||||||
uint64_t StrToHex(const char* str);
|
|
||||||
CRGB blendCRGB(CRGB a, CRGB b, uint8_t blendAmount);
|
|
||||||
void FillLEDsFromPaletteColors( uint8_t colorIndex);
|
|
||||||
|
|
||||||
class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
||||||
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
||||||
// TODO: switching of does not work because this function is only triggered when new devices are detected, ergo it will always be stuck with the last device
|
|
||||||
// the decrementing in line 58 needs to be moved to a seperate timebased function
|
|
||||||
String addr = advertisedDevice.getAddress().toString();
|
String addr = advertisedDevice.getAddress().toString();
|
||||||
String color = String(addr.substring(9,11)+addr.substring(12,14)+addr.substring(15,17));
|
String color = String(addr.substring(9,11)+addr.substring(12,14)+addr.substring(15,17));
|
||||||
color.toUpperCase();
|
color.toUpperCase();
|
||||||
@@ -52,55 +63,39 @@ class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
|||||||
deviceDatabase[color] = MAX_VANISH_COUNTER;
|
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());
|
Serial.printf("Address: %s RSSI: %d TX Power: %d Calculated color: %s \n", addr.c_str(), advertisedDevice.getRSSI(), advertisedDevice.getTXPower(), color.c_str());
|
||||||
std::vector<String> keys;
|
UpdatePalette();
|
||||||
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);
|
|
||||||
}
|
|
||||||
Serial.printf("Devicedatabase size: %d\n", deviceDatabase.size());
|
|
||||||
updatePalette();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
updatePalette();
|
|
||||||
FastLED.delay(1000/FRAMES_PER_SECOND);
|
|
||||||
FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS);
|
|
||||||
for(int i=0; i<NUM_LEDS;i++) {
|
|
||||||
leds[i] = CRGB::Red;
|
|
||||||
}
|
|
||||||
FastLED.show();
|
|
||||||
FastLED.delay(1000/FRAMES_PER_SECOND);
|
|
||||||
for(int i=0; i<NUM_LEDS;i++) {
|
|
||||||
leds[i] = CRGB::Blue;
|
|
||||||
}
|
|
||||||
FastLED.show();
|
|
||||||
FastLED.delay(1000/FRAMES_PER_SECOND);
|
|
||||||
for(int i=0; i<NUM_LEDS;i++) {
|
|
||||||
leds[i] = CRGB::Green;
|
|
||||||
}
|
|
||||||
FastLED.show();
|
|
||||||
FastLED.delay(1000/FRAMES_PER_SECOND);
|
|
||||||
for(int i=0; i<NUM_LEDS;i++) {
|
|
||||||
leds[i] = CRGB::Black;
|
|
||||||
}
|
|
||||||
FastLED.setBrightness(BRIGHTNESS);
|
|
||||||
FastLED.show();
|
|
||||||
|
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Scanning...");
|
|
||||||
|
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;
|
startindex = 0;
|
||||||
|
updateTimer.set(UPDATE_INTERVAL, CleanDatabase);
|
||||||
|
|
||||||
|
Serial.println("Scanning...");
|
||||||
BLEDevice::init("");
|
BLEDevice::init("");
|
||||||
pBLEScan = BLEDevice::getScan(); //create new scan
|
pBLEScan = BLEDevice::getScan(); //create new scan
|
||||||
pBLEScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks());
|
pBLEScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks());
|
||||||
@@ -110,10 +105,11 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
updateTimer.update();
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(callBackMutex);
|
std::lock_guard<std::mutex> lock(databaseMutex);
|
||||||
|
|
||||||
BLEScanResults *foundDevices = pBLEScan->start(scanTime, false);
|
BLEScanResults *foundDevices = pBLEScan->start(BLE_SCAN_TIME, false);
|
||||||
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
|
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
|
||||||
}
|
}
|
||||||
FillLEDsFromPaletteColors(startindex++);
|
FillLEDsFromPaletteColors(startindex++);
|
||||||
@@ -121,13 +117,43 @@ void loop() {
|
|||||||
FastLED.delay(1000/FRAMES_PER_SECOND);
|
FastLED.delay(1000/FRAMES_PER_SECOND);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t StrToHex(const char* str)
|
// 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);
|
return (uint64_t) strtoull(str, 0, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FillLEDsFromPaletteColors( uint8_t colorIndex)
|
// 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;
|
uint8_t brightness = 255;
|
||||||
|
|
||||||
for( int i = 0; i < NUM_LEDS; ++i) {
|
for( int i = 0; i < NUM_LEDS; ++i) {
|
||||||
@@ -136,16 +162,8 @@ void FillLEDsFromPaletteColors( uint8_t colorIndex)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blends two CRGB colors using a uint8_t blend amount (0-255)
|
// Updates the palette with gradients calculated from the lower 24 bits of the btle address
|
||||||
CRGB blendCRGB(CRGB a, CRGB b, uint8_t blendAmount) {
|
void UpdatePalette() {
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void updatePalette() {
|
|
||||||
std::lock_guard<std::mutex> lock(blendMutex);
|
std::lock_guard<std::mutex> lock(blendMutex);
|
||||||
uint8_t numColors = 2;
|
uint8_t numColors = 2;
|
||||||
String colors[MAX_MONITOR_DEVICES];
|
String colors[MAX_MONITOR_DEVICES];
|
||||||
@@ -181,7 +199,7 @@ void updatePalette() {
|
|||||||
if (index >= 256) break;
|
if (index >= 256) break;
|
||||||
|
|
||||||
uint8_t blendAmount = (j * 255) / segmentSize; // Integer blend factor
|
uint8_t blendAmount = (j * 255) / segmentSize; // Integer blend factor
|
||||||
pal[index] = blendCRGB(startColor, endColor, blendAmount);
|
pal[index] = BlendCRGB(startColor, endColor, blendAmount);
|
||||||
|
|
||||||
if (index % 16 == 0) yield(); // Prevent watchdog reset
|
if (index % 16 == 0) yield(); // Prevent watchdog reset
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user