Skip to content

Commit

Permalink
PyPi Package preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Berwick committed Mar 27, 2013
1 parent 8def85f commit 793befa
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 85 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -10,7 +10,6 @@ dist
build
eggs
parts
bin
var
sdist
develop-eggs
Expand Down
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -0,0 +1 @@
v0.1.0, 2013-03-27 -- Initial release.
File renamed without changes.
2 changes: 2 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,2 @@
include *.txt
recursive-include docs *.txt
84 changes: 0 additions & 84 deletions README.md

This file was deleted.

92 changes: 92 additions & 0 deletions README.rst
@@ -0,0 +1,92 @@
BlinkStick Python
=================

BlinkStick Python interface to control devices connected to the
computer.

What is BlinkStick? Check it out here:

http://www.blinkstick.com

Requirements
------------

- Python
- PyUSB
- grapefruit - package for color manipulations
- pyusb - package to access USB devices
- psutil - only for example-cpu.py
- websocket-client - only for example-connect.py to connect to
BlinkStick.com

Installation
------------

Install all required packages with pip:

::

[sudo] pip install grapefruit pyusb psutil websocket-client

If websocket-client fails to install, please make sure you run the
following command:

::

sudo apt-get install python-dev

Replace *python-dev* with *python2.7-dev* if you are installing on
Raspberry Pi.

Description
-----------

Description of files:

- blinkstick.py - main BlinkStick class definition
- example-info.py - displays information of each BlinkStick
- example-infoblock.py - read/write info block sample
- example-off.py - turn all blinksticks off
- example-random.py - set random color to all BlinkSticks
- example-cpu.py - displays CPU usage with a BlinkStick (transitions
from green as 0% to red as 100%)
- example-connect.py - sample code to connect to BlinkStick.com and
control it remotely

Running examples:

::

python example-info.py

Permission problems
-------------------

If the script returns with an error

::

Access denied (insufficient permissions)

You can either run the script with sudo, for example:

::

sudo python example-info.py

Or you can add a udev rule to allow any user to access the device
without root permissions with this single command:

::

echo "SUBSYSTEM==\"usb\", ATTR{idVendor}==\"20a0\", ATTR{idProduct}==\"41e5\", MODE:=\"0666\"" | sudo tee /etc/udev/rules.d/85-blinkstick.rules

Reboot computer after you have added the command and all users will have
permissions to access the device without the need of root permissions.

Maintainers
-----------

- Arvydas Juskevicius - http://twitter.com/arvydev
- Rob Berwick - http://twitter.com/robberwick

111 changes: 111 additions & 0 deletions bin/blinkstick-connect.py
@@ -0,0 +1,111 @@
import websocket
import json
import sys
from blinkstick import blinkstick

print "Connect to BlinkStick.com and control BlinkStick remotely"
print "(c) Agile Innovative Ltd"
print ""

if len(sys.argv) > 1:
access_code = sys.argv[1]
else:
print "Usage:"
print " [sudo] python example-connect.py \"AccessCode\""
print ""
print "You can obtain the AccessCode parameter from the device information page on BlinkStick.com"
sys.exit()

bstick = blinkstick.find_first()

if bstick is None:
sys.exit("BlinkStick not found...")


def HTMLColorToRGB(colorstring):
""" convert #RRGGBB to an (R, G, B) tuple """
colorstring = colorstring.strip()
if colorstring[0] == '#': colorstring = colorstring[1:]
if len(colorstring) != 6:
raise ValueError, "input #%s is not in #RRGGBB format" % colorstring
r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
r, g, b = [int(n, 16) for n in (r, g, b)]
return (r, g, b)


def on_message(ws, message):
global client_id
global access_code
global bstick

#Uncomment this for debugging purposes
#print message

m = json.loads(message)

if m[0]['channel'] == '/meta/connect':
ws.send(json.dumps(
{'channel': '/meta/connect',
'clientId': client_id,
'connectionType': 'websocket'}))
return

elif m[0]['channel'] == '/meta/handshake':
client_id = m[0]['clientId']

print "Acquired clientId: " + client_id

ws.send(json.dumps(
{'channel': '/meta/subscribe',
'clientId': client_id,
'subscription': '/devices/' + access_code}))
return

elif m[0]['channel'] == '/devices/' + access_code:
if 'color' in m[0]["data"]:
print "Received color: " + m[0]["data"]["color"]

