Created
February 18, 2016 19:44
-
-
Save computermacgyver/5d525e457b9adf13406c to your computer and use it in GitHub Desktop.
Python code to use pebble as USB remote clicker (next slide/previosu slide)
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 python | |
#Disconnect your pebble from your phone | |
#Pair with your computer directly | |
#Launch the python script. Open the music app | |
import argparse | |
import os | |
import libpebble | |
import time | |
import pexpect | |
import tkMessageBox | |
from pebble_remote import i18n | |
from pykeyboard import PyKeyboard | |
keyboard = PyKeyboard() | |
from Tkinter import * | |
_ = i18n.language.gettext | |
MAX_ATTEMPTS = 5 | |
window = Tk() | |
window.wm_withdraw() | |
window.geometry("1x1+200+200") #remember its .geometry("WidthxHeight(+or-)X(+or-)Y") | |
LightBluePebbleError = libpebble.LightBluePebble.LightBluePebbleError | |
PebbleError = libpebble.pebble.PebbleError | |
def cmd_remote(pebble, args): | |
pebble.set_nowplaying_metadata(_("Remote Control "), _("Next"), _("Previous")) | |
try: | |
pass | |
except Exception: | |
print _("Something's wrong") | |
raise | |
return False | |
def my_event_handler(event): | |
if event == "next": | |
#pexpect.run(right_click) | |
print("next") | |
try: | |
keyboard.tap_key('Page_Down') | |
except: | |
print("error") | |
if event == "previous": | |
#pexpect.run(left_click) | |
print("previous") | |
try: | |
keyboard.tap_key('Page_Up') | |
except: | |
print("error") | |
if event == "exit": | |
print event | |
try: | |
keyboard.tap_key('b') | |
except: | |
print("error") | |
def music_control_handler(endpoint, resp): | |
events = { | |
"PLAYPAUSE": "exit", | |
"PREVIOUS": "previous", | |
"NEXT": "next" | |
} | |
my_event_handler(events[resp]) | |
print _("waiting for events") | |
while True: | |
try: | |
pebble.register_endpoint("MUSIC_CONTROL", music_control_handler) | |
time.sleep(5) | |
except LightBluePebbleError as e: | |
tkMessageBox.showerror(title="Error", message=e._message, parent=window) | |
raise e | |
#raise KeyboardInterrupt | |
except PebbleError as e: | |
tkMessageBox.showerror(title="Error", message=e._message, parent=window) | |
raise e | |
#raise KeyboardInterrupt | |
#except KeyboardInterrupt: | |
# return | |
def main(): | |
parser = argparse.ArgumentParser(description='a utility belt for pebble development') | |
parser.add_argument('--pebble_id', type=str, help='the last 4 digits of the target Pebble\'s MAC address. \nNOTE: if \ | |
--lightblue is set, providing a full MAC address (ex: "A0:1B:C0:D3:DC:93") won\'t require the pebble \ | |
to be discoverable and will be faster') | |
parser.add_argument('--lightblue', action="store_true", help='use LightBlue bluetooth API') | |
parser.add_argument('--pair', action="store_true", help='pair to the pebble from LightBlue bluetooth API before connecting.') | |
subparsers = parser.add_subparsers(help='commands', dest='which') | |
remote_parser = subparsers.add_parser('remote', help='generate keypress with music app on Pebble') | |
#remote_parser.add_argument('app_name', type=str, help='title of application to be controlled') | |
#remote_parser.add_argument('odp_file_path', type=str, help='path for libreoffice impress presentation file') | |
remote_parser.set_defaults(func=cmd_remote) | |
args = parser.parse_args() | |
attempts = 0 | |
while True: | |
if attempts > MAX_ATTEMPTS: | |
raise _('Could not connect to Pebble') | |
try: | |
pebble_id = args.pebble_id | |
if pebble_id is None and "PEBBLE_ID" in os.environ: | |
pebble_id = os.environ["PEBBLE_ID"] | |
print("args.lightblue: {}".format(args.lightblue)) | |
print("args.pair: {}".format(args.pair)) | |
pebble = libpebble.Pebble(pebble_id, args.lightblue, args.pair) | |
break | |
except LightBluePebbleError as e: | |
tkMessageBox.showerror(title="Error", message=_("Bluetooth connection error"), parent=window) | |
raise KeyboardInterrupt | |
except PebbleError as e: | |
tkMessageBox.showerror(title="Error", message=e._message, parent=window) | |
raise e | |
raise KeyboardInterrupt | |
except: | |
time.sleep(5) | |
attempts += 1 | |
try: | |
args.func(pebble, args) | |
except KeyboardInterrupt as e: | |
pebble.disconnect() | |
pebble=None | |
except LightBluePebbleError as e: | |
tkMessageBox.showerror(title="Error", message=e._message, parent=window) | |
raise e | |
raise KeyboardInterrupt | |
except PebbleError as e: | |
tkMessageBox.showerror(title="Error", message=e._message, parent=window) | |
raise e | |
raise KeyboardInterrupt | |
except Exception as e: | |
pebble.disconnect() | |
pebble=None | |
raise e | |
return | |
if pebble!=None: | |
pebble.disconnect() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment