Last active
October 21, 2016 13:22
-
-
Save royvandam/c89bd99ddcddf7a3883c7d587db6d273 to your computer and use it in GitHub Desktop.
Some BeagleBone Black python code to poll a digital IO pin and send a UDP command to a video player
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
#!/usr/bin/env python2 | |
import signal, time, threading, socket, sys | |
import Adafruit_BBIO.GPIO as GPIO | |
class Pir: | |
def __init__(self, pin, pud=GPIO.PUD_UP): | |
self.pin = pin | |
self.pud = pud | |
GPIO.setup(self.pin, GPIO.IN, pull_up_down=pud) | |
def wait_for_edge(self, flank): | |
return GPIO.wait_for_edge(self.pin, flank) | |
def wait_for_active(self): | |
flank = GPIO.FALLING if self.pud == GPIO.PUD_UP else GPIO.RISING | |
return self.wait_for_edge(flank) | |
def wait_for_inactive(self): | |
flank = GPIO.RISING if self.pud == GPIO.PUD_UP else GPIO.FALLING | |
return self.wait_for_edge(flank) | |
def read(self): | |
value = GPIO.input(self.pin) | |
return not value if self.pud == GPIO.PUD_UP else value | |
class Panel: | |
def __init__(self, address, port = 65000): | |
self.endpoint = (address, port) | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
self.write('mode:{"mode":"Player"}') | |
time.sleep(0.5) | |
self.write('stop') | |
def write(self, data): | |
self.socket.sendto(data, self.endpoint) | |
def preset(self, id): | |
self.write('preset:%d' % id) | |
def previous(self): | |
self.write('previous') | |
def next(self): | |
self.write('next') | |
def play(self): | |
self.write('play') | |
def stop(self): | |
self.write('stop') | |
def pauze(self): | |
self.write('pauze') | |
class AutoSelect(threading.Thread): | |
def __init__(self, pir, panel, preset): | |
self.pir = pir | |
self.panel = panel | |
self.preset = preset | |
self._stop = threading.Event() | |
super(AutoSelect, self).__init__() | |
def stop(self): | |
self._stop.set() | |
def run(self): | |
index = 0 | |
active = False | |
while not self._stop.isSet(): | |
value = self.pir.read() | |
if value and not active: | |
active = True | |
if index == 0: | |
self.panel.preset(self.preset['id']) | |
else: | |
self.panel.next() | |
index = (index + 1) % self.preset['size'] | |
if not value and active: | |
active = False | |
self.panel.stop() | |
time.sleep(0.2) | |
if __name__ == '__main__': | |
selector = None | |
def onSignal(_signo, _stack_frame): | |
print("Caught ctrl+c stopping application...") | |
if selector: | |
selector.stop() | |
GPIO.cleanup() | |
sys.exit(0) | |
signal.signal(signal.SIGINT, onSignal) | |
pir = Pir('GPIO1_28') | |
panel = Panel('10.0.0.143') | |
selector = AutoSelect(pir, panel, preset = { | |
'id': 1, | |
'size': 6 | |
}) | |
selector.start() | |
print("Running, press ctrl+c to stop.") | |
signal.pause() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment