#include "TinyDebug.h"

/**
 * @file DoublableCallback.ino Example of toggling an LED using callback
 * functions instead of a clicker. When the button is single-clicked, the LED
 * will be illuminated. Double-click to turn the LED off.
 * 
 * @copyright Copyright (c) 2024 John Scott
 */
#include <AbleButtons.h>

// Identify which buttons you are using...
using Button     = AblePullupCallbackDoubleClickerButton;     ///< Using callback pull-up button.
using ButtonList = AblePullupCallbackDoubleClickerButtonList; ///< Using callback pull-up button list.

// Forward declaration of callback function.
void clickedCallback(Button::CALLBACK_EVENT, uint8_t);

#define BUTTON_PIN 2 ///< Connect button between this pin and ground.
Button btn(BUTTON_PIN, clickedCallback); ///< The button to check.

/**
 * Setup the ClickedBtn example. Called once to initialise everything.
 */
void setup() {
  btn.begin();
}

/**
 * Control the ClickedBtn example. Called repeatedly in a loop.
 */
void loop() {
  btn.handle();
}


//  enum CALLBACK_EVENT {
//    BEGIN_EVENT,          ///< The button's begin() method has completed.
//    PRESSED_EVENT,        ///< The button has been pressed.
//    RELEASED_EVENT,       ///< The button has been released.
//    HELD_EVENT,           ///< The button has been held down for a while.
//    IDLE_EVENT,           ///< The button has been idle (untouched) for a while.
//    SINGLE_CLICKED_EVENT, ///< The button has been clicked (pressed+released).
//    DOUBLE_CLICKED_EVENT  ///< The button has been double-clicked.
//  };


/**
 * Callback function for button released.
 * 
 * @param event The event that has occured.
 * @param id The identifier of the button generating the callback (ignored in this example).
 */
void clickedCallback(Button::CALLBACK_EVENT event, uint8_t id) {
  (void)id; // id is unused.

  Debug.print(millis());

  switch (event) {
       case Button::BEGIN_EVENT          :  Debug.println("begin"         );  break;
       case Button::PRESSED_EVENT        :  Debug.println("press"         );  break;
       case Button::RELEASED_EVENT       :  Debug.println("release"       );  break;
       case Button::HELD_EVENT           :  Debug.println("hold"          );  break;
       case Button::IDLE_EVENT           :  Debug.println("idle"          );  break;
       case Button::SINGLE_CLICKED_EVENT :  Debug.println("single click"  );  break;
       case Button::DOUBLE_CLICKED_EVENT :  Debug.println("double click"  );  break;
  }
}
ATTINY8520PU