make configuratable through defs, fix bug with switch off

Signed-off-by: dragonchaser <christian@boltares.de>
This commit is contained in:
dragonchaser
2025-07-14 14:56:22 +02:00
parent dfee5f4c49
commit 942c28add4

View File

@@ -7,8 +7,6 @@
#include <mutex> #include <mutex>
// Methods // Methods
CRGB BlendCRGB(CRGB a, CRGB b, uint8_t blendAmount);
uint64_t StrToHex(const char* str); uint64_t StrToHex(const char* str);
void CleanDatabase(); void CleanDatabase();
@@ -45,20 +43,55 @@ void UpdatePalette();
// CONSTS // CONSTS
const std::map<String, String> overrideColors = { const std::map<String, String> overrideColors = {
{"36E898", "FF0000"}, // MiiBand Klaas {"36E898", "FF0000"}, // MiiBand Klaas
{"0C873F", "00FF00"}, // Tester 1
{"D050CF", "0000FF"}, // Tester 2
}; };
const std::map<String, void*> blackList = { const std::map<String, void*> blackList = {
//{ "36E898", nullptr }, //{ "36E898", nullptr },
}; };
#define PUBLIC_LAMP 1
#define PRIVATE_LAMP 2
#define AMULETT 3
#define DEVICE_TYPE PUBLIC_LAMP
#ifndef DEVICE_TYPE
// WE DO NOT HAVE A DEVICETYPE, so we do not have any variables here
// to make sure the code compile fails
#error "DEVICE_TYPE is not defined"
#elif DEVICE_TYPE == PUBLIC_LAMP
// This is a PUBLIC LAMP, no restrictions here
// Does not have a vibration motor
static const bool useVibro = false;
// WARNING: IF THIS LIST CONTAINS > 0 VALUES, NO OTHER DEVICES WILL BE ALLOWED // WARNING: IF THIS LIST CONTAINS > 0 VALUES, NO OTHER DEVICES WILL BE ALLOWED
// IF THIS LIST IS EMPTY IT WILL NOT BE USED // IF THIS LIST IS EMPTY IT WILL NOT BE USED
const std::map<String, void*> whiteList = { static const std::map<String, void*> whiteList = {};
//{ "36E898", nullptr },
#elif DEVICE_TYPE == PRIVATE_LAMP
// This is a PRIVATE LAMP, restricted to a list of users
// Does not have a vibration motor
static const bool useVibro = false;
// WARNING: IF THIS LIST CONTAINS > 0 VALUES, NO OTHER DEVICES WILL BE ALLOWED
// IF THIS LIST IS EMPTY IT WILL NOT BE USED
static const std::map<String, void*> whiteList = {
{ "36E898", nullptr },
}; };
#elif DEVICE_TYPE == AMULETT
// This is an portable device, restricted to a list of users
// Comes with a vibration motor to alert wearer
static const bool useVibro = true;
// WARNING: IF THIS LIST CONTAINS > 0 VALUES, NO OTHER DEVICES WILL BE ALLOWED
// IF THIS LIST IS EMPTY IT WILL NOT BE USED
static const std::map<String, void*> whiteList = {
{ "36E898", nullptr },
};
#else
#error "Unknown DEVICE_TYPE"
#endif
// VARIABLES // VARIABLES
BLEScan *pBLEScan; BLEScan *pBLEScan;
@@ -167,15 +200,6 @@ void loop() {
FastLED.delay(1000/LED_FRAMES_PER_SECOND); FastLED.delay(1000/LED_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 // Converts a string to a 64bit HEX value
uint64_t StrToHex(const char* str) { uint64_t StrToHex(const char* str) {
return (uint64_t) strtoull(str, 0, 16); return (uint64_t) strtoull(str, 0, 16);
@@ -196,7 +220,7 @@ void CleanDatabase() {
deviceDatabase.erase(itr); deviceDatabase.erase(itr);
Serial.printf("Have not seen %s for %d ticks, removing!\n", itr, MAX_VANISH_COUNTER); Serial.printf("Have not seen %s for %d ticks, removing!\n", itr, MAX_VANISH_COUNTER);
} }
if (!keys.empty()) { if (!keys.empty() || deviceDatabase.size() < 1) {
// there are changes to the database, we need to update the palette // there are changes to the database, we need to update the palette
UpdatePalette(); UpdatePalette();
} }
@@ -212,13 +236,17 @@ void FillLEDsFromPaletteColors( uint8_t colorIndex) {
// Updates the palette using module to create a repeated fillpatern of all detected devices using the lower 24bits of the btle address // Updates the palette using module to create a repeated fillpatern of all detected devices using the lower 24bits of the btle address
void UpdatePalette() { void UpdatePalette() {
std::lock_guard<std::mutex> lock(blendMutex); std::lock_guard<std::mutex> lock(blendMutex);
String colors[MAX_MONITOR_DEVICES]; std::vector<String> colors;
int numColors = deviceDatabase.size(); int numColors = deviceDatabase.size();
int pos = 0 ; int pos = 0 ;
for (auto itr: deviceDatabase) { for (auto itr: deviceDatabase) {
colors[pos++] = itr.first; colors.push_back(itr.first);
} }
if (deviceDatabase.size() > 0 ) {
if (colors.empty()) {
fill_solid(pal.entries, 256, CRGB::Black);
} else {
for (int i=0; i<256; i++) { for (int i=0; i<256; i++) {
pal[i] = CRGB(StrToHex(colors[ i % deviceDatabase.size() ].c_str())); pal[i] = CRGB(StrToHex(colors[ i % deviceDatabase.size() ].c_str()));
} }