#include "ArduinoTrace.h"
#include "ptScheduler.h"
#include "ezButton.h"

ptScheduler Task1Sch=ptScheduler(1000000);

byte led1=11;
byte led2=12;

ezButton button1(1);
bool button1IsPressed = false;

ezButton button2(2);
bool button2IsPressed = false;

ezButton button3(3);
bool button3IsPressed = false;

bool T1Start;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  button1.setDebounceTime(50);
  button2.setDebounceTime(50);
  button3.setDebounceTime(50);
}

void loop() {
  // put your main code here, to run repeatedly:
  button1.loop();
  button2.loop();
  button3.loop();

  if (button1.isPressed()){
    button1IsPressed = true;
  }

  if (button2.isPressed()){
    button2IsPressed = true;
  }

  if (button3.isPressed()){
    button3IsPressed = true;
  }

  if(Task1Sch.call())
  {
    T1Start=true;
    Task1(); 
  } 
}

void Task1()
{
  enum {null,s1,s2,s3,s4};
  static int step=s1;
  int pstep=null;
  static bool Q0=0;
  bool Q1 = 1;
  while(step!=pstep)
  {
    pstep=step;
    switch(step)
    {
      case s1:
        if(T1Start)
        {
          T1Start=false;
          step=s2;
        }
      break;
      case s2:
        Q0 = 0;
        if (button1IsPressed){
          button1IsPressed = false;
          step = s3;
        }
      break;
      case s3:
        Q0 = 1;
        if (button2IsPressed){
          button2IsPressed = false;
          step = s4;
        }
      break;

      case s4:
        Q1 = 0;
        if (button3IsPressed){
          button3IsPressed = false;
          step = s1;
          Q1 = 1;
        }
      break;
    }  
  }

  digitalWrite(led1,Q0);
  digitalWrite(led2,Q1);
  // DUMP(millis());
  
}