#include <SevSeg.h>
#include <timeObj.h>
#include <mechButton.h>


// Click the button to start a timing sequence. This'll give you 3 seconds
// to get ready. Then, the LED comes on, and you have to click the button
// again as fast as possible to stop the timing.
//
// You will need to, using library manager in the IDE, install LC_baseTools
// to compile this.


#define LED_PIN   11


SevSeg      sevseg;
timeObj     startTimer(3000,false);
timeObj     timer(50,false);
mechButton  startStopBtn(10);
int         dispNumber = 0;
bool        timing;
bool        dec;

void setup() {
  
  byte digitPins[] = {};
  byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9};
  bool resistorsOnSegments = true;
  byte hardwareConfig = COMMON_CATHODE;
  sevseg.begin(hardwareConfig, 1, digitPins, segmentPins, true);
  sevseg.setBrightness(100);
  pinMode(LED_PIN, OUTPUT);
  startStopBtn.setCallback(btnClk);   // Hook the button to it's callback function.
  timing = false;                     // And no, we are not timing now.
}


// Called whenever one wants to start timing someone.
void startTiming(void) {
  
  dispNumber = 0;               // clear out the display number.
  dec = false;                  // Not showing a decimal point to begin with.
  timing = true;                // We are timing.
  timer.start();                // Start the timer for the count up.
  digitalWrite(LED_PIN,HIGH);   // Turn on the LED to tell the user she's being timed.
}


// Called when the user pushes the button when being timed.
void stopTiming(void) {
  
  timing = false;             // Stop the timing calls.
  digitalWrite(LED_PIN,LOW);  // Shut off the LED.
}


// Button has changed state, lets check it out!
void btnClk(void) {

  if (!startStopBtn.getState()) { // If it's been grounded..
    if (timing) {                 // If we are currently timing someone.
      stopTiming();               // Stop timing them.
    } else {                      // Else we are NOT timimng anyone?
      startTimer.start();         // Start up the starting timer.
    }
  }
}


void loop() {
  
  idle();                                   // Runs the magic behind the scenes.
  if (timing) {                             // If we're timing the user..
    if (timer.ding()) {                     // If our digit timer has expired..
      dispNumber++;                         // Bump up the display number.
      if (dispNumber == 20) {               // If we reach 20..
        dispNumber = 0;                     // We'll reset to zero.
      }                                     //
      if (dec) {                            // If we're showing the decimal.
        sevseg.setNumber(dispNumber/2);     // We write the new value without.
      } else {                              // else..
        sevseg.setNumber(dispNumber/2,0);   // We write it with the decimal.
      }                                     //
      dec = !dec;                           // Flip the decimal.
      sevseg.refreshDisplay();              // Show the result.
      timer.stepTime();                     // Reset timer with no accumulated error.
    }                                       //
  } else {                                  // Else we are NOT timing someone..
    if (startTimer.ding()) {                // If the starting timer has expired. (Means it's been activated!)
      startTimer.reset();                   // Shut down the starting timer.
      startTiming();                        // Start timing the user.
    }
  }
}