keypatch/src/NeoPixelHandler.ino
2025-08-31 17:37:14 +02:00

48 lines
1.9 KiB
C++

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel* pixels = nullptr; // Zeiger, um flexible Anzahl von LEDs zu erlauben
void NeoPixel_init(uint8_t numPixels) {
if (pixels) delete pixels; // falls schon initialisiert
pixels = new Adafruit_NeoPixel(numPixels, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
pixels->begin();
pixels->setBrightness(BRIGHTNESS);
pixels->show(); // Alle Pixel aus
}
void NeoPixel_setState(uint8_t ledIndex, uint8_t state) {
Serial.print("LED Update ");
Serial.print(ledIndex);
Serial.print(" new State ");
Serial.println(state);
if (!pixels) return; // Schutz, falls init noch nicht erfolgt
if (!state) {
pixels->setPixelColor(ledIndex, pixels->Color(0, 150, 0)); // grün = HIGH
} else {
pixels->setPixelColor(ledIndex, pixels->Color(150, 0, 0)); // rot = LOW
}
pixels->show();
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this loop:
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
// strip.rainbow() can take a single argument (first pixel hue) or
// optionally a few extras: number of rainbow repetitions (default 1),
// saturation and value (brightness) (both 0-255, similar to the
// ColorHSV() function, default 255), and a true/false flag for whether
// to apply gamma correction to provide 'truer' colors (default true).
pixels->rainbow(firstPixelHue);
// Above line is equivalent to:
// strip.rainbow(firstPixelHue, 1, 255, 255, true);
pixels->show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}