clean up states in statemachine

This commit is contained in:
petrucci4prez 2017-06-08 02:15:50 -04:00
parent 960389d043
commit b294a12264
2 changed files with 29 additions and 22 deletions

View File

@ -11,7 +11,7 @@ class Blinkenlights(ExceptionThread):
self._stopper = Event() self._stopper = Event()
self._pin = pin self._pin = pin
self.blink = False self._blink = False
self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds
GPIO.setup(pin, GPIO.OUT) GPIO.setup(pin, GPIO.OUT)
@ -21,7 +21,7 @@ class Blinkenlights(ExceptionThread):
pwm.start(0) pwm.start(0)
while not self._stopper.isSet(): while not self._stopper.isSet():
t = self._sleeptime t = self._sleeptime
if self.blink: if self._blink:
for dc in chain(range(100, -1, -5), range(0, 101, 5)): for dc in chain(range(100, -1, -5), range(0, 101, 5)):
pwm.ChangeDutyCycle(dc) pwm.ChangeDutyCycle(dc)
time.sleep(t) time.sleep(t)
@ -43,5 +43,8 @@ class Blinkenlights(ExceptionThread):
def setCyclePeriod(self, cyclePeriod): def setCyclePeriod(self, cyclePeriod):
self._sleeptime = cyclePeriod/20/2 self._sleeptime = cyclePeriod/20/2
def setBlink(self, toggle):
self._blink = toggle
def __del__(self): def __del__(self):
self.stop() self.stop()

View File

@ -25,38 +25,35 @@ class SIGNALS(enum.Enum):
TRIGGER = enum.auto() TRIGGER = enum.auto()
class State: class State:
def __init__(self, stateMachine, name, entryCallbacks=[], exitCallbacks=[], blinkLED=True, sound=None): def __init__(self, stateMachine, name, entryCallbacks=[], exitCallbacks=[], sound=None):
self.stateMachine = stateMachine self.stateMachine = stateMachine
self.name = name self.name = name
self.entryCallbacks = entryCallbacks self.entryCallbacks = entryCallbacks
self.exitCallbacks = exitCallbacks self.exitCallbacks = exitCallbacks
self.blinkLED = blinkLED
if not sound and name in stateMachine.soundLib.soundEffects: sfx = stateMachine.soundLib.soundEffects
self.sound = stateMachine.soundLib.soundEffects[name]
else: self._sound = sfx[name] if not sound and name in sfx else sound
self.sound = sound
def entry(self): def entry(self):
logger.info('entering ' + self.name) logger.info('entering ' + self.name)
if self.sound: if self._sound:
self.sound.play() self._sound.play()
self.stateMachine.LED.blink = self.blinkLED
self.stateMachine.keypadListener.resetBuffer()
for c in self.entryCallbacks: for c in self.entryCallbacks:
c() c()
def exit(self): def exit(self):
logger.info('exiting ' + self.name) logger.info('exiting ' + self.name)
if self.sound: if self._sound:
self.sound.stop() self._sound.stop()
for c in self.exitCallbacks: for c in self.exitCallbacks:
c() c()
def next(self, signal): def next(self, signal):
if signal in SIGNALS: if signal in SIGNALS:
t = (self, signal) s = (self, signal)
return self if t not in self.stateMachine.transitionTable else self.stateMachine.transitionTable[t] t = self.stateMachine.transitionTable
return self if s not in t else t[s]
else: else:
raise Exception('Illegal signal') raise Exception('Illegal signal')
@ -112,35 +109,42 @@ class StateMachine:
self._timer.stop() self._timer.stop()
self._timer = None self._timer = None
blinkingLED = partial(self.LED.setBlink, True)
sfx = self.soundLib.soundEffects
stateObjs = [ stateObjs = [
State( State(
self, self,
name = 'disarmed', name = 'disarmed',
blinkLED = False entryCallbacks = [partial(self.LED.setBlink, False)]
), ),
State( State(
self, self,
name='disarmedCountdown', name = 'disarmedCountdown',
entryCallbacks = [partial(startTimer, 30, self.soundLib.soundEffects['disarmedCountdown'])], entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['disarmedCountdown'])],
exitCallbacks = [stopTimer] exitCallbacks = [stopTimer]
), ),
State( State(
self, self,
name = 'armed' name = 'armed',
entryCallbacks = [blinkingLED]
), ),
State( State(
self, self,
name = 'armedCountdown', name = 'armedCountdown',
entryCallbacks = [partial(startTimer, 30, self.soundLib.soundEffects['armedCountdown'])], entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['armedCountdown'])],
exitCallbacks = [stopTimer] exitCallbacks = [stopTimer]
), ),
State( State(
self, self,
name = 'triggered', name = 'triggered',
entryCallbacks = [intruderAlert] entryCallbacks = [blinkingLED, intruderAlert]
) )
] ]
for s in stateObjs:
s.entryCallbacks.append(self.keypadListener.resetBuffer)
self.states = namedtuple('States', [s.name for s in stateObjs])(*stateObjs) self.states = namedtuple('States', [s.name for s in stateObjs])(*stateObjs)
self.currentState = getattr(self.states, stateFile['state']) self.currentState = getattr(self.states, stateFile['state'])