Vous êtes sur la page 1sur 7

BRANCH SERVICE

This service keeps track of each "branch" and its LED strips
MODULE VARIABLES
MyPriority
ES_Framework priority
CurrentState
Current state of this service
AnalogInput
Array to hold analog values at TIVA pins
CurrentAnalog
Current analog value
OldAnalog
Previous analog value
CurrentStates
Current states of proximity sensors
LastStates
Previous states of proximity sensors
Sensors
Array of proximity sensors
BranchColors_ByStrip
Array of HSV colors of each strip on the tree
PrevBranchColors_ByStrip
Array of previous HSV colors of each strip
WeatherSequence
Array holding weather sequences for each season
NumWeatherSequences
Number of weather frames per season
MinSliderColors
Minimum slider color values for each season
MaxSliderColors
Maximum slider color values for each season
ColorLimit
Color corresponding to current analog value
CurrentSeason
Current season
NextSeason
Season we are about to change to
SeasonChanged
Boolean, whether the season needs to be changed
ReplayArray
Array holding all color changes throughout a year
ReplayTimeIter
Time iterator for stepping through the replay array
NumReplaySteps
Number of total replay frames
ReplayStepSize
Number of frames to skip with each replay time step
ReplayFactor
Multiplicative factor to slow down replay
DoneReplaying
Boolean, whether the replay is finished
TimeToReplay
Boolean, whether its time to replay
NumArrowColors
Number of colors the arrow displays
ArrowColors
Array of colors arrow can display
Init_BranchService
Parameters:
priority number of this service
Returns:
success/failure
Local Variables: Pseudo-code:
Initialize Branch hardware
Set priority of this service
Post_BranchService
Parameters:
Returns:
Local Variables: Pseudo-code:
ES_PostToService
Return success/failure
Run_BranchService

Parameters:
Returns:
Local Variables:

ThisEvent
Event we are handling
ReturnEvent ES_NO_EVENT if no errors
ReturnEvent
ES_Event to return (ES_NO_EVENT if no errors)
WeatherIter
Frame iterator to step through weather sequence
BranchStripIter
Iterator to step through branches during welcome
NewHSV
New color to set a branch to
PostEvent
ES_Event to post to other service

Pseudo-code:
Switch ThisEvent
IF event is ES_INIT
Set the current season to spring
Set the current state to WelcomingBranch
Initialize the branch timer to replay delay
IF event is ES_TIMEOUT
IF state is WeatherBranch
Set all the branches to the current color in weather sequence
Increment WeatherIter
IF WeatherIter > number of weather frames
Set CurrentSteate to NormalBranch
Restore the branches to their pre-weather state
Set the WeatherIter to 0
IF the season has changed
Set the current season to next season
Update the front panel
END IF
IF it is time to replay the year
Set TimeToReplay to false
Set CurrentState to ReplayingBranch
Post Event REPLAY to tree state machine
Set NumReplaySteps and ReplayStepSize accordingly
Start the branch timer to replay delay
END IF
ELSE
start branch timer for weather interval time
END IF
increment the replay time iterations
ELSE IF state is ReplayingBranch
IF done replaying
Set DoneReplaying to false
Set the CurrentState to WelcomingBranch
Start branch timer for replay delay
ELSE
Light up all the branches for this time step
IF we've finished the replay
Turn off all of the branches
Post DONE_REPLAYING event to tree state machine
Start branch timer for delay
Set replay time iterations to 0
ELSE

Set branch timer for adjusted replay delay


END IF

IF

IF

IF
IF

IF

IF

IF

END IF
ELSE IF state is WelcomingBranch
Set current branch to random color
Update the arrow color
Loop through each strip of LEDs on each branch
Start timer for replay delay
END IF
event is GROW
IF current state is NormalBranch
Increment corresponding colors of the branch to be grown
Increment BranchStripIter
END IF
event is WEATHER
IF current state is NormalBranch
Set the current state to WeatherBranch
Archive the current color of the branches
Start branch timer for weather time delay
END IF
event is ANALOG_CHANGE
Set the color limit based on the analog change
event is SEASON_CHANGE
IF state is WeatherBranch
Set NextSeason to the paramter of SEASON_CHANGE event
ELSE
Set CurrentSeason to the parameter of SEASON_CHANGE event
Update the front panel
END IF
the event is REPLAY
IF the current state is WeatherBranch
Set TimeToReplay to true
ELSE
Set CurrentState to ReplayingBranch
Post ThisEvent to tree state machine
Set the number of replay steps, step size, and replay factor
Start the branch timer for adjusted replay delay
END IF
event is WELCOME_REPLAY
IF CurrentState is WelcomingBranch
Start branch timer for replay delay time
END IF
event is RESET
IF CurrentState is WelcomingBranch
Set CurrentState to NormalBranch
Set CurrentSeason to SPRING
Turn all of the branches off
Turn the arrow off
Post DONE_REPLAYING event to tree state machine

END IF
IF event is START
Update front panel
Set CurrentState to NormalBranch
END Switch
Branch_IncrementColor
Parameters:
BRANCHidx
index of branch whose color we are changing
StripNum
index of strip on BRANCHidx whose color we are changing
Returns:
Local Variables: Pseudo-code:
Call Branch_SetColor with parameters BRANCHidx, StripNum, and ColorLimit
Call IncReplayTimeIter
Branch_SetColor
Parameters:

BRANCHidx
StripNum
HSV
-

index of branch whose color we are changing


index of strip on BRANCHidx whose color we are changing
color we are changing the strip to

Returns:
Local Variables:
Pseudo-code:
Set color of appropriate LED strip to HSV
Store HSV at appropriate spot in BranchColors_ByStrip
AllBranchesOff
Parameters:
Returns:
Local Variables: Pseudo-code:
Call SetAllBranches with parameter HSV_OFF
RestoreBranches
Parameters:
Returns:
Local Variables: Pseudo-code:
Set all values in BranchColors_ByStrip to those in PrevBranchColors_ByStrip
ArchiveBranches
Parameters:
Returns:
Local Variables: Pseudo-code:
Set all values in PrevBranchColors_ByStrip to those in BranchColors_ByStrip
SetAllBranches
Parameters:
Returns:

HSV
-

Color to change the whole tree to

Local Variables: Pseudo-code:


Call Branch_SetColor on every branch and strip to set them to HSV
CheckProxEvents
Parameters:
Returns:
Local Variables:

returnVal
returnVal
ThisEvent

Whether there was a prox event


Whether there was a prox event
ES_Event to post to other services

Pseudo-code:
FOR EACH branch
Poll its proximity sensor
IF the proximity sensor has changed state
Post the appropriate event to Tree State Machine
Update LastStates appropriately
Set returnVal to true
END IT
END FOR
Return returnVal
PollProx
Parameters:
SensorIdx
Sensor to poll
Returns:
uint8_t
Value (HIGH or LOW) of the sensor
Local Variables: Pseudo-code:
Return the value of SensorIdxs input pin on the TIVA
CheckAnalogEvent
Parameters:
Returns:
Local Variables:

returnVal
returnVal
ThisEvent

Whether there was a prox event


Whether there was a prox event
ES_Event to post to other services

Pseudo-code:
Read from the analog pins and store the result in CurrentAnalog
IF the analog value has changed more than ANALOG_BIN_SIZE
Post ANALOG_CHANGE event to Tree State Machine
Set OldAnalog to CurrentAnalog;
Set returnVal to true
END IT
Return returnVal

HSV2RGB
Parameters:
Returns:

HSV
RGB

HSV value to change to RGB


transformed HSV value

Local Variables:

H
S
V
R
G
B

Current hue value


Current saturation value
Current value value
Corresponding red value
Corresponding green value
Corresponding blue value

Pseudo-code:
Map HSV to RGB per http://web.mit.edu/storborg/Public/hsvtorgb.c
Return RGB
SetNewColorLimit
Parameters:
Returns:
Local Variables:

Input
MaxH
MaxS
MaxV
MinH
MinS
MinV
NewH
NewS
NewV

Analog input
Max
Max
Max
Min
Min
Min
New
New
New

hue value based on current season


saturation value based on current season
value value based on current season
hue value based on current season
saturation value based on current season
value value based on current season
hue value based on analog input
saturation value based on analog input
vale value based on analog input

Pseudo-code:
FOR EACH pair of [MinH,MaxH], [MinS,MaxS], [MinV,MaxV]
Interpolate between the pair based on Input
Set NewH, NewS, and NewV based on these interpolations
END FOR
Update ColorLimit with NewH, NewS, and NewV
Set analog LED indicator to ColorLimit
IncReplayTimeIter
Parameters:
Returns:
Local Variables: Pseudo-code:
Store all values in BranchColors_ByStrip in ReplayArray
Increment ReplayTimeIter
IF we have exceeded the maximum number of replay frames
Set ReplayTimeIter to MAX_REPLAY_STEPS
END IF

UpdateFrontPanel
Parameters:
Returns:
Local Variables:

Pseudo-code:
Read from the analog pins
Call SetNewColorLimit with parameter of the analog sliders value
Call LED_SetColor on COLOR_MIN_STRIP_IDX and COLOR_MAX_STRIP_IDX based on the season
UpdateArrow
Parameters:
Returns:
Local Variables:

NewH
NewS
NewV
ArrowIter
ArrowFadeIter
ArrowFadeRes
ArrowInc
CurrArrowColor

New hue to set the arrow to


New saturation to set the arrow to
New value to set the arrow to
Iterator to step through the arrows colors
Iterator to fade in and out of current color
Number of steps in the fade
Size of each step in the fade
Current color of the arrow

Pseudo-code:
IF the arrow is fading brighter
Add ArrowInc to CurrArrowColor Value value (i.e. V in HSV)
ELSE IF the arrow is fading dimmer
Subtract ArrowInc from CurrArrowColors Value value (i.e. V in HSV)
ELSE IF the arrow is finished fading in and out of this color
Reset ArrowFadeIter
Increment ArrowIter
Set the Value value of CurrArrowColor to 0
END IF
Set the arrow to CurrArrowColor
Increment ArrowFadeIter

Vous aimerez peut-être aussi