srwi / FastLEDHub

Control multiple FastLED lightstrip animations on the ESP8266 and ESP32 without reuploading.
GNU Lesser General Public License v2.1
58 stars 3 forks source link

Cycle button triggered on boot for some devices #19

Open thijstriemstra opened 7 months ago

thijstriemstra commented 7 months ago

Program:

#include <Arduino.h>
#include <FastLEDHub.h>

#include "Animations/Confetti.h"
#include "Animations/Color.h"
#include "Animations/Gradient.h"
#include "Animations/Rainbow.h"
#include "Animations/RGBWave.h"

#define CYCLE_BTN_PIN 10

#define NUM_LEDS 13
#define LIGHTSTRIP_PIN 9
#define LED_TYPE WS2812B

CRGB leds[NUM_LEDS];

void setup() {
  FastLEDHub.initialize("Nintendo");
  FastLEDHub.addLeds<LED_TYPE, LIGHTSTRIP_PIN, GRB>(leds, NUM_LEDS);

  FastLEDHub.registerAnimation(new Color("Color"));
  FastLEDHub.registerAnimation(new Gradient("Gradient"));
  FastLEDHub.registerAnimation(new Rainbow("Rainbow"));
  FastLEDHub.registerAnimation(new RGBWave("RGB Wave"));
  FastLEDHub.registerAnimation(new Confetti("Confetti"));

  FastLEDHub.registerSlider(new Slider("Saturation", 0, 255, 255, 1, "palette2"));

  FastLEDHub.registerColorPicker(new ColorPicker("Primary Color", CRGB(255, 0, 0)));
  FastLEDHub.registerColorPicker(new ColorPicker("Secondary Color", CRGB(0, 255, 0)));

  // cycle button
  FastLEDHub.enableCycleButton(CYCLE_BTN_PIN);
}

void loop() {
  FastLEDHub.handle();
}

I have the following config:

{
  "timeZone": 0,
  "summerTime": 0,
  "longitude": 0,
  "latitude": 0,
  "alarmEnabled": false,
  "alarmHour": 0,
  "alarmMinute": 0,
  "alarmDuration": 1,
  "alarmAnimation": "",
  "postAlarmAnimation": "",
  "sunsetEnabled": false,
  "sunsetHour": 18,
  "sunsetMinute": 6,
  "sunsetDuration": 1,
  "sunsetOffset": 0,
  "sunsetAnimation": "",
  "startupAnimation": "Gradient",
  "sliderValues": [
    255,
    252,
    255
  ],
  "colorPickerValues": [
    "3080ff",
    "8cffc2"
  ]
}

And these animations:

afbeelding

but restarting the device plays the wrong animation (Rainbow instead of Gradient). Same happens for the other animations, it's always index + 1 it seems.

thijstriemstra commented 7 months ago

it looks like the currentAnimation isn't (re)stored correctly because currentAnimation is always index + 1 after a reboot, e.g.

 Received:  {

  "timeZone": 0,

  "summerTime": 0,

  "longitude": 0,

  "latitude": 0,

  "alarmEnabled": false,

  "alarmHour": 0,

  "alarmMinute": 0,

  "alarmDuration": 1,

  "alarmAnimation": "",

  "postAlarmAnimation": "",

  "sunsetEnabled": false,

  "sunsetHour": 18,

  "sunsetMinute": 6,

  "sunsetDuration": 1,

  "sunsetOffset": 0,

  "sunsetAnimation": "",

  "startupAnimation": "Color",

  "sliderValues": [

    255,

    252,

    255

  ],

  "colorPickerValues": [

    "ff002f",

    "8cffc2"

  ],

  "status": 2,

  "currentAnimation": "Rainbow",

  "animations": [

    "Color",

    "Rainbow",

    "RGB Wave"

  ],

  "sliders": [

    {

      "name": "Brightness",

      "min": 0,

      "max": 255,

      "step": 1,

      "value": 255,

      "icon": "brightness-high"

    },

    {

      "name": "Speed",

      "min": 0,

      "max": 255,

      "step": 1,

      "value": 252,

      "icon": "speedometer"

    },

    {

      "name": "Saturation",

      "min": 0,

      "max": 255,

      "step": 1,

      "value": 255,

      "icon": "palette2"

    }

  ],

  "colorPickers": [

    {

      "name": "Color",

      "value": "ff002f",

      "icon": "eyedropper"

    }

  ]

}
thijstriemstra commented 7 months ago

Looks like commenting out the cycle button fixes it:

// cycle button
//  FastLEDHub.enableCycleButton(CYCLE_BTN_PIN);

so it seems the button is triggered at startup..?

thijstriemstra commented 7 months ago

I'm using an ESP32-C3 (supermini) and looks like GPIO10 (CYCLE_BTN_PIN) is HIGH at boot: https://www.studiopieters.nl/esp32-c3-pinout/

I'll try a different pin.

Update: changing to pin 21 makes no difference

thijstriemstra commented 7 months ago

In the end, I decided to replace FastLEDHub.enableCycleButton(CYCLE_BTN_PIN); with a custom implementation (using AceButton) that seems to work fine calling FastLEDHub.cycle() when needed. It also gives me ability to implement extra features using long press, double-click etc.

#include <Arduino.h>
#include <AceButton.h>
#include <FastLEDHub.h>

#include "Animations/Color.h"
#include "Animations/Rainbow.h"
#include "Animations/RGBWave.h"

#define CYCLE_BTN_PIN 21

#define NUM_LEDS 13
#define LIGHTSTRIP_PIN 9
#define LED_TYPE WS2812B

CRGB leds[NUM_LEDS];
ace_button::AceButton cycle_btn(CYCLE_BTN_PIN);

class ButtonHandler: public ace_button::IEventHandler {
  public:
    void handleEvent(ace_button::AceButton* /* button */, uint8_t eventType,
        uint8_t buttonState) override {

      switch (eventType) {
        case ace_button::AceButton::kEventPressed:
          FastLEDHub.cycle();
          break;
      }
    }
};

ButtonHandler handleEvent;

void setup() {
  FastLEDHub.initialize("Nintendo");
  FastLEDHub.addLeds<LED_TYPE, LIGHTSTRIP_PIN, GRB>(leds, NUM_LEDS);

  FastLEDHub.registerAnimation(new Color("Color"));
  FastLEDHub.registerAnimation(new Rainbow("Rainbow"));
  FastLEDHub.registerAnimation(new RGBWave("RGB Wave"));

  FastLEDHub.registerSlider(new Slider("Saturation", 0, 255, 255, 1, "palette2"));

  FastLEDHub.registerColorPicker(new ColorPicker("Color", CRGB(255, 0, 0)));

  // cycle button
  pinMode(CYCLE_BTN_PIN, INPUT);
  ace_button::ButtonConfig* buttonConfig = cycle_btn.getButtonConfig();
  buttonConfig->setIEventHandler(&handleEvent);
}

void loop() {
  FastLEDHub.handle();

  // Should be called every 4-5ms or faster, for the default debouncing time
  // of ~20ms.
  cycle_btn.check();
}
srwi commented 7 months ago

Hi @thijstriemstra, thank you for raising the issue. Unfortunately, I was unable to reproduce the problem on the ESP8266 (Wemos D1 Mini) or my only ESP32 board, the Wemos Lolin32. The Lolin32 board also shows some pins as high on boot, but this doesn't appear to be causing the problem on my end.

It's possible that the GPIO state on boot needs better handling. Perhaps there's a clue in the AceButton library you mentioned. I'll take a look when I have some time.