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


// ********************************************
// Create a class that manages the relays.
// ********************************************


#define RELAY1 13
#define RELAY2 12
#define RELAY3 11
#define RELAY4 10

class relayThing : public idler {

  public:
          relayThing(void);
          ~relayThing(void);

          void begin(void);
          void startRunning();
          void setRelays(bool r1,bool r2,bool r3,bool r4);
  virtual void idle(void);

          timeObj timer;
          bool    running;
          int     step;
};


relayThing::relayThing(void) { }


relayThing::~relayThing(void) {  }


void relayThing::begin(void) {

  hookup();
  running = false;
  step    = 1;
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  setRelays(false,false,true,false); 
  timer.setTime(2200);
}


void relayThing::startRunning() { running = true; }


void relayThing::setRelays(bool r1,bool r2,bool r3,bool r4) {

  digitalWrite(RELAY1, r1); 
  digitalWrite(RELAY2, r2); 
  digitalWrite(RELAY3, r3); 
  digitalWrite(RELAY4, r4);
}


void relayThing::idle(void) {

  if (running) {
    if (timer.ding()) {
      step++;
      if (step>7) {
        step = 1;
      }
      switch(step) {
        case 1  :
          setRelays(false,false,true,false);
          running = false;
          Serial.println("Back to step 1");
          Serial.println("Click button to begin again.");
        break;
        case 2  :
          setRelays(false,true,true,false);
          Serial.println("step 2");
          timer.start();
        break;
        case 3  :
          setRelays(false,true,false,true);
          Serial.println("step 3");
          timer.start();
        break;
        case 4  :
          setRelays(true,true,false,true);
          Serial.println("step 4");
          timer.start();
        break;
        case 5  :
          setRelays(true,false,false,true);
          Serial.println("step 5");
          timer.start();
        break;
        case 6  :
          setRelays(true,false,true,false);
          Serial.println("step 6");
          timer.start();
        break;
        case 7  :
          setRelays(true,false,true,false);
          Serial.println("step 7");
          timer.start();
        break;
      }
    }
  }
}
    


// ********************************************
// Using the class, write the program..
// ********************************************

mechButton  button(6);  // Button manager
relayThing  relayMgr;   // Relay manager.


void setup() {

  Serial.begin(115200);                             // Start serial to amuse the user.
  Serial.println("CLick button to start pattern."); // Let her know what's going on.
  button.setCallback(btnClick);                     // When the button state changes, call this function.               
  relayMgr.begin();                                 // Do all them thing needed to setup the relays.
}


// This is called when the button state changes.
void btnClick(void) {

  if (!button.getState()) {               // If it's been grounded. (Pressed)
    if (relayMgr.running) {               // Already running?
      Serial.println("Sorry, running.");  // Tell user we can't do anything.
    } else {                              // Else, not running?
      relayMgr.startRunning();            // Cool! Start it up!
    }
  }
}


void loop() { idle(); }   // Idle runs all the magic.