#include <SPI.h>
#include "sr595.h"
#define DIV 5
#define SIZE_OF(a) (sizeof(a) / sizeof(a[0]))
const byte PIN_CS = 4;
SR59532 sr32(PIN_CS);
struct OutTime {
uint32_t dataOut;
uint32_t time;
};
struct SCycle {
OutTime *outTimes;
OutTime *ot = nullptr;
uint8_t idx;
uint8_t nMaxIdx;
uint32_t greenTimeRef;
uint32_t saveMillis;
uint8_t phaseCnt;
uint8_t pSplit;
};
SCycle vehicleCycle;
SCycle vehicleCycle1;
SCycle *ptrCycle = &vehicleCycle;
OutTime outTimeList[] = {
{ 0x864921, 30000UL / DIV }, // green
{ 0x864922, 3000UL / DIV }, // yellow
{ 0x864924, 1000UL / DIV }, // red
{ 0x86130C, 25000UL / DIV }, // green
{ 0x861514, 3000UL / DIV },
{ 0x861924, 1000UL / DIV },
{ 0x249924, 15000UL / DIV },
{ 0x491924, 3000UL / DIV },
{ 0x921924, 1000UL / DIV },
{ 0x921864, 30000UL / DIV },
{ 0x9228A4, 3000UL / DIV },
{ 0x924924, 1000UL / DIV } // red
};
OutTime outTimeList1[] = {
{ 0x864921, 30000UL / DIV }, // green
{ 0x864922, 3000UL / DIV }, // yellow
{ 0x864924, 1000UL / DIV }, // red
{ 0x86130C, 10000UL / DIV }, // green
{ 0x861514, 3000UL / DIV },
{ 0x861924, 1000UL / DIV },
{ 0x249924, 15000UL / DIV },
{ 0x491924, 3000UL / DIV },
{ 0x921924, 1000UL / DIV },
{ 0x921864, 30000UL / DIV },
{ 0x9228A4, 3000UL / DIV },
{ 0x924924, 1000UL / DIV } // red
};
// attr_lss4
// verde 10000 / DIV
// fase_time = 10000 + 3000 + 1000 + 3000 + 1000 = 18000
// verde_lss0 = outTimeList[0].time(30000)
// verde_init_end = (verde_lss0 - fase_time) / 2
// verde_init_end = 30000 - 18000 = 12000 / 2 = 6000
uint32_t greenTimeRef;
//OutTime *outPtr = outTimeList;
OutTime *outPtr = nullptr;
uint8_t nMaxIdx = SIZE_OF(outTimeList);
OutTime *sot = nullptr;
uint8_t idx = 0;
uint32_t saveMillis;
uint8_t phaseCnt = 1;
uint8_t pSplit;
// for develop
uint8_t pSplitOld = 4;
uint8_t phaseCntOld = 255;
OutTime g_ot;
void exec(OutTime *ot, uint8_t nMax) {
if (sot == nullptr) {
sot = &ot[idx];
//sr32.write(sot->dataOut);
//Serial.println(sot->time);
g_ot = ot[idx];
sr32.write(g_ot.dataOut);
saveMillis = millis();
if (idx % 3 == 0) {
// si entra qui ad inizio di ogni fase
// greenTimeRef segna l'istante in cui
// commuta il verde per ogni fase.
greenTimeRef = saveMillis;
if (idx != 0) {
//Serial.print("phase: ");
//Serial.println(phaseCnt);
phaseCnt++;
}
}
//Serial.println(sot->time);
//} else if (millis() - saveMillis >= sot->time) {
} else if (millis() - saveMillis >= g_ot.time) {
sot = nullptr;
idx++;
if (idx == nMaxIdx) {
Serial.println("ciclo completato");
phaseCnt = 1;
}
idx = idx % nMax;
pSplit = (pSplit + 1) % 3;
//Serial.println(pSplit);
} else if (1) {
sot = nullptr;
idx++;
}
}
void setup() {
Serial.begin(115200);
Serial.println(sizeof(outTimeList));
outPtr = (OutTime*)malloc(sizeof(outTimeList));
memcpy(outPtr, outTimeList, sizeof(outTimeList));
vehicleCycle.outTimes = outPtr;
vehicleCycle.idx = 0;
vehicleCycle.nMaxIdx = 12;
OutTime lot;
lot = outPtr[1];
Serial.println(lot.time);
pinMode(PIN_CS, OUTPUT);
digitalWrite(PIN_CS, HIGH);
SPI.begin();
sr32.write(0x924924);
delay(2000);
}
uint8_t oldIdx = 13;
uint8_t oldPc = 4;
void loop() {
//exec(outPtr, nMaxIdx);
selectPhase(ptrCycle);
exec1(ptrCycle);
if (ptrCycle->phaseCnt != oldPc) {
oldPc = ptrCycle->phaseCnt;
Serial.println(ptrCycle->phaseCnt);
}
}
void selectPhase(SCycle *sc) {
if (sc->ot == nullptr) {
sc->ot = &sc->outTimes[sc->idx];
sr32.write(sc->ot->dataOut);
//Serial.println(sot->time);
sc->saveMillis = millis();
if (sc->idx % 3 == 0) {
// si entra qui ad inizio di ogni fase
// greenTimeRef segna l'istante in cui
// commuta il verde per ogni fase.
sc->greenTimeRef = saveMillis;
if (sc->idx != 0) {
//Serial.print("phase: ");
//Serial.println(phaseCnt);
sc->phaseCnt++;
}
}
} //Serial.println(sot->time);
}
void exec1(SCycle *sc) {
/*if (sc->ot == nullptr) {
sc->ot = &sc->outTimes[sc->idx];
sr32.write(sc->ot->dataOut);
//Serial.println(sot->time);
sc->saveMillis = millis();
if (sc->idx % 3 == 0) {
// si entra qui ad inizio di ogni fase
// greenTimeRef segna l'istante in cui
// commuta il verde per ogni fase.
sc->greenTimeRef = saveMillis;
if (sc->idx != 0) {
//Serial.print("phase: ");
//Serial.println(phaseCnt);
sc->phaseCnt++;
}
}
//Serial.println(sot->time);
} else */
if (sc->ot) {
if (millis() - sc->saveMillis >= sc->ot->time) {
sc->ot = nullptr;
sc->idx++;
if (sc->idx == sc->nMaxIdx) {
Serial.println("ciclo completato");
sc->phaseCnt = 0;
sc->idx = 0;
}
//idx = idx % nMax;
sc->pSplit = (sc->pSplit + 1) % 3;
//Serial.println(pSplit);
}
}
}
lss0
id=0
lss1
id=1
lss3
id=3
lss2
id=2
LS0
LS1
LS2
LS3
LS4
LS5
LS6
LS7
LS8
LS9
lss4
lss5
lss7
lss6
______________
--------------------------------------------
--------------------------------------------
____________________________________
--------------------------------------------
--------------------------------------------
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
p5
p4
p6
p7
____________________________________
____________________________________
____________________________________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________
______________