Created
May 12, 2014 22:40
-
-
Save highfestiva/7c8698941d2c2b22bdce to your computer and use it in GitHub Desktop.
Generator state machine home alarm snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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