I have a volatile unsigned char array LedState[9] variable which is shared between threads. Each index in the array denotes a state. According to each state, the LED will flash in different sequences. One thread sets the state in the array and another thread based on the array index will flash the LEDs. Each state maintains a set of arrays ontimer and offtimer.
unsigned long TimersForBlueLedOn[] = {100,200,500,1,0,0,100,200,500};
unsigned long TimersForBlueLedOff[] = {100,200,500,0,0,0,100,200,500};
In the main thread, I traverse the each state in the array and check if the state is turned on or not. If state is on, I flash the LED for the timer values corresponding to the state.
For eg: state 2 has to be ON for 500ms and OFF for ms. We continue to be in state 2 till state 3 is set. State 3 has ON timer as 1 and no OFF timer, which implies LED should be ON all the time.
State 3 is the base state, that is any state after state 3, should blink according to the timer and should go back to state 3.
For eg, Led blue is turned on after state 3, when state 6 is set, the leds should blink for 100ms ON and 100ms OFF. The leds should blink till state 6 is turned off and go back to state 3. So basically it is a priority based. If state 7 is ON as well, after finishing state 6, it should blink state 7 till state 7 is OFF and should go back to state 3.
My problem is the blink looks like flicker as the state 3 is always set. I need to have stateless transition. I am unable to turn off state 3 based on the next state.
void TurnOnLed(ModemState state) {
LEDState[state] = 1;
}
void TurnOffLed(ModemState state) {
LEDState[state] = 0;
}
unsigned char CheckLedState(unsigned char state) {
return LEDState[state];
}
void GetLedStateVar(LEDStateVar *pLS) {
unsigned char state = pLS->State;
pLS->LongflashCode = INVALID_VAL;
switch(state) {
case ModemTurnOn:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
case ModemInit:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
case GSMConnected:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
case GPRSOn:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
case ServerNotConnected:
pLS->LED = Green;
pLS->OnTimer = TimersForGreenLedOn[state];
pLS->OffTimer = TimersForGreenLedOff[state];
break;
case SwUpdateDownload:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
case SwUpdateRestart:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
case SwUpdateNewVersion:
pLS->LED = Blue;
pLS->OnTimer = TimersForBlueLedOn[state];
pLS->OffTimer = TimersForBlueLedOff[state];
break;
}
void FlashBlueLed(LEDStateVar *pLSV) {
if(pLSV->OnTimer == 1) {
SetLEDBlue(1);
}else {
if(GetElapsedTime(&BlueFlashTimer) > pLSV->OnTimer * MILLI_SECONDS) {
if(!GetLEDBlue()) {
SetLEDBlue(1);
StartTimer(&BlueFlashTimer);
}
}
if(GetElapsedTime(&BlueFlashTimer) > pLSV->OffTimer * MILLI_SECONDS) {
if(GetLEDBlue()) {
SetLEDBlue(0);
StartTimer(&BlueFlashTimer);
}
}
}
}
for(unsigned char i=0; i< FLASHSTATES; ++i) {
LF.State = i;
GetLedStateVar(&LF);
//Flashcode not complete but the state has been reset
if(i == LastBlueState || i == LastGreenState) {
if(LF.LED == Blue) { // BLUE LED
FlashBlueLed(&LF);
}else if(LF.LED == Green) {
FlashGreenLed(&LF);
}
} else if(CheckLedState(i) && LF.OnTimer) {
if(LF.LED == Blue) { // BLUE LED
if(LastBlueState == INVALID_VAL) {
FlashBlueLed(&LF);
}
} else if(LF.LED == Green) { // GREEN LED
if(LastGreenState == INVALID_VAL) {
FlashGreenLed(&LF);
}
} else if(LF.LED == Both) { //BOTH GREEN AND BLUE LED
FlashBothLeds(&LF);
}
}
}