#include <blinker.h>      // The base LC_baseTools blinker class.
#include <mechButton.h>   // The base LC_baseTools mechButton class.

#define DEF_HOLD_MS 1000  // How many ms we'll call a "long" button press.



// ***************************************************
// fancyBtn:
// 
// This is the class you need to add to your skech.
// Just copy and paste it into your .ino file and 
// It'll work. Just like it is here.
//
// ***************************************************


class fancyBtn :  public mechButton,
                  public timeObj {
  public:
          fancyBtn(int inPin);
  virtual ~fancyBtn(void);

          void  setLongCallback(void(*funct)(void));
  virtual void  idle(void);

          void		(*longCallback)(void);
};


fancyBtn::fancyBtn(int inPin)
  : mechButton(inPin),
  timeObj(DEF_HOLD_MS,false) { longCallback = NULL; }


fancyBtn::~fancyBtn(void) {  }


void fancyBtn::setLongCallback(void(*funct)(void)) {

  longCallback = funct;
  hookup();
}


void fancyBtn::idle(void) {
  
  bool  state;

  state = setAs;                    // Save the state we have now.
  trueFalse();                      // Do the mechButton chekcing state stuff.
  if (state!=setAs) {               // If there was a state change..
    if (!setAs) {                   // If button was pressed..
      start();                      // We start our timer.
    } else {                        // Else, the button was let up.
      reset();                      // Turn the timer off.                   
    }
  } else {                          // Else the button has not changed..
    if (!setAs) {                   // If the button is being held down..
      if (ding()&&longCallback) {   // We run out of time AND have a callback..
        longCallback();             // Call the callback.
        reset();                    // Turn the timer off.
      }
    }
  }
}


// ***************************************************
// END OF fancyBtn:
// 
// ***************************************************



// *************************************
//
//  testing.ino
//
// *************************************


blinker   greenBlinker(4);  // The thing that blinks in the background.
blinker   redBlinker(2);    // The other thing...
fancyBtn  theButton(5);     // Our custom long/short button class.

void setup() {

  theButton.setCallback(quickCallback);     // When the button gets clicked. Call this function.
  theButton.setLongCallback(holdCallback);  // If it's been held down for a "long" time call this.
}


// This is the function that is called on a quck click.
void quickCallback(void) {

  if (!theButton.trueFalse()) {     // If the button has been grounded..
    greenBlinker.setOnOff(true);    // Start blinking the green LED.
  } else {                          // Else, the button has been released..
    redBlinker.setOnOff(false);     // Turn OFF the red blinking LED.
    greenBlinker.setOnOff(false);   // Turn OFF the green blinking LED.
  }
}

// This is the function that is called on a long click.
void holdCallback(void) {

  redBlinker.setOnOff(true);  // Long press has happened. Turn ON the red blniking LED.
}


void loop() {

  idle();   // Run the magic that happens in the backgroud. (Buttons & blinking).
  delay(4); // ESP32 accelerator. Seriosly! Comment it out and see what happens.
}