-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(🌈): add rainbow LED animation (#27)
- Loading branch information
Showing
3 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters