diff options
author | lonkaars <loek@pipeframe.xyz> | 2021-08-03 20:31:16 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2021-08-03 20:31:16 +0200 |
commit | d30c6279efe1e534780f6dfd4ccefcf82bf3c0c1 (patch) | |
tree | 4f0f543d3e92faff093ce53f2f0a1642df8934eb | |
parent | 773c4ef10e3b2be7376271afe30633848fb0eb94 (diff) |
threading update + message backlog for eventual delivery
-rw-r--r-- | main.py | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -1,12 +1,20 @@ #!/bin/python3 from bluepy.btle import Peripheral, ADDR_TYPE_PUBLIC +import threading import time import colorsys import sys -lampmac = sys.argv[1] +mac = sys.argv[1] +dev = None -dev = Peripheral(lampmac, ADDR_TYPE_PUBLIC) +def verify_connection(): + global dev + while dev == None or dev.getState() == 'disc': + dev = Peripheral(mac, ADDR_TYPE_PUBLIC) + +verify_connection() +print("connected") def makemsg(r, g, b, l=0): return bytes([ @@ -17,7 +25,17 @@ def makemsg(r, g, b, l=0): int(l > 0), l, ]) +messages = [] +def thread_func(): + while True: + if len(messages) < 1: continue + message = messages.pop(0) + verify_connection() + dev.writeCharacteristic(0x002A, message) + +threading.Thread(target=thread_func).start() + for line in sys.stdin: r, g, b, l = [ int(x, 16) for x in [ line.strip()[i:i+2] for i in range(0, 8, 2) ] ] - dev.writeCharacteristic(0x002A, makemsg(r, g, b, l)) + messages.append(makemsg(r, g, b, l)) |