// ***********************************************************
// 
// This shows a simple method to toggle 
// your debugging Serial.print() on and off 
// Note: See lines 32 to 40 for the code. 
//
// Created by leftCoast on github
// LC_baseTools
// mechButton_Pro.ino
// https://github.com/leftCoast/LC_baseTools/tree/master/examples/mechButton_Pro
// 
// Modified by Edoctoor Feb 3, 2022
//
// as I wanted to toggle my print lines off and on to 
// see how much memory serial prints takes up by 
// comparing compiles.
// In this project we are using debug() to toggle a star ship;
// as an example of how you can use debug() to toggle debugging Serial.prints on and off
// 
// More information on this can be found here, 
// STOP using debug in your Arduino code! THIS is better.
// https://youtu.be/--KxxMaiwSE
//
// ***********************************************************

#include <mechButton.h>
#include <idlers.h>

#define BUTTON_PIN1     2                                      // Pin we will hook the button to. The other side hooks to ground.
#define LED_PIN         13                                     // Usual pin number for built in LED.

#define DEBUG           1                                      // This toggles the debug() statements 1 for on, or 0 for off.

#if DEBUG == 1
#define debug(x)        Serial.print(x);                       // Use debug(x) to toggle Serial.print 
#define debugln(x)      Serial.println(x);
#else                                                          // Else the the compiler does not generate Serial.print statements
#define debug(x)
#define debugln(x)
#endif

// ***********************************************************
//
//                      proButton class definition
//
//    Inherit from mechButton and creae a class that does
//    your custom bidding.
//
// ***********************************************************


class proButton      :  public mechButton {

   public:
                        proButton(int inPin);
   virtual              ~proButton(void);

                  void  begin(void);     
                  void  blank(int numBlanks);
                  void  line(int numLines);
   virtual        void  takeAction(void);

};


// ***********************************************************
//
//                      proButton class code
//
// ***********************************************************


// Constructor, not much going on. Just passing the pin number in.
proButton::proButton(int inPin)
   :mechButton(inPin) {    }


// Destructor, nothing allocated so nothing to do.
proButton::~proButton(void)   {    }


// begin, Needed somewhere to place the hookup() call. begin is tradional.
void  proButton::begin(void)  { hookup(); }


// blank, print some blanks.
void proButton::blank(int numBlanks)   {

   for (int i=1;i<=numBlanks;i++)   {
      debug(' ');
   }
}


// line, give us some blank linkes.
void proButton::line(int numLines) {

   for (int i=0;i<numLines;i++)   {
      debugln();
   }
}


// tackAction, this is what is called when there is no callback set.
void proButton::takeAction(void) {

   if (setAs)  {
      blank(36);
      debugln(F("Live lone and prosper.\n"));
      if (DEBUG)  {
         Serial.print(F("edit #define DEBUG 0 to hide the star ship")); 
         Serial.println(F("=========================================="));
      } else   {
         Serial.print(F("edit #define DEBUG 1 to see the star ship")); 
         Serial.println(F("=========================================="));
      }
   } else {
      line(4);
      blank(10);debugln(F("                                     --------------------------===="));
      blank(10);debugln(F("            __                      ( |                         ==="));
      blank(10);debugln(F("         /------\\                    ---------------------------=="));
      blank(10);debugln(F("------------------------------           |  |"));
      blank(10);debugln(F("\\____________________________/]          |  |"));
      blank(10);debugln(F("         --------       \\     \\          |  |"));
      blank(10);debugln(F("                         \\     \\         |  |"));
      blank(10);debugln(F("                          \\ --------_____|  |__"));
      blank(10);debugln(F("                         | |              --  /"));
      blank(10);debugln(F("                        -||                  ||"));
      blank(10);debugln(F("                         | |__________/------== "));
      line(2);
   }
}


// ***********************************************************
//
//                      Sketch starts here
//
// ***********************************************************


proButton button1(BUTTON_PIN1);                                // Set button1 to pin 2

// Your standard sketch setup()
void setup()   {

   Serial.begin(115200);                                       // Fire up our serial monitor thing.
   pinMode(LED_PIN, OUTPUT);                                   // Set up the LED pin for OUTPUT.
   button1.begin();                                            // Fire up the button. (Calls hookup() for idling.)
}


// Your standard sketch loop()
void loop()    {
   bool  buttonState;

   idle();                                                     // Let all the idlers have time to do their thing.
   buttonState = button1.trueFalse();                          // Have a look at what the current button state is.
   digitalWrite(LED_PIN, !buttonState);                        // Since the button grounds when pushed, invert the logic with ! 
}