Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Created May 12, 2014 22:40
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 highfestiva/7c8698941d2c2b22bdce to your computer and use it in GitHub Desktop.
Save highfestiva/7c8698941d2c2b22bdce to your computer and use it in GitHub Desktop.
Generator state machine home alarm snippet
def alarm_main():
print('Alarm not turned on.')
while not alarm_active():
yield # yield resumes in the next timeslice.
print('Alarm turned on, waiting for burglar.')
while alarm_active():
if alarm_sensor_detecting_something():
print('FAT BEEP! (Waiting 10 seconds to wake the neighbours up.)')
for _ in range(10): yield
print('Siren turned off. Resetting alarm.')
return # Returning restarts this function.
yield
print('Acme Alarm Software v9.3')
tick = State(alarm_main, intermission=1)
while True:
tick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment