Você está na página 1de 4

LightsStateMachine.

h
static bool PostLightsSM(ES_Event ThisEvent);
static ES_Event InitLightsSM(uint8_t Priority);
static ES_Event RunLightsSM(ES_Event ThisEvent);
Public variables:
WhiteWeatherModulator = 1
YellowWeatherModulator = 1
RedWeatherModulator = 1
BlueWeatherModulator = 1

LightsStateMachine.c
module level variables are:
//These are for keeping track of time. Steps are the number of gradations desire
d in each period of day. Counter reflects how far through this gradations we are
.
DawnCounter, SunRisingCounter, SunSettingCounter, DuskCounter, NightCounter
DawnSteps, SunRisingSteps, SunSettingSteps, DuskSteps, NightSteps
//These are for controlling light levels at each phase of the day
MaxRedDawn, MaxBlueDawn, MaxWhiteDawn, MaxYellowDawn,
MaxRedSunRising, MaxBlueSunRising, MaxWhiteSunRising, MaxYellowSunRising,
MaxRedSunSetting, MaxBlueSunSetting, MaxWhiteSunSetting, MaxYellowSunSetting,
MaxRedDusk, MaxBlueDusk, MaxWhiteDusk, MaxYellowDusk,
MaxRedNight, MaxBlueNight, MaxWhiteNight, MaxYellowNight,
//to maintain smooth transitions IF the light color is increasing in brightness
during the new phase.
WhitePrevious, YellowPrevious, RedPrevious, BluePrevious
ES_Event RunLightsSM(ES_Event ThisEvent){}
current state is Idle
if event type is welcome
start circadianClock with timeStep
set all counters to zero
nextState is Dawn
endif
if event type is ES_TIMEOUT
start circadianClock with timeStep
set all counters to zero
nextState is Dawn
endif
if event type is ES_RESET
set all counters to zero
turn all lights off
nextState is Idle
endif
End of Idle state
current state is Dawn
if event type is ES_TIMEOUT & posting clock was circadian clock
increment SunRisingCounter by 1
set pwm of red lights to RedPrevious + (DawnCounter/DawnSteps)*M

axRedDawn*RedWeatherModulator
set pwm of yellow lights to YellowPrevious + (DawnCounter/DawnSt
eps)*MaxYellowDawn*YellowWeatherModulator
set pwm of blue lights to BluePrevious - (DawnCounter/DawnSteps)
*MaxBlueDawn*BlueWeatherModulator
set pwm of white lights to WhitePrevious + (DawnCounter/DawnStep
s)*MaxWhiteDawn*WhiteWeatherModulator
set circadianClock with timeStep
if(DawnSteps == DawnCounter)
Set all previous color PWM values to the current values
nextState = SunRising
endif
endif
if event type is ES_RESET
set all counters to zero
turn all lights off
nextState is Idle
endif
end Dawn statements
current state is SunRising
if event type is ES_TIMEOUT & posting clock was circadian clock
increment SunRisingCounter by 1
set pwm of red lights to RedPrevious - (SunRisingCounter/SunRisi
ngSteps)*MaxRedSunRising*RedWeatherModulator
set pwm of yellow lights to YellowPrevious + (SunRisingCounter/S
unRisingSteps)*MaxYellowSunRising*YellowWeatherModulator
set pwm of blue lights to BluePrevious - (SunRisingCounter/SunRi
singSteps)*MaxBlueSunRising*BlueWeatherModulator
set pwm of white lights to WhitePrevious + (SunRisingCounter/Sun
RisingSteps)*MaxWhiteSunRising*WhiteWeatherModulator
set circadianClock with timeStep
if(SunRisingSteps == SunRisingCounter)
Set all previous color PWM values to the current values
nextState = SunSetting
endif
endif
if event type is ES_RESET
set all counters to zero
turn all lights off
nextState is Idle
endif
end SunRising statements
current state is SunSetting
if event type is ES_TIMEOUT & posting clock was circadian clock
increment SunSettingCounter by 1
set pwm of red lights to RedPrevious + (SunSettingCounter/SunSet
tingSteps)*MaxRedSunSetting*RedWeatherModulator
set pwm of yellow lights to YellowPrevious + (SunSettingCounter/
SunSettingSteps)*MaxYellowSunSetting*YellowWeatherModulator
set pwm of blue lights to BluePrevious + (SunSettingCounter/SunS
ettingSteps)*MaxBlueSunSetting*BlueWeatherModulator
set pwm of white lights to WhitePrevious - (SunSettingCounter/Su
nSettingSteps)*MaxWhiteSunSetting*WhiteWeatherModulator
set circadianClock with timeStep
if(SunSettingSteps == SunSettingCounter)
Set all previous color PWM values to the current values
nextState = Dusk

endif
endif
if event type is ES_RESET
set all counters to zero
turn all lights off
nextState is Idle
endif
end SunSetting statements
current state is Dusk
if event type is ES_TIMEOUT & posting clock was circadian clock
increment DuskCounter by 1
set pwm of red lights to RedPrevious + (DuskCounter/DuskSteps)*M
axRedDusk*RedWeatherModulator
set pwm of yellow lights to YellowPrevious + ((DuskSteps-DuskCou
nter)/DuskSteps)*MaxYellowDusk*YellowWeatherModulator
set pwm of blue lights to BluePrevious + (DuskCounter/DuskSteps)
*MaxBlueDusk*BlueWeatherModulator
set pwm of white lights to WhitePrevious - ((DuskSteps-DuskCount
er)/DuskSteps)*MaxWhiteDusk*WhiteWeatherModulator
set circadianClock with timeStep
if(DuskSteps == DuskCounter)
Set all previous color PWM values to the current values
nextState = Night
endif
endif
if event type is ES_RESET
set all counters to zero
turn all lights off
nextState is Idle
endif
end Dusk statements
current state is Night
if event type is ES_TIMEOUT & posting clock was circadian clock
increment NightCounter by 1
set pwm of red lights to RedPrevious - (SunRisingCounter/SunRisi
ngSteps)*MaxRedNight*RedWeatherModulator
set pwm of yellow lights to YellowPrevious - (NightCounter/Night
Steps)*MaxYellowNight*YellowWeatherModulator
set pwm of blue lights to BluePrevious + ((NightSteps-NightCount
er)/NightSteps)*MaxBlueNight*BlueWeatherModulator
set pwm of white lights to WhitePrevious - (NightCounter/NightSt
eps)*MaxWhiteNight*WhiteWeatherModulator
set circadianClock with timeStep
if(NightSteps == NightCounter)
Set all previous color PWM values to the current values
nextState = Idle
endif
endif
if event type is ES_RESET
set all counters to zero
turn all lights off
nextState is Idle
endif
end Night statements
currentState = nextState
return returnEvent;

Você também pode gostar