From 9d9c40b06d25a090457e3340e0f9a736644db4f3 Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Sat, 31 Dec 2016 01:48:43 +0000 Subject: [PATCH] Some fixes from BoS and cleanup of music --- software/main.py | 6 ++++++ software/trespassed/game.py | 9 ++++++++- software/trespassed/gpio.py | 19 +++++++++++++++++-- software/trespassed/media.py | 5 ++--- software/trespassed/serial.py | 12 +++++++----- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/software/main.py b/software/main.py index 5dc0f4a..8b6a2cc 100644 --- a/software/main.py +++ b/software/main.py @@ -11,3 +11,9 @@ application = Application("Trespassed") serial_service = SerialService() watchdog_service.setServiceParent(application) serial_service.setServiceParent(application) + +def cleanup(): + from trespassed.media import MEDIA_CHANNELS + MEDIA_CHANNELS.music.stop() + +reactor.addSystemEventTrigger('before', 'shutdown', cleanup) diff --git a/software/trespassed/game.py b/software/trespassed/game.py index 06eb0bd..f186034 100644 --- a/software/trespassed/game.py +++ b/software/trespassed/game.py @@ -1,4 +1,4 @@ -from twisted.internet import defer +from twisted.internet import defer, reactor from twisted.python import log from trespassed.lights import lights, flicker_lights @@ -14,6 +14,10 @@ OPEN = 1 ON = True OFF = False +class Props: + ButtonBox = 0x00 + Input = 0xaa + INPUT_MAP = { 0x00: {0: "DOLLSWITCH0", 1: "DOLLSWITCH1", @@ -64,6 +68,7 @@ def input_parse(b_id, input_no, transition): if transition: game_off() else: + game_off() yield game_init() elif sw is "INNER": if transition is OPEN: @@ -85,3 +90,5 @@ def input_parse(b_id, input_no, transition): end_sequence() yield delay(10) lights(False) + +reactor.callLater(0, game_init) diff --git a/software/trespassed/gpio.py b/software/trespassed/gpio.py index 7604b77..c1e98d9 100644 --- a/software/trespassed/gpio.py +++ b/software/trespassed/gpio.py @@ -27,11 +27,16 @@ Controller.available_pins = [ INPUTS.ResetButton, OUTPUTS.MudroomLock, OUTPUTS.Lights, OUTPUTS.DogBox, OUTPUTS.FrontCameraPower ] +@defer.inlineCallbacks def set_output(pin_no, level): #print(pin_no, level) if not pin_no in PINS: - pin = Controller.alloc_pin(pin_no, OUTPUT) - PINS[pin_no] = pin + try: + pin = yield Controller.alloc_pin(pin_no, OUTPUT) + PINS[pin_no] = pin + except Exception as e: + log.msg("Failed to allocate {}: {}".format(pin_no, e)) + defer.returnValue(None) pin = PINS[pin_no] if level: log.msg("Setting {}".format(pin_no)) @@ -39,3 +44,13 @@ def set_output(pin_no, level): else: log.msg("Clearing {}".format(pin_no)) pin.reset() + +skip = True +def quit(*args, **kwargs): + global skip + if not skip: + reactor.stop() + sys.exit(0) + skip = False + +Controller.alloc_pin(INPUTS.ResetButton, INPUT, quit, RISING) diff --git a/software/trespassed/media.py b/software/trespassed/media.py index dd472cd..c945dc3 100644 --- a/software/trespassed/media.py +++ b/software/trespassed/media.py @@ -43,12 +43,12 @@ class MediaProcessProtocol(protocol.ProcessProtocol): class MediaHandler(object): deferred = attr.ib() transport = attr.ib() - stopped = attr.ib(default=False) def stop(self): print("Trying to stop {}".format(self.transport.pid)) + self.deferred.cancel() self.transport.signalProcess('INT') - self.stopped = True + def addCallbacks(self, *args, **kwargs): self.deferred.addCallbacks(*args, **kwargs) @@ -83,7 +83,6 @@ def play_music(path): args = ['mpg123', MEDIA_PATH+path] mh = play_media(args, 'music') def repeat_music(proto): - if not MEDIA_CHANNELS.music.stopped: log.msg("Restarting Soundtrack") play_music(path) mh.addCallbacks(repeat_music, email_failure) diff --git a/software/trespassed/serial.py b/software/trespassed/serial.py index 304cf8b..1771438 100644 --- a/software/trespassed/serial.py +++ b/software/trespassed/serial.py @@ -48,6 +48,9 @@ class NBusProtocol(protocol.Protocol): #log.msg("PACKET: {}".format(hexlify(packet))) p_type = ord(packet[0]) b_id = ord(packet[1]) + if b_id not in INPUT_MAP: + log.msg("Packet from unknown device: {}".format(b_id)) + defer.returnValue(None) data = packet[2:-2] r_crc = packet[-2:] crc16 = crcmod.predefined.Crc('xmodem') @@ -56,18 +59,17 @@ class NBusProtocol(protocol.Protocol): if crc != r_crc: log.err("CRC Mismatch: {}".format(packet)) defer.returnValue(None) + WATCHDOG[b_id] = time() if p_type == int(PacketType.Keepalive): - WATCHDOG[b_id] = time() + pass elif p_type == int(PacketType.Input): - WATCHDOG[b_id] = time() yield input_parse(b_id, ord(data[0]), ord(data[1])) elif p_type == int(PacketType.Output): pass elif p_type == int(PacketType.Solved): - #log.msg("Solved Puzzle: {}".format(b_id)) + log.msg("Solved Puzzle: {}".format(b_id)) set_output(OUTPUTS.ClosetLock, False) elif p_type == int(PacketType.Failed): - WATCHDOG[b_id] = time() yield delay(1) play_sound_effect("fail.mp3") elif p_type == int(PacketType.Log): @@ -87,7 +89,7 @@ def watchdog(): age = time() - WATCHDOG[key] if age > WATCHDOG_TIMEOUT: log.msg("NBus device #{} timed out, reseting bus".format(key, age)) - WATCHDOG[key] = time() + del WATCHDOG[key] yield reset_nbus() watchdog_service = TimerService(5, watchdog)