Hanglamp/src/Led.cpp
2025-04-21 14:40:20 +02:00

167 lines
3.5 KiB
C++

#include "Led.h"
CRGB led;
CRGB source;
CRGB target;
#ifdef LEVEL_SHIFT_WS2812B
#define SKIP_PIXELS 1
#else
#define SKIP_PIXELS 0
#endif
#define NUM_LEDS STRIP_LENGTH + SKIP_PIXELS
// Add one black pixel to the front
CRGB strip1[NUM_LEDS];
CRGB strip2[NUM_LEDS];
int fadeStart = 0;
#ifdef LED_NEOPIXEL_BUS
NeoPixelBus<NeoGrbFeature, NeoWs2812xMethod> strip(STRIP_LENGTH, PIN_STRIP1);
#endif
void fadeTo(const CRGB & dest){
Serial.println("fadeTask is not implemented");
return;
char buffer[80];
if(led.r == dest.r && led.g == dest.g && led.b == dest.b){
return;
}
snprintf(buffer, sizeof(buffer), "Have a new target: 0x%02X%02X%02X (old color 0x%02X%02X%02X)\n", dest.r, dest.g, dest.b, led.r, led.g, led.b);
Serial.println(buffer);
source = led;
target = dest;
fadeStart = millis();
}
void displayLed(){
#ifdef LED_FASTLED
for(uint8_t index = SKIP_PIXELS; index < NUM_LEDS; index++){
// Serial.printf("Led 1:%02d 0x%02x%02x%02x\n", index, strip1[index].r, strip1[index].g, strip1[index].b);
}
for(uint8_t index = SKIP_PIXELS; index < NUM_LEDS; index++){
// Serial.printf("Led 2:%02d 0x%02x%02x%02x\n", index, strip2[index].r, strip2[index].g, strip2[index].b);
}
auto before = micros();
FastLED.show();
auto after = micros();
Serial.printf("FastLED.show took %d micros\n", after - before);
#endif
#ifdef LED_NEOPIXEL_BUS
for(uint8_t index = 0; index < STRIP_LENGTH; index++){
RgbColor color;
color.R = strip1[index].r;
color.G = strip1[index].g;
color.B = strip1[index].b;
strip.SetPixelColor(index, color);
}
strip.Show();
#endif
}
void jumpTo(const CRGB & dest){
// auto before = micros64();
// FastLED.showColor(dest);
// auto after = micros64();
// Serial.printf("FastLED.showColor took %d micros\n", after - before);
// return;
for(uint8_t index = SKIP_PIXELS; index < NUM_LEDS; index++){
strip1[index] = CRGB(dest);
strip2[index] = CRGB(dest);
}
Serial.printf("Set color to 0x%02x%02x%02x\n", dest.r, dest.g, dest.b);
displayLed();
}
void fadeTask(){
Serial.println("fadeTask is not implemented");
return;
if(fadeStart == 0){
return;
}
float fadeInMillis = millis() - fadeStart;
float progress = (fadeInMillis / 1000) / FADE_PERIOD;
fract8 fract = progress * 256;
if(progress >= 1.0f){
led = target;
source = target;
fadeStart = 0;
}else{
led = source.lerp8(target, fract);
displayLed();
}
}
void Led_setup(){
pinMode(PIN_STRIP1, OUTPUT);
pinMode(PIN_STRIP2, OUTPUT);
// analogWriteRange(255);
#ifdef LED_FASTLED
FastLED.addLeds<WS2812B, PIN_STRIP1, BRG>(strip2, NUM_LEDS).setRgbw(RgbwDefault());
FastLED.addLeds<WS2812B, PIN_STRIP2, BRG>(strip2, NUM_LEDS).setRgbw(RgbwDefault());
strip1[0] = CRGB::Black;
strip2[0] = CRGB::Black;
FastLED.show();
#endif
#ifdef LED_NEOPIXEL_BUS
strip.Begin();
#endif
}
void Led_loop(){
strip2[0] = CRGB(255, 0, 0);
FastLED.show();
delay(500);
strip2[1] = CRGB(0, 255, 0);
FastLED.show();
delay(500);
strip2[2] = CRGB(0, 0, 255);
FastLED.show();
delay(500);
strip2[5] = CRGB(150, 0, 255);
FastLED.show();
delay(500);
strip2[9] = CRGB(255, 200, 20);
FastLED.show();
delay(500);
strip2[14] = CRGB(85, 60, 180);
FastLED.show();
delay(500);
strip2[15] = CRGB(50, 255, 20);
FastLED.show();
delay(500);
}
void Led_status(CRGB color){
strip1[0] = color;
strip2[0] = color;
FastLED.show();
}