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

ptScheduler Task1Sch=ptScheduler(1000000);
ezButton buttonG(3);
ezButton buttonR(2);
ezButton buttonY(4);

bool T1Ready;
bool bGFlag,bRFlag,bYFlag;
bool ledState=LOW;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  buttonG.setDebounceTime(50);
  buttonR.setDebounceTime(50);
  buttonY.setDebounceTime(50);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonG.loop();
  buttonR.loop();
  buttonY.loop();
  if(buttonG.isPressed()) bGFlag=true;
  if(buttonR.isPressed()) bRFlag=true;
  if(buttonY.isPressed()) bYFlag=true;
  //if(Task1Sch.call()) T1Ready=true;
  if(Task1Sch.call()) Task1();
  //Task1();
}

void Task1()
{
  enum {null,s1,s2,s3};
  static int step=s1;
  int pstep=null;
  ledState=digitalRead(13);
  static bool Q0=1;        //stored action
  bool Q1=1;               //continous action
  while(step!=pstep)
  {
    pstep=step;
    switch(step)
    {
      case s1:
        if(bGFlag==true)
        {         
          step=s2;
        }
      break;

      case s2:
        if(bRFlag)
        {        
          step=s3;
        }
      break;
      case s3:
        if(bYFlag)
        {         
          step=s1;
        }
      break;
    }  
    //stored actions 
    if(step==s1) Q0=0;
    if(step==s2) Q0=1;
  }
  //accept flags
  bGFlag=false;
  bRFlag=false;
  bYFlag=false;
  //continous actions
  if(step==s3) Q1=0;

  digitalWrite(9,Q0);
  digitalWrite(8,Q1);

  DUMP(Q1);
  DUMP(Q0);
}