Last active
January 4, 2017 15:12
-
-
Save ofgulban/6a111cb3a4a8acbf2489055df1e9f8b8 to your computer and use it in GitHub Desktop.
An example script showing how to interact with Griffin Powermate using pyusb library.
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
"""Pyusb with Griffin Powermate. | |
Linux notes: | |
On Debian 8 it requires admin rights to access the powermate device. | |
The script can be tested via activating superuser and the virtual | |
environment with pyusb and calling the script. | |
Usage notes: | |
dev.read returns the following values in an array: | |
0: button state (0 not pressed, 1 pressed) | |
1: amount turned. CW: 1, 2, 3 etc, CCW: 255, 254, 253 etc | |
2: 0 (always 0) | |
3: brightness of the light (0-255) | |
4: LED status (pulsing or constant brightness, pulse while sleep) | |
5: pulse value (0-255) | |
""" | |
import usb.core | |
import usb.util | |
VENDOR = 0x077d # 1917 | |
PRODUCT = 0x0410 # 1040 | |
dev = usb.core.find(idVendor=VENDOR, idProduct=PRODUCT) | |
if dev is None: | |
raise Exception("Could not find Griffin PowerMate.") | |
if dev.is_kernel_driver_active(0): | |
dev.detach_kernel_driver(0) | |
dev.set_configuration() | |
# get an endpoint instance | |
print dev.get_active_configuration() | |
# %% | |
# start with light turned off | |
light = 0 | |
dev.write(0x2, [0x00]) | |
# turn powermate to increase/decrease light level | |
while dev.read(129, 6, timeout=99999999999)[0] == 0: | |
resp = dev.read(129, 6, timeout=99999999999)[1] | |
if resp < 127: | |
light = (light + resp) % 255 | |
print resp, light | |
dev.write(0x2, [light]) | |
elif resp >= 127: | |
resp = resp - 256 # for opposite direction | |
light = (light + resp) % 255 | |
print resp, light | |
dev.write(0x2, [light]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment