Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cycle button triggered on boot for some devices #19

Open
thijstriemstra opened this issue Apr 5, 2024 · 5 comments
Open

Cycle button triggered on boot for some devices #19

thijstriemstra opened this issue Apr 5, 2024 · 5 comments
Labels

Comments

@thijstriemstra
Copy link
Contributor

thijstriemstra commented Apr 5, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

thijstriemstra commented Apr 5, 2024

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
Copy link
Contributor Author

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
Copy link
Owner

srwi commented Apr 7, 2024

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.

@srwi srwi changed the title Incorrect startup animation Cycle button triggered on boot for some devices Apr 7, 2024
@srwi srwi added the bug label Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants