diff --git a/btcontrol.ino b/btcontrol.ino index 32589fc..f541492 100644 --- a/btcontrol.ino +++ b/btcontrol.ino @@ -29,14 +29,18 @@ void UpdatePalette(); #define LED_COLOR_ORDER RGB #define LED_BRIGHTNESS 96 -#define LED_FRAMES_PER_SECOND 24 +#define LED_FRAMES_PER_SECOND 144 #define LED_SETUP_DISPLAY_DELAY 500 // 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 +// Number of devices that can be monitored (might cause a reset because of memory oversaturation!) +#define MAX_MONITOR_DEVICES 5 +// Minimum transmit power the device needs to be recognice (this defines the min proximity needed for the device to be used) +#define MIN_RSSI_POWER -50 + // VARIABLES @@ -57,14 +61,16 @@ 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; + if (advertisedDevice.getRSSI() < MIN_RSSI_POWER) { + 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(); } - Serial.printf("Address: %s RSSI: %d TX Power: %d Calculated color: %s \n", addr.c_str(), advertisedDevice.getRSSI(), advertisedDevice.getTXPower(), color.c_str()); - UpdatePalette(); } }; @@ -153,17 +159,38 @@ void CleanDatabase() { } // Renderes Colors from the calculated palette to the led strip -void FillLEDsFromPaletteColors( uint8_t colorIndex) { - uint8_t brightness = 255; - +void FillLEDsFromPaletteColors( uint8_t colorIndex) { for( int i = 0; i < LED_NUM_LEDS; ++i) { - leds[i] = ColorFromPalette( pal, colorIndex, brightness, LINEARBLEND); - colorIndex += stepSegments; + leds[i] = pal[i+colorIndex]; } } -// Updates the palette with gradients calculated from the lower 24 bits of the btle address +// Renderes Colors from the calculated palette to the led strip +void FillLEDsFromPaletteColorsOld( uint8_t colorIndex) { + for( int i = 0; i < LED_NUM_LEDS; ++i) { + leds[i] = ColorFromPalette( pal, colorIndex, LED_BRIGHTNESS, LINEARBLEND); + colorIndex += stepSegments; + } +} + +// Updates the palette using module to create a repeated fillpatern of all detected devices using the lower 24bits of the btle address void UpdatePalette() { + std::lock_guard lock(blendMutex); + String colors[MAX_MONITOR_DEVICES]; + int numColors = deviceDatabase.size(); + int pos = 0 ; + for (auto itr: deviceDatabase) { + colors[pos++] = itr.first; + } + if (deviceDatabase.size() > 0 ) { + for (int i=0; i<256; i++) { + pal[i] = CRGB(StrToHex(colors[ i % deviceDatabase.size() ].c_str())); + } + } +} + +// Updates the palette with gradients calculated from the lower 24 bits of the btle address +void UpdatePaletteOld() { std::lock_guard lock(blendMutex); uint8_t numColors = 2; String colors[MAX_MONITOR_DEVICES]; @@ -174,14 +201,14 @@ void UpdatePalette() { colors[1] = "000000"; case 1: numColors = 2; - colors[0] = deviceDatabase.begin()->first.substring(2,8); - colors[1] = deviceDatabase.begin()->first.substring(2,8); + colors[0] = deviceDatabase.begin()->first; + colors[1] = deviceDatabase.begin()->first; break; default: numColors = deviceDatabase.size(); int pos = 0 ; for (auto itr: deviceDatabase) { - colors[pos++] = itr.first.substring(2,8); + colors[pos++] = itr.first; } break; } @@ -194,7 +221,7 @@ void UpdatePalette() { 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++) { + for (uint8_t j = 0; j < segmentSize; j++) { uint16_t index = i * segmentSize + j; if (index >= 256) break;