Skip to content

Instantly share code, notes, and snippets.

@bhwarren
Last active December 1, 2019 21:10
Show Gist options
  • Save bhwarren/89c0d1e133b03c4b71024c1ad54c723b to your computer and use it in GitHub Desktop.
Save bhwarren/89c0d1e133b03c4b71024c1ad54c723b to your computer and use it in GitHub Desktop.
import serial
import serial.tools.list_ports
from playsound import playsound
import time
def getSerial():
pid = "67"
vid = "9025"
ports=serial.tools.list_ports.comports(include_links=False)
for p in ports:
if(str(p.pid) == pid and str(p.vid) == vid):
return serial.Serial(p.device)
return None
def runEventLoop(serial, callback):
while(serial.isOpen()):
line = serial.readline().decode('utf-8').strip()
callback(line)
def cb(event):
print(event)
if(event == "button_pressed"):
playsound('patd.mp3')
def main():
while True:
try:
s = getSerial()
print("initialized on device "+s.port)
print("waiting for events...")
runEventLoop(s, cb)
except:
time.sleep(5)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment