From 692f1ae346d8842fb6d66cb2bcfe799c521763e2 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Sun, 23 Dec 2018 10:44:21 -0500 Subject: [PATCH] Cleanup --- CP101_StateMachines/brute-force/code.py | 19 ++++++---- CP101_StateMachines/classes/code.py | 50 +++++++++++++++++-------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/CP101_StateMachines/brute-force/code.py b/CP101_StateMachines/brute-force/code.py index 83bf51f58..563177711 100644 --- a/CP101_StateMachines/brute-force/code.py +++ b/CP101_StateMachines/brute-force/code.py @@ -12,7 +12,7 @@ Licensed under the MIT license. All text above must be included in any redistribution. """ -# pylint: disable=global-statement +# pylint: disable=global-statement,stop-iteration-return,no-self-use import time import random @@ -126,7 +126,7 @@ def random_color(): blue = random_color_byte() return (red, green, blue) -# Color cycling. See https://learn.adafruit.com/hacking-ikea-lamps-with-circuit-playground-express/lamp-it-up +# Color cycling. def wheel(pos): # Input a value 0 to 255 to get a color value. @@ -230,6 +230,15 @@ def stop_playing(): if audio.playing: audio.stop() +def almost_NY(): + t = rtc.datetime + return (t.tm_mday == 31 and + t.tm_mon == 12 and + t.tm_hour == 23 and + t.tm_min == 59 and + t.tm_sec == 50) + + state = WAITING_STATE paused_state = None @@ -284,11 +293,7 @@ while True: switch.update() test_trigger = True - if test_trigger or (t.tm_mday == 31 and - t.tm_mon == 12 and - t.tm_hour == 23 and - t.tm_min == 59 and - t.tm_sec == 50): + if test_trigger or almost_NY(): test_trigger = False # Play the song start_playing('./countdown.wav') diff --git a/CP101_StateMachines/classes/code.py b/CP101_StateMachines/classes/code.py index e651282cd..cd3b201fa 100644 --- a/CP101_StateMachines/classes/code.py +++ b/CP101_StateMachines/classes/code.py @@ -1,4 +1,3 @@ -import time """ Class based state machine implementation @@ -13,6 +12,9 @@ Licensed under the MIT license. All text above must be included in any redistribution. """ +# pylint: disable=global-statement,stop-iteration-return,no-self-use + +import time import random import board from digitalio import DigitalInOut, Direction, Pull @@ -105,7 +107,7 @@ def random_color(): blue = random_color_byte() return (red, green, blue) -# Color cycling. See https://learn.adafruit.com/hacking-ikea-lamps-with-circuit-playground-express/lamp-it-up +# Color cycling. def wheel(pos): # Input a value 0 to 255 to get a color value. @@ -287,6 +289,9 @@ class State(object): class WaitingState(State): + def __init__(self): + super().__init__() + @property def name(self): return 'waiting' @@ -317,6 +322,7 @@ class WaitingState(State): class DroppingState(State): def __init__(self): + super().__init__() self.rainbow = None self.rainbow_time = 0 self.drop_finish_time = 0 @@ -336,11 +342,10 @@ class DroppingState(State): def exit(self, machine): State.exit(self, machine) - now = time.monotonic() servo.throttle = 0.0 stop_playing() machine.reset_fireworks() - machine.firework_stop_time = now + FIREWORKS_DURATION + machine.firework_stop_time = time.monotonic() + FIREWORKS_DURATION def update(self, machine): if State.update(self, machine): @@ -356,6 +361,9 @@ class DroppingState(State): class BurstState(State): + def __init__(self): + super().__init__() + @property def name(self): return 'burst' @@ -377,6 +385,9 @@ class BurstState(State): class ShowerState(State): + def __init__(self): + super().__init__() + @property def name(self): return 'shower' @@ -391,7 +402,7 @@ class ShowerState(State): def update(self, machine): if State.update(self, machine): if shower(machine, time.monotonic()): - if now >= machine.firework_stop_time: + if time.monotonic() >= machine.firework_stop_time: machine.go_to_state('idle') else: machine.go_to_state('burst') @@ -401,6 +412,9 @@ class ShowerState(State): class IdleState(State): + def __init__(self): + super().__init__() + @property def name(self): return 'idle' @@ -420,6 +434,9 @@ class IdleState(State): class RaisingState(State): + def __init__(self): + super().__init__() + @property def name(self): return 'raising' @@ -448,11 +465,12 @@ class RaisingState(State): class PausedState(State): - @property def __init__(self): + super().__init__() self.switch_pressed_at = 0 self.paused_servo = 0 + @property def name(self): return 'paused' @@ -481,17 +499,17 @@ class PausedState(State): ################################################################################ # Create the state machine -machine = StateMachine() -machine.add_state(WaitingState()) -machine.add_state(DroppingState()) -machine.add_state(BurstState()) -machine.add_state(ShowerState()) -machine.add_state(IdleState()) -machine.add_state(RaisingState()) -machine.add_state(PausedState()) +pretty_state_machine = StateMachine() +pretty_state_machine.add_state(WaitingState()) +pretty_state_machine.add_state(DroppingState()) +pretty_state_machine.add_state(BurstState()) +pretty_state_machine.add_state(ShowerState()) +pretty_state_machine.add_state(IdleState()) +pretty_state_machine.add_state(RaisingState()) +pretty_state_machine.add_state(PausedState()) -machine.go_to_state('waiting') +pretty_state_machine.go_to_state('waiting') while True: switch.update() - machine.update() + pretty_state_machine.update()