Skip to content

Instantly share code, notes, and snippets.

@1328
Created February 3, 2016 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1328/bc43daa81022f9f5dd40 to your computer and use it in GitHub Desktop.
Save 1328/bc43daa81022f9f5dd40 to your computer and use it in GitHub Desktop.
mockup v2
import time
# initialize an events dictionary that will later be populated with different
# functions to be called as events are identified by poll
global EVENTS
def poll(state):
'''
this polls the audrino and clock to see if there has been some event
Right now we'll just use the clock to mock up events
yields a pair of state, value of changed states from last polling
'''
prior_state = state.copy()
while True:
# update clock
running = time.time() - state['start']
state['clock'] = int(state['countdown'] - running)
# find differences
keys = state.keys() | prior_state.keys()
delta = {}
for key in keys:
if state.get(key) != prior_state.get(key):
delta[key] = state[key]
if not delta:
# nothing has happened
time.sleep(.1)
continue
# we are making this a generator because generators are cool
prior_state = state.copy()
for key, value in delta.items():
yield key, value
def ignite_boosters(state):
print('igniting boosters')
if state.get('enable_boosters') != True:
print('\t boosters have failed to ignire')
else:
print('\t boosters go')
state['boosters'] = True
def liftoff(state):
if state['boosters'] == True:
print('lift off')
else:
print('critical failure')
state['done'] = True # causes program to stop
def clock(state):
'''
generic function to handle timed events
'''
print('T minus {}'.format(state['clock']))
event = 'clock_{}'.format(state['clock'])
if event in EVENTS:
func = EVENTS[event]
func(state)
def abort(state):
print('abort button pressed!')
print('shutting down')
state['done'] = True
def t_minus_4(state):
'''
to mock up a button press, we will pretend that the button changed at T
minus 4 by changing a state
here we pretend to enable/disable the boosters, something necessary for the
booster ignitition
'''
state['enable_boosters'] = True
# removing comment from next line will cause a booster failure
#state['enable_boosters'] = False
# removing comment from next line will cause an abort event, as if a stop
# button pushed
# state['abort'] = True
EVENTS = {
'clock': clock,
'clock_4': t_minus_4,
'clock_3': ignite_boosters,
'clock_0': liftoff,
'abort': abort,
#'boosters': confirm_boosters, # event called when booster state changes
}
def main():
state = {
'start': time.time(),
'countdown': 5,
'clock': 5,
'ok': True,
'boosters': False,
}
for event, value in poll(state):
# remove comments to watch how events pop up
#print('DEBUG: event = {}, value = {}'.format(event, value))
if event == 'done' and value == True:
break
if event in EVENTS:
func = EVENTS[event]
func(state)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment