#include <SPI.h>

const byte PIN_DATA    =  11;    
const byte PIN_LATCH   =  4;   
const byte PIN_CLOCK   =  13; 
const byte PIN_START   =  7;

byte      g_state;
uint16_t  g_rndNumber; // range 1000÷3000 ms
bool      g_btn0State;

struct timer_t {
    uint32_t oneSec;
    uint16_t oneSecInt;
    uint32_t t0;
    uint32_t t1;
} timer;

struct Semaforo_t {
    byte cnt = 8;
    byte data;
} semaforo;  // istanza di tipo Semaforo_t

enum State {
    RE_START, SEMAFORO, MONITORING, END_RESET
};

byte g_testResult;

void setup() {
 
    Serial.begin(115200);
    pinMode(PIN_START, INPUT_PULLUP);
    SPI.begin();
    
    pinMode(PIN_LATCH, OUTPUT);
    //pinMode(PIN_DATA, OUTPUT);
    //pinMode(PIN_CLOCK, OUTPUT);
    digitalWrite(PIN_LATCH, HIGH);
    /*digitalWrite(PIN_LATCH, LOW);
    SPI.transfer(85);
    digitalWrite(PIN_LATCH, HIGH);
    
    delay(100);
    digitalWrite(PIN_LATCH, LOW);
    SPI.transfer(0);
    digitalWrite(PIN_LATCH, HIGH);*/
    writeShiftReg(0);
}

// writeShiftReg usa la SPI hardware
// la chiamata writeShiftReg(byte data) consuma 148us
void writeShiftReg(byte data) {
   digitalWrite(PIN_LATCH, LOW);
   SPI.transfer(data);  // spi hardware
   //shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, data); //spi software
   digitalWrite(PIN_LATCH, HIGH);
}

byte semaforoRun() {
    if ((millis() - timer.oneSec) >= timer.oneSecInt) {
        timer.oneSec = millis();
        timer.oneSecInt = 1000;
        
        semaforo.data |= (1 << (semaforo.cnt - 1)) 
                      | (1 << (semaforo.cnt - 2)); // end command

        //uint32_t g_us = micros();
        writeShiftReg(semaforo.data);
        //Serial.println(micros() - g_us);
        semaforo.cnt -= 2;

        if (semaforo.cnt == 0) {
            semaforo.cnt = 8;
            // *** GHANGE STATE
            g_state = MONITORING;
            timer.t0 = millis();
            
            return g_state;
        }
    }
    return g_state;
}

void initRandom() {
    static bool lstate;
    if (!lstate) {
        Serial.println("Premi per iniziare il test.");
        
        lstate = true;
    } else {
        if (digitalRead(PIN_START) == LOW) {
            // carica il timer oneSec
            timer.oneSec = millis();
            // azzera intervallo
            timer.oneSecInt = 0;
            // Inizializza il generatore rnd
            randomSeed(micros());
            // genera un numero nel range 1000÷3000
            g_rndNumber = random(1000, 3001);
        }
    }
}


void loop() { 
    switch (g_state) {
      case RE_START:
      
        initRandom();
        if (g_rndNumber) {
            // *** CHAGE STATE ***
            g_state = SEMAFORO;
        }
        break;

      case SEMAFORO:
        // quando tutte le luci sono accese
        // g_state assume il valore SEMAFORO + 1
        // che è uguale a MONITORING
        g_state = semaforoRun();
        break;

      case MONITORING:
        g_btn0State = digitalRead(PIN_START);
        if (((millis() - timer.t0) >= g_rndNumber
                && semaforo.data != 0)) {
            // spegne tutt i led
            semaforo.data = 0;
            writeShiftReg(0);
            // salva millis()
            timer.t0 = millis();
        }
        // Se pulsante premuto e data == 0
        if (g_btn0State == LOW && semaforo.data == 0) {
            g_testResult = 1;
            timer.t1 = millis();
            /*Serial.print("Tempo di reazione: ");
            Serial.print(millis() - timer.t0);
            Serial.println("ms");*/

            // ** CHAGE STATE **
            g_state = END_RESET;
        // Se premi il pulsante con i led ancora accesi
        } else if (g_btn0State == LOW && semaforo.data != 0) {
            //Serial.println("Falsa partenza");
            g_testResult = 2;
            // ** CHAGE STATE **
            g_state = END_RESET;
        }
        break;

      case END_RESET:
        
        semaforo.data = 0;
        writeShiftReg(0);
        if (g_testResult == 1) {
            Serial.print("Tempo di reazione: ");
            Serial.print(timer.t1 - timer.t0);
            Serial.println("ms");
        } else if (g_testResult == 2) {
            Serial.println("Falsa partenza");
        }
        g_testResult = 0;
        delay(200);
        while(!digitalRead(PIN_START));
        //delay(200);
        delay(g_rndNumber/8);
        
        Serial.print("rnd number: ");
        Serial.println(g_rndNumber);        
        Serial.println("Premi per ricominciare.");
  
        g_rndNumber = 0;

        // ** CHAGE STATE **
        g_state = RE_START;
        break;
    }
}
74HC595