diff --git a/senseBox-bike-atrai-v2/src/led/LED.cpp b/senseBox-bike-atrai-v2/src/led/LED.cpp new file mode 100644 index 0000000..86e3ddc --- /dev/null +++ b/senseBox-bike-atrai-v2/src/led/LED.cpp @@ -0,0 +1,87 @@ +#include "LED.h" + +// Constructor +LED::LED(uint8_t pin, uint16_t numPixels, uint8_t brightness) + : pixels(numPixels, pin, NEO_GRB + NEO_KHZ800), hue(0), taskHandle(NULL) +{ + pixels.setBrightness(brightness); +} + +// Initialize the NeoPixel strip +void LED::begin() +{ + pixels.begin(); +} + +// Start the FreeRTOS task to update the LED color +void LED::startRainbow() +{ + // Create the LED update task + xTaskCreate( + LEDTask, // Task function + "LED Task", // Name of the task (for debugging) + 1024, // Stack size (in words) + this, // Task input parameter (this LED instance) + 1, // Priority of the task + &taskHandle // Task handle + ); +} + +// Stop the FreeRTOS task +void LED::stopRainbow() +{ + if (taskHandle != NULL) + { + vTaskDelete(taskHandle); + taskHandle = NULL; + } + // turn off the LED + pixels.clear(); + pixels.show(); +} + +// Task function to update the LED color +void LED::LEDTask(void *pvParameters) +{ + LED *ledInstance = static_cast(pvParameters); + + // Infinite loop to continuously update the LED color + while (true) + { + ledInstance->update(); + vTaskDelay(pdMS_TO_TICKS(5)); // Delay of 10 milliseconds + } +} + +// Update the LED color to the next hue +void LED::update() +{ + pixels.setPixelColor(0, Wheel(hue)); + pixels.show(); + + hue += 1; + if (hue > 255) + { + hue = 0; + } +} + +// Private function to generate rainbow colors across 0-255 positions +uint32_t LED::Wheel(byte WheelPos) +{ + WheelPos = 255 - WheelPos; + if (WheelPos < 85) + { + return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3); + } + else if (WheelPos < 170) + { + WheelPos -= 85; + return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3); + } + else + { + WheelPos -= 170; + return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0); + } +} diff --git a/senseBox-bike-atrai-v2/src/led/LED.h b/senseBox-bike-atrai-v2/src/led/LED.h new file mode 100644 index 0000000..07d82de --- /dev/null +++ b/senseBox-bike-atrai-v2/src/led/LED.h @@ -0,0 +1,25 @@ +#ifndef LED_H +#define LED_H + +#include +#include + +class LED +{ +public: + LED(uint8_t pin, uint16_t numPixels, uint8_t brightness = 50); + void begin(); + void startRainbow(); + void stopRainbow(); + +private: + uint32_t Wheel(byte WheelPos); + void update(); + static void LEDTask(void *pvParameters); + + Adafruit_NeoPixel pixels; + uint16_t hue; + TaskHandle_t taskHandle; +}; + +#endif diff --git a/senseBox-bike-atrai-v2/src/main.cpp b/senseBox-bike-atrai-v2/src/main.cpp index 07f9824..2b37e84 100644 --- a/senseBox-bike-atrai-v2/src/main.cpp +++ b/senseBox-bike-atrai-v2/src/main.cpp @@ -5,8 +5,7 @@ #include "sensors/AccelerationSensor/AccelerationSensor.h" #include "display/Display.h" #include "ble/BLEModule.h" - -#include "Adafruit_NeoPixel.h" +#include "led/LED.h" DustSensor dustSensor; TempHumiditySensor tempHumiditySensor; @@ -17,16 +16,16 @@ SBDisplay display; BLEModule bleModule; -Adafruit_NeoPixel rgb_led = Adafruit_NeoPixel(1, 1, NEO_GRB + NEO_KHZ800); +LED led(1, 1); void setup() { Serial.begin(115200); delay(1000); - rgb_led.begin(); + led.begin(); - rgb_led.setBrightness(30); + led.startRainbow(); SBDisplay::begin(); @@ -55,26 +54,6 @@ void setup() SBDisplay::showLoading("Start measurements...", 1); - // Subscribe to sensor measurements - // tempHumiditySensor.subscribe([](String uuid, std::vector values) { - // float temperature = values[0]; - // float humidity = values[1]; - // Serial.printf("TempHumiditySensor [%s]: %.2f, %.2f\n", uuid.c_str(), temperature, humidity); }); - - // dustSensor.subscribe([](String uuid) { - // Serial.printf("DustSensor [%s]\n", uuid.c_str()); - // }, "UUID-Dust"); - - // distanceSensor.subscribe([](std::vector values) - // { Serial.printf("DistanceSensor [%s]: %.2f\n", values[0]); }); - - // accelerationSensor.subscribe([](String uuid, std::vector values) - // { - // float x = values[0]; - // float y = values[1]; - // float z = values[2]; - // Serial.printf("Acceleration [%s]: %.2f, %.2f, %.2f\n", uuid.c_str(), x, y, z); }); - dustSensor.startSubscription(); tempHumiditySensor.startSubscription(); distanceSensor.startSubscription(); @@ -86,6 +65,8 @@ void setup() accelerationSensor.startBLE(); display.showConnectionScreen(); + + led.stopRainbow(); } void loop()