From b9c56702a052bbb18cdf8959daf25128ef536c71 Mon Sep 17 00:00:00 2001 From: dragonchaser Date: Mon, 14 Jul 2025 16:09:07 +0200 Subject: [PATCH] add support for vibration motor Signed-off-by: dragonchaser --- btcontrol.ino | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/btcontrol.ino b/btcontrol.ino index 633e8be..47c14c4 100644 --- a/btcontrol.ino +++ b/btcontrol.ino @@ -12,6 +12,7 @@ uint64_t StrToHex(const char* str); void CleanDatabase(); void FillLEDsFromPaletteColors( uint8_t colorIndex); void UpdatePalette(); +void Vibrate(); // Bluetooth configuration @@ -30,6 +31,12 @@ void UpdatePalette(); #define LED_FRAMES_PER_SECOND 24 #define LED_SETUP_DISPLAY_DELAY 500 +// VIBRATION MOTOR configuration +#define VIBRO_PIN 2 +#define VIBRO_REPEAT 3 +#define VIBRO_SMALL_DELAY 100 +#define VIBRO_BIG_DELAY 500 + // DATA maintenance configuration #define UPDATE_INTERVAL 10000 // how often do we update de database? @@ -49,18 +56,18 @@ const std::map blackList = { //{ "36E898", nullptr }, }; -#define PUBLIC_LAMP 1 -#define PRIVATE_LAMP 2 -#define AMULETT 3 +#define DEVICE_TYPE_PUBLIC_LAMP 1 +#define DEVICE_TYPE_PRIVATE_LAMP 2 +#define DEVICE_TYPE_AMULETT 3 -#define DEVICE_TYPE PUBLIC_LAMP +#define DEVICE_TYPE DEVICE_TYPE_AMULETT #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 +#elif DEVICE_TYPE == DEVICE_TYPE_PUBLIC_LAMP // This is a PUBLIC LAMP, no restrictions here // Does not have a vibration motor static const bool useVibro = false; @@ -68,7 +75,7 @@ const std::map blackList = { // IF THIS LIST IS EMPTY IT WILL NOT BE USED static const std::map whiteList = {}; -#elif DEVICE_TYPE == PRIVATE_LAMP +#elif DEVICE_TYPE == 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; @@ -78,7 +85,7 @@ const std::map blackList = { { "36E898", nullptr }, }; -#elif DEVICE_TYPE == AMULETT +#elif DEVICE_TYPE == 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; @@ -143,6 +150,15 @@ class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { color = it->second; } } + + // when the device is not in the database yet, vibrate on first connect + if (useVibro) { + auto it = deviceDatabase.find(color); + if (it != deviceDatabase.end()) { + Vibrate(); + } + } + if (deviceDatabase.contains(color) || deviceDatabase.size() + 1 < MAX_MONITOR_DEVICES) { deviceDatabase[color] = MAX_VANISH_COUNTER; } @@ -154,6 +170,10 @@ class AdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { void setup() { Serial.begin(115200); + if (useVibro) { + pinMode(VIBRO_PIN, OUTPUT); + } + UpdatePalette(); FastLED.setBrightness(LED_BRIGHTNESS); @@ -252,3 +272,16 @@ void UpdatePalette() { } } } + +// Causes the vibration motor to vibrate +void Vibrate() { + for (int i = 0; i < VIBRO_REPEAT; i++) { + for (int j = 0; j < 3; j++) { + delay(VIBRO_SMALL_DELAY); + digitalWrite(VIBRO_PIN, HIGH); + delay(VIBRO_SMALL_DELAY); + digitalWrite(VIBRO_PIN, LOW); + } + delay(VIBRO_BIG_DELAY); + } +}