Retry device search if tablet disconnects

This commit is contained in:
Eric Jiang 2020-10-29 18:01:14 -07:00
parent 71d396dc7b
commit ec0754804d

View File

@ -1,4 +1,5 @@
import os
import time
from _xdo_cffi import ffi, lib
@ -8,10 +9,6 @@ BUTTON_BINDINGS = {}
def main():
xdo = lib.xdo_new(ffi.NULL)
hidraw_path = get_tablet_hidraw('256c', '006e')
if hidraw_path is None:
print("Could not find tablet hidraw device")
print("Found tablet at " + hidraw_path)
if os.path.isfile(CONFIG_FILE_PATH):
read_config(CONFIG_FILE_PATH)
else:
@ -19,9 +16,27 @@ def main():
create_default_config(CONFIG_FILE_PATH)
print("Created an example config file at " + CONFIG_FILE_PATH)
return 1
hidraw = open(hidraw_path, 'rb')
while True:
hidraw_path = get_tablet_hidraw('256c', '006e')
if hidraw_path is None:
print("Could not find tablet hidraw device")
time.sleep(2)
continue
print("Found tablet at " + hidraw_path)
try:
hidraw = open(hidraw_path, 'rb')
except PermissionError as e:
print(e)
print("Trying again in 5 seconds...")
time.sleep(5)
continue
while True:
try:
btn = get_button_press(hidraw)
except OSError as e:
print("Lost connection with the tablet - searching for tablet...")
time.sleep(3)
break
print("Got button %s" % (btn,))
if btn in BUTTON_BINDINGS:
print("Sending %s" % (BUTTON_BINDINGS[btn],))