Skip to content

Instantly share code, notes, and snippets.

@1328
Created February 1, 2016 16:38
Show Gist options
  • Save 1328/a1d6e07f0534b22c6fe8 to your computer and use it in GitHub Desktop.
Save 1328/a1d6e07f0534b22c6fe8 to your computer and use it in GitHub Desktop.

Revisions

  1. 1328 created this gist Feb 1, 2016.
    78 changes: 78 additions & 0 deletions x.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@

    import time

    # create a dictionary
    state = {
    'start': time.time(),
    'countdown': 5,
    'clock': 5,
    'ok': True,
    'boosters': 'off',
    }


    def poll(state):
    '''
    this would probably just check the clock on the audrino
    but for now we mock it up using time.time
    '''

    running = time.time() - state['start']
    state['clock'] = int(state['countdown'] - running)

    # now define a bunch of functions to be called at various times on the countdown
    # each function gets passed the state dictionary and can modify that dictionary
    # (sicne dictionaries are mutable) to pass information to other parts of the
    # code

    def do_systems_check(state):
    if state['ok']:
    print('all systems go')

    def do_boosters(state):
    if state['ok']:
    state['boosters'] = 'on'
    print('boosters on')

    def do_lift_off(state):
    if state['ok'] and state['boosters'] == 'on':
    print('lift off')

    # now we put all of those do_ functions into a dictionary, with keys that
    # identify when they should be called, thus: at T-4 we do a systems check, at


    EVENTS = {
    4:do_systems_check,
    2:do_boosters,
    0:do_lift_off,
    }

    last_clock = None # to prevent duplicates
    while state['clock']>0:
    poll(state)
    clock = state['clock']
    if clock == last_clock:
    # the clock has not meaningfully changed, so just sleep and wait for
    # poll to return something interesting
    # the logic will need to be extended to allow for other events to
    # wake-up the logic, other than just the clock rolling over
    # also, consider doing away with the sleep, as it will cause a bit of a
    # lag

    time.sleep(.1)
    continue
    last_clock = clock

    print('T minus {}'.format(state['clock']))

    # now check the events dictionary to see if we trigger anything

    if clock in EVENTS:
    func = EVENTS[clock]
    func(state)