Skip to content

Commit

Permalink
feat(🌈): add rainbow LED animation (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy authored Jun 10, 2024
1 parent 58092c6 commit 1af546e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 25 deletions.
87 changes: 87 additions & 0 deletions senseBox-bike-atrai-v2/src/led/LED.cpp
Original file line number Diff line number Diff line change
@@ -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<LED *>(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);
}
}
25 changes: 25 additions & 0 deletions senseBox-bike-atrai-v2/src/led/LED.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef LED_H
#define LED_H

#include <Adafruit_NeoPixel.h>
#include <Arduino.h>

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
31 changes: 6 additions & 25 deletions senseBox-bike-atrai-v2/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -55,26 +54,6 @@ void setup()

SBDisplay::showLoading("Start measurements...", 1);

// Subscribe to sensor measurements
// tempHumiditySensor.subscribe([](String uuid, std::vector<float> 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<float> values)
// { Serial.printf("DistanceSensor [%s]: %.2f\n", values[0]); });

// accelerationSensor.subscribe([](String uuid, std::vector<float> 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();
Expand All @@ -86,6 +65,8 @@ void setup()
accelerationSensor.startBLE();

display.showConnectionScreen();

led.stopRainbow();
}

void loop()
Expand Down

0 comments on commit 1af546e

Please sign in to comment.