(r, g, b) = HTMLColorToRGB(m[0]["data"]["color"])
bstick.set_color(r, g, b)
elif 'status' in m[0]["data"] and m[0]["data"]['status'] == "off":
print "Turn off"
bstick.turn_off()

elif m[0]['channel'] == '/meta/subscribe':
if m[0]['successful']:
print "Subscribed to device. Waiting for color message..."
else:
print "Subscription to the device failed. Please check the access_code value in the file."

#Reconnect again and wait for further messages
ws.send(json.dumps(
{'channel': '/meta/connect',
'clientId': client_id,
'connectionType': 'websocket'}))


def on_error(ws, error):
print error


def on_close(ws):
print "### closed ###"


def on_open(ws):
ws.send(json.dumps(
{'channel': '/meta/handshake',
'version': '1.0',
'supportedConnectionTypes': ['long-polling', 'websocket']}))


if __name__ == "__main__":
# Set this to True for debugging purposes
websocket.enableTrace(False)
ws = websocket.WebSocketApp("ws://live.blinkstick.com:9292/faye",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open

ws.run_forever()
22 changes: 22 additions & 0 deletions bin/blinkstick-cpu.py
@@ -0,0 +1,22 @@
#!/usr/bin/python

from blinkstick import blinkstick
import psutil

print "Display CPU usage with BlinkStick"
print "(c) Agile Innovative Ltd"
print ""

bstick = blinkstick.find_first()

if bstick is None:
print "No BlinkSticks found..."
else:
print "Displaying CPU usage (Green = 0%, Amber = 50%, Red = 100%)"
print "Press Ctrl+C to exit"

while True:
cpu = psutil.cpu_percent(interval=1)
intensity = int(255 * cpu / 100)

bstick.set_color(intensity, 255 - intensity, 0)
14 changes: 14 additions & 0 deletions bin/blinkstick-find.py
@@ -0,0 +1,14 @@
#!/usr/bin/python

from blinkstick import blinkstick

print "Find BlinkStick by serial number"
print "(c) Agile Innovative Ltd"
print ""

bstick = blinkstick.find_by_serial("BS000001-1.0")

if bstick is None:
print "Not found..."
else:
print "BlinkStick found. Current color: " + bstick.get_color_string()
16 changes: 16 additions & 0 deletions bin/blinkstick-info.py
@@ -0,0 +1,16 @@
#!/usr/bin/python

from blinkstick import blinkstick

print "Display BlinkStick info"
print "(c) Agile Innovative Ltd"
print ""

for bstick in blinkstick.find_all():
print "Found device:"
print " Manufacturer: " + bstick.get_manufacturer()
print " Description: " + bstick.get_description()
print " Serial: " + bstick.get_serial()
print " Current Color: " + bstick.get_color_string()
print " Info Block 1: " + bstick.get_info_block1()
print " Info Block 2: " + bstick.get_info_block2()
15 changes: 15 additions & 0 deletions bin/blinkstick-infoblock.py
@@ -0,0 +1,15 @@
#!/usr/bin/python

from blinkstick import blinkstick

print "Display BlinkStick Name (InfoBlock1)"
print "(c) Agile Innovative Ltd"
print ""

bstick = blinkstick.find_first()
bstick.set_info_block1("Kitchen BlinkStick")
print bstick.get_info_block1()

# set and get device info-block2 here
#bstick.set_info_block2("info-block-2data")
#print bstick.get_info_block2()
11 changes: 11 additions & 0 deletions bin/blinkstick-off.py
@@ -0,0 +1,11 @@
#!/usr/bin/python

from blinkstick import blinkstick

print "Turn BlinkSticks off"
print "(c) Agile Innovative Ltd"
print ""

for bstick in blinkstick.find_all():
bstick.turn_off()
print bstick.get_serial() + " turned off"
11 changes: 11 additions & 0 deletions bin/blinkstick-random.py
@@ -0,0 +1,11 @@
#!/usr/bin/python

from blinkstick import blinkstick

print "Set random BlinkStick color"
print "(c) Agile Innovative Ltd"
print ""

for bstick in blinkstick.find_all():
bstick.set_random_color()
print bstick.get_serial() + " " + bstick.get_color_string()

0 comments on commit 793befa

Please sign in to comment.