#include "OneButton.h"
// enum Modes Mode;
OneButton mainButton;
// OneButton forwardbutton;
// OneButton Backbutton;
// OneButton button;
#define MAIN_BUTTON 17
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
#define PIN_LED 25
#include "buttonHelper.h"
int ledState = LOW;
// setup code here, to run once:
void setup() {
Serial.begin(115200);
Serial.println("One Button Example with polling.");
// enable the standard led on pin PIN_LED.
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
digitalWrite(PIN_LED, ledState);
// Set the GPIO pin 17 (MAIN_BUTTON) as a wake-up source
esp_sleep_enable_ext0_wakeup(GPIO_NUM_17, 0); // Wake up when button is pressed (LOW signal)
// setup OneButton
mainButton.setup(MAIN_BUTTON, INPUT_PULLUP, true);
// link the doubleclick function to be called on a doubleclick event.
mainButton.attachDoubleClick(toggleAltModes);
mainButton.attachClick(incrementMode);
mainButton.setLongPressIntervalMs(350);
mainButton.attachLongPressStart(toggleMachineState);
} // setup
// main code here, to run repeatedly:
void loop() {
// keep watching the push button:
mainButton.tick();
if (Mode == off) {
Serial.println("Entering deep sleep mode...");
delay(100); // Allow time for the message to be sent before sleeping
while(mainButton.isLongPressed());
esp_deep_sleep_start();
}
switch (Mode) {
case fm: blink(FM_MODE); break;
case player: blink(PLAYER_MODE); break;
case bluetooth: blink(BT_MODE); break;
case master: blink(WIFI_MASTER_MODE); break;
case slave: blink(WIFI_SLAVE_MODE); break;
case unused: blink(IDLE); break;
case hibernating: blink(HIBERNATING); break;
case off: blink(SHUTDOWN); break;
default: blink(0); break;
}
// You can implement other code in here or just wait a while
delay(10);
} // loop
// this function will be called when the button was clicked in a short timeframe.
void incrementMode() {
crementMode(Mode, 1);
Serial.println("The System Mode is now ");
Serial.println(Mode);
}
// this function will be called when the button was pressed 2 times in a short timeframe.
void toggleAltModes() {
Serial.println("x2");
ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // doubleClick
void toggleMachineState() {
if (Mode == off) {
Mode = fm; // Start with FM_MODE when turned on
} else {
Mode = off;
}
Serial.print("Machine state toggled. The System Mode is now ");
Serial.println(Mode);
}
void blink(unsigned char state) {
int period = 2000;
float duty = float(state) / 8.00;
float deRem = millis() % period;
if (deRem > 100 && deRem < (duty * period + 100)) {
digitalWrite(PIN_LED, HIGH);
} else {
digitalWrite(PIN_LED, LOW);
}
}