#include <SPI.h>
#include "Semafori.h"
#include "sr595.h"
//#include <JC_Button.h>
//Button btn3(10);

const byte PIN_CS   =  4;  
#define ENDL      255
// Dichiarazione e formazione dei gruppi.
uint8_t id0[] =       {0, ENDL};          // Lanterna a nord lss0, id=0
uint8_t id0_6[] =     {0, 6, ENDL};       // Lanterne lss0 e lss6
uint8_t id2[] =       {2, ENDL};          // Lanterna lss2
uint8_t id2_4[] =     {2, 4, ENDL};       // Lanterne lss2 e lss4
uint8_t id1_3_4[] =   {1, 3, 4, ENDL};    // Lanterne lss1, lss3 e lss4
uint8_t id1_3[] =     {1, 3, ENDL};       // Lanterne lss1 e lss3
uint8_t id5[] =       {5, ENDL};          // Lanterna lss5
uint8_t id5_6_7[] =   {5, 6, 7, ENDL};    // Lanterne lss5, lss6 e lss7
uint8_t id5_7[] =     {5, 7, ENDL};       // Lanterne lss5 e lss7

// DIV divisore, es: 30000 / 5 = 6000ms (6 secondi) 
#define DIV 5

#define TPH0 30000UL  / DIV
#define TPH1 3000UL   / DIV
#define TPH2 1000UL   / DIV
#define TPH3 30000UL  / DIV
#define TPH4 3000UL   / DIV
#define TPH5 1000UL   / DIV
#define TPH6 30000UL  / DIV
#define TPH7 3000UL   / DIV
#define TPH8 1000UL   / DIV
#define TPH9 30000UL  / DIV
#define TPH10 3000UL  / DIV
#define TPH11 1000UL  / DIV

struct Phase {
    uint8_t *outs;  // puntatore ad array delle lanterne conivolte
    uint32_t ms;    // tempo di permanenza del coloro
};

// Pre-timed phases
Phase phases[] = {
    
    // 4 lanterne stradali, e 4 pedonali.
    // Divisione in 12 PHASES
    // Si inizia sempre da nord che commuta su verde.
    { id0_6, TPH0 },       // GREEN,  PHASE 0    
    { id0, TPH1 },         // YELLOW, PHASE 1
    { id0, TPH2 },         // RED,    PHASE 2
    { id1_3_4, TPH3 },     // GREEN,  PHASE 3
    { id1_3, TPH4 },       // YELLOW, PHASE 4
    { id1_3, TPH5 },       // RED,    PHASE 5
    { id5_7, TPH6 },       // GREEN,  PHASE 6
    { id5_6_7, TPH7 },     // YELLOW,  PHASE 7
    { id5_6_7, TPH8},      // RED,     PHASE 8
    { id2, TPH9 },         // GREEN,   PHASE 9
    { id2_4, TPH10 },      // YELLOW,  PHASE 10
    { id2_4, TPH11 },      // RED,     PHASE 11
    { nullptr, 0 }
};
/*   0  1  2  3  4  5  6  7  
    "g  r  r  r  r  r  g  r"
    "r  g  r  g  g  r  g  r"
    "r  r  r  r  g  g  g  g"
    "r  r  g  r  g  r  r  r"
*/

SR59532 sr32(PIN_CS);

LSS out0(sr32, 0, 1, 2);
LSS out1(sr32, 3, 4, 5);
LSS out2(sr32, 6, 7, 8);
LSS out3(sr32, 9, 10, 11);
LSS out4(sr32, 12, 13, 14);
LSS out5(sr32, 15, 16, 17);
LSS out6(sr32, 18, 19, 20);
LSS out7(sr32, 21, 22, 23);
LSS out8(sr32, 24, 25, 26);   // non usata
LSS out9(sr32, 27, 28, 29);   // non usata

LSS *outs[10] = { &out0, &out1, &out2, &out3, &out4, &out5, &out6, &out7, &out8, &out9 };

uint32_t wait;
uint32_t durationCycle;
uint8_t phasesIdx = 0;
uint8_t colorCounter = 0; // 0 1 2 0 1 2 ecc, GREEN, YELLOW, RED
uint8_t exec;
uint32_t saveTime;
uint32_t refTime;

LSS &out(uint8_t n) {
    return *outs[n];
}

Phase &getCurrentPhase() {
    return phases[phasesIdx];
}

void initId() {
    
    // Mentre quelle stradali hanno id=0÷9 
    for (uint8_t i=0; i<10; i++) {
      
        out(i).setId(i);
    }
}

void setAllRed() {
    for (uint8_t i=0; i<8; i++) {
        out(i).setColor(Color::RED);
    }
}

void initAll() {
    
    initId();
    // setAllRed(): imposta tutte le lanterne sul rosso 
    setAllRed();
    //btn3.begin();
    sr32.update();
    delay(2000);
    exec = 1;
}

void setup() {
 
    Serial.begin(115200);
    pinMode(PIN_CS, OUTPUT);
    digitalWrite(PIN_CS, HIGH);
    SPI.begin();
    
    initAll();
    refTime = millis();
}

void execPhases() {
    if (exec == 1) {
        
        Phase &ph = getCurrentPhase();
        wait = ph.ms;
        durationCycle += wait;
        
        uint8_t idIdx = 0;
        Serial.print("id:");
        // cicla sul gruppo
        while (ph.outs[idIdx] != ENDL) {
                       
            out(ph.outs[idIdx]).setColor(colorCounter);
            
            Serial.print(out(ph.outs[idIdx]).id());
            Serial.print(", ");
            
            idIdx++;
        }
        
        sr32.update();
        
        Serial.print("Color:");
        Serial.print(colorCounter);
        Serial.print(", ");
        Serial.print("Duration:");
        Serial.print(wait);
        Serial.println();
        saveTime = millis();
        exec = 2;
    } 

    if (exec == 2) {
        
        if (millis() - saveTime >= wait) {
            saveTime = millis();
            phasesIdx++;
            
            if (phases[phasesIdx].outs == nullptr) {
                
                phasesIdx = 0;
                Serial.print("durationCycle:");
                Serial.println(durationCycle);
                Serial.print("real time: ");
                Serial.println(millis() - refTime);
                refTime = millis();
                durationCycle = 0;
                Serial.println("\nRestart from Phase 0");
            }
            colorCounter++;
            colorCounter = colorCounter % 3;
            exec = 1;
        }
    }
    
}

void loop() { 

    execPhases();
    
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
74HC595
sr1:Q1
sr1:Q2
sr1:Q3
sr1:Q4
sr1:Q5
sr1:Q6
sr1:Q7
sr1:GND
sr1:Q7S
sr1:MR
sr1:SHCP
sr1:STCP
sr1:OE
sr1:DS
sr1:Q0
sr1:VCC
led3:A
led3:C
led4:A
led4:C
led5:A
led5:C
led6:A
led6:C
led7:A
led7:C
led8:A
led8:C
74HC595
sr2:Q1
sr2:Q2
sr2:Q3
sr2:Q4
sr2:Q5
sr2:Q6
sr2:Q7
sr2:GND
sr2:Q7S
sr2:MR
sr2:SHCP
sr2:STCP
sr2:OE
sr2:DS
sr2:Q0
sr2:VCC
led11:A
led11:C
led12:A
led12:C
led13:A
led13:C
led14:A
led14:C
led15:A
led15:C
led16:A
led16:C
lss0 id=0
lss1 id=1
lss3 id=3
lss2 id=2
Strisce pedonali
sw1:1a
sw1:2a
sw1:3a
sw1:4a
sw1:5a
sw1:6a
sw1:7a
sw1:8a
sw1:8b
sw1:7b
sw1:6b
sw1:5b
sw1:4b
sw1:3b
sw1:2b
sw1:1b
sw2:1a
sw2:2a
sw2:3a
sw2:4a
sw2:5a
sw2:6a
sw2:7a
sw2:8a
sw2:8b
sw2:7b
sw2:6b
sw2:5b
sw2:4b
sw2:3b
sw2:2b
sw2:1b
sw3:1a
sw3:2a
sw3:3a
sw3:4a
sw3:5a
sw3:6a
sw3:7a
sw3:8a
sw3:8b
sw3:7b
sw3:6b
sw3:5b
sw3:4b
sw3:3b
sw3:2b
sw3:1b
sw4:1a
sw4:2a
sw4:3a
sw4:4a
sw4:5a
sw4:6a
sw4:7a
sw4:8a
sw4:8b
sw4:7b
sw4:6b
sw4:5b
sw4:4b
sw4:3b
sw4:2b
sw4:1b
74HC595
sr3:Q1
sr3:Q2
sr3:Q3
sr3:Q4
sr3:Q5
sr3:Q6
sr3:Q7
sr3:GND
sr3:Q7S
sr3:MR
sr3:SHCP
sr3:STCP
sr3:OE
sr3:DS
sr3:Q0
sr3:VCC
74HC595
sr4:Q1
sr4:Q2
sr4:Q3
sr4:Q4
sr4:Q5
sr4:Q6
sr4:Q7
sr4:GND
sr4:Q7S
sr4:MR
sr4:SHCP
sr4:STCP
sr4:OE
sr4:DS
sr4:Q0
sr4:VCC
LS0
LS1
LS2
LS3
LS4
LS5
LS6
LS7
LS8
LS9
led1:A
led1:C
led2:A
led2:C
led9:A
led9:C
lss4
led10:A
led10:C
led17:A
led17:C
led18:A
led18:C
lss5
and1:A
and1:B
and1:OUT
not1:IN
not1:OUT
led19:A
led19:C
led20:A
led20:C
led21:A
led21:C
led22:A
led22:C
led23:A
led23:C
led24:A
led24:C
lss7
lss6
Strisce pedonali
Strisce pedonali
Strisce pedonali
--------------------------------------------
--------------------------------------------
--------------------------------------------
--------------------------------------------
--------------------------------------------
--------------------------------------------
--------------------------------------------
--------------------------------------------
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -