fix: neopattern example wdt reset

This commit is contained in:
2025-10-18 13:49:39 +02:00
parent b404852fc7
commit ce70830678
3 changed files with 47 additions and 45 deletions

View File

@@ -4,7 +4,6 @@
NeoPattern::NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)(int)) NeoPattern::NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)(int))
: Adafruit_NeoPixel(pixels, pin, type) : Adafruit_NeoPixel(pixels, pin, type)
{ {
frameBuffer = (uint8_t *)malloc(768);
OnComplete = callback; OnComplete = callback;
TotalSteps = numPixels(); TotalSteps = numPixels();
begin(); begin();
@@ -13,40 +12,21 @@ NeoPattern::NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type, void (*callba
NeoPattern::NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type) NeoPattern::NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type)
: Adafruit_NeoPixel(pixels, pin, type) : Adafruit_NeoPixel(pixels, pin, type)
{ {
frameBuffer = (uint8_t *)malloc(768);
TotalSteps = numPixels(); TotalSteps = numPixels();
begin(); begin();
} }
NeoPattern::~NeoPattern() { NeoPattern::~NeoPattern() {
if (frameBuffer) { // No frameBuffer to clean up
free(frameBuffer);
}
} }
void NeoPattern::handleStream(uint8_t *data, size_t len) // Removed unused handleStream and drawFrameBuffer functions
{
//const uint16_t *data16 = (uint16_t *)data;
bufferSize = len;
memcpy(frameBuffer, data, len);
}
void NeoPattern::drawFrameBuffer(int w, uint8_t *frame, int length)
{
for (int i = 0; i < length; i++)
{
uint8_t r = frame[i];
uint8_t g = frame[i + 1];
uint8_t b = frame[i + 2];
setPixelColor(i, r, g, b);
}
}
void NeoPattern::onCompleteDefault(int pixels) void NeoPattern::onCompleteDefault(int pixels)
{ {
//Serial.println("onCompleteDefault"); //Serial.println("onCompleteDefault");
// FIXME no specific code // FIXME no specific code
if (ActivePattern == THEATER_CHASE) if (ActivePattern == THEATER_CHASE || ActivePattern == RAINBOW_CYCLE)
{ {
return; return;
} }
@@ -127,7 +107,12 @@ void NeoPattern::RainbowCycleUpdate()
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255)); setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
} }
show(); show();
Increment(); // RainbowCycle is continuous, just increment Index
Index++;
if (Index >= 255)
{
Index = 0;
}
} }
// Initialize for a Theater Chase // Initialize for a Theater Chase

View File

@@ -52,17 +52,12 @@ class NeoPattern : public Adafruit_NeoPixel
// Callback on completion of pattern // Callback on completion of pattern
void (*OnComplete)(int); void (*OnComplete)(int);
uint8_t *frameBuffer;
int bufferSize = 0;
// Constructor - calls base-class constructor to initialize strip // Constructor - calls base-class constructor to initialize strip
NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)(int)); NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)(int));
NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type); NeoPattern(uint16_t pixels, uint8_t pin, uint8_t type);
~NeoPattern(); ~NeoPattern();
// Stream handling // Stream handling functions removed
void handleStream(uint8_t *data, size_t len);
void drawFrameBuffer(int w, uint8_t *frame, int length);
// Pattern completion // Pattern completion
void onCompleteDefault(int pixels); void onCompleteDefault(int pixels);

View File

@@ -340,8 +340,30 @@ void NeoPatternService::setPattern(NeoPatternType pattern) {
neoPattern->ActivePattern = static_cast<::pattern>(pattern); neoPattern->ActivePattern = static_cast<::pattern>(pattern);
resetStateForPattern(pattern); resetStateForPattern(pattern);
// Initialize the pattern using the registry // Set up pattern-specific parameters
patternRegistry.initializePattern(static_cast<uint8_t>(pattern)); switch (pattern) {
case NeoPatternType::RAINBOW_CYCLE:
neoPattern->RainbowCycle(updateIntervalMs, static_cast<::direction>(direction));
break;
case NeoPatternType::THEATER_CHASE:
neoPattern->TheaterChase(currentState.color, currentState.color2, updateIntervalMs, static_cast<::direction>(direction));
break;
case NeoPatternType::COLOR_WIPE:
neoPattern->ColorWipe(currentState.color, updateIntervalMs, static_cast<::direction>(direction));
break;
case NeoPatternType::SCANNER:
neoPattern->Scanner(currentState.color, updateIntervalMs);
break;
case NeoPatternType::FADE:
neoPattern->Fade(currentState.color, currentState.color2, currentState.totalSteps, updateIntervalMs, static_cast<::direction>(direction));
break;
case NeoPatternType::FIRE:
// Fire pattern doesn't need setup
break;
case NeoPatternType::NONE:
// None pattern doesn't need setup
break;
}
} }
void NeoPatternService::setPatternByName(const String& name) { void NeoPatternService::setPatternByName(const String& name) {
@@ -425,7 +447,7 @@ void NeoPatternService::registerPatterns() {
"rainbow_cycle", "rainbow_cycle",
static_cast<uint8_t>(NeoPatternType::RAINBOW_CYCLE), static_cast<uint8_t>(NeoPatternType::RAINBOW_CYCLE),
"Rainbow cycle pattern", "Rainbow cycle pattern",
[this]() { neoPattern->RainbowCycle(updateIntervalMs, static_cast<::direction>(direction)); }, nullptr, // No initializer needed, state is set up in setPattern
[this]() { updateRainbowCycle(); }, [this]() { updateRainbowCycle(); },
false, // doesn't require color2 false, // doesn't require color2
true // supports direction true // supports direction
@@ -435,7 +457,7 @@ void NeoPatternService::registerPatterns() {
"theater_chase", "theater_chase",
static_cast<uint8_t>(NeoPatternType::THEATER_CHASE), static_cast<uint8_t>(NeoPatternType::THEATER_CHASE),
"Theater chase pattern", "Theater chase pattern",
[this]() { neoPattern->TheaterChase(currentState.color, currentState.color2, updateIntervalMs, static_cast<::direction>(direction)); }, nullptr, // No initializer needed, state is set up in setPattern
[this]() { updateTheaterChase(); }, [this]() { updateTheaterChase(); },
true, // requires color2 true, // requires color2
true // supports direction true // supports direction
@@ -445,7 +467,7 @@ void NeoPatternService::registerPatterns() {
"color_wipe", "color_wipe",
static_cast<uint8_t>(NeoPatternType::COLOR_WIPE), static_cast<uint8_t>(NeoPatternType::COLOR_WIPE),
"Color wipe pattern", "Color wipe pattern",
[this]() { neoPattern->ColorWipe(currentState.color, updateIntervalMs, static_cast<::direction>(direction)); }, nullptr, // No initializer needed, state is set up in setPattern
[this]() { updateColorWipe(); }, [this]() { updateColorWipe(); },
false, // doesn't require color2 false, // doesn't require color2
true // supports direction true // supports direction
@@ -455,7 +477,7 @@ void NeoPatternService::registerPatterns() {
"scanner", "scanner",
static_cast<uint8_t>(NeoPatternType::SCANNER), static_cast<uint8_t>(NeoPatternType::SCANNER),
"Scanner pattern", "Scanner pattern",
[this]() { neoPattern->Scanner(currentState.color, updateIntervalMs); }, nullptr, // No initializer needed, state is set up in setPattern
[this]() { updateScanner(); }, [this]() { updateScanner(); },
false, // doesn't require color2 false, // doesn't require color2
false // doesn't support direction false // doesn't support direction
@@ -465,7 +487,7 @@ void NeoPatternService::registerPatterns() {
"fade", "fade",
static_cast<uint8_t>(NeoPatternType::FADE), static_cast<uint8_t>(NeoPatternType::FADE),
"Fade pattern", "Fade pattern",
[this]() { neoPattern->Fade(currentState.color, currentState.color2, currentState.totalSteps, updateIntervalMs, static_cast<::direction>(direction)); }, nullptr, // No initializer needed, state is set up in setPattern
[this]() { updateFade(); }, [this]() { updateFade(); },
true, // requires color2 true, // requires color2
true // supports direction true // supports direction
@@ -475,7 +497,7 @@ void NeoPatternService::registerPatterns() {
"fire", "fire",
static_cast<uint8_t>(NeoPatternType::FIRE), static_cast<uint8_t>(NeoPatternType::FIRE),
"Fire effect pattern", "Fire effect pattern",
[this]() { neoPattern->Fire(50, 120); }, nullptr, // No initializer needed, state is set up in setPattern
[this]() { updateFire(); }, [this]() { updateFire(); },
false, // doesn't require color2 false, // doesn't require color2
false // doesn't support direction false // doesn't support direction
@@ -499,7 +521,7 @@ void NeoPatternService::resetStateForPattern(NeoPatternType pattern) {
neoPattern->Index = 0; neoPattern->Index = 0;
neoPattern->Direction = static_cast<::direction>(direction); neoPattern->Direction = static_cast<::direction>(direction);
neoPattern->completed = 0; neoPattern->completed = 0;
lastUpdateMs = 0; // Don't reset lastUpdateMs to 0, keep the current timing
} }
uint32_t NeoPatternService::parseColor(const String& colorStr) const { uint32_t NeoPatternService::parseColor(const String& colorStr) const {
@@ -538,9 +560,9 @@ String NeoPatternService::getPatternDescription(const String& name) const {
void NeoPatternService::update() { void NeoPatternService::update() {
if (!initialized) return; if (!initialized) return;
//unsigned long now = millis(); unsigned long now = millis();
//if (now - lastUpdateMs < updateIntervalMs) return; if (now - lastUpdateMs < updateIntervalMs) return;
//lastUpdateMs = now; lastUpdateMs = now;
// Use pattern registry to execute the current pattern // Use pattern registry to execute the current pattern
patternRegistry.executePattern(static_cast<uint8_t>(activePattern)); patternRegistry.executePattern(static_cast<uint8_t>(activePattern));