This is a Python library for controlling Lightpack
It is a fork of the official Python library, which at the time of writing is buggy, unpackaged, unmaintained and undocumented. The goal of this project is to address those issues and make other improvements.
Python 2 and 3 are supported.
Install from PiPI:
sudo pip install py-lightpack
Or install from source by cloning this repository and running
sudo python setup.py install
See the code or pydoc lightpack
for full documentation.
There are a number of breaking changes from the original library, though code should not be difficult to migrate. The changes include the following:
- The class name is now in studly case, so use
lightpack.Lightpack()
instead oflightpack.lightpack()
. - The constructor now takes named arguments rather than expecting them in a
particular order, and the
apikey
argument has been renamed toapi_key
. setSmooth
has been renamed tosetSmoothness
.getAPIStatus
has been renamed togetApiStatus
.- Colours are now passed to
setColour
and friends as a single tuple of red, green and blue values rather than separate arguments for each. So uselp.setColourToAll((10, 255, 128))
rather thanlp.setColourToAll(10, 255, 128)
. - When failing to connect (or when authentication fails during connection) there
is now a much more reliable
CannotConnectError
. - Method calls now raise a
CommandFailedError
on failure where before they were silent.
Methods with the British spellings "colour" now exist, but the American "color" spellings are still supported.
from __future__ import print_function
import lightpack
from time import sleep
import sys
# Configuration
# host = 'localhost' # (default)
# port = 3636 # (default)
led_map = [ # Optional aliases for the LEDs in order
'bottom-right',
'right-bottom',
'right-top',
'top-far-right',
'top-right',
'top-left',
'top-far-left',
'left-top',
'left-bottom',
'bottom-left',
]
# api_key = '{secret-code}' # Default is None
# Connect to the Lightpack API
lp = lightpack.Lightpack(led_map=led_map)
try:
lp.connect()
except lightpack.CannotConnectError as e:
print(repr(e))
sys.exit(1)
# Lock the Lightpack so we can make changes
lp.lock()
# Flash green three times
for i in range(3):
# The American spellings such as setColorToAll are available as aliases
lp.setColourToAll((0, 255, 0))
sleep(0.2)
lp.setColourToAll((0, 0, 0))
sleep(0.2)
# Set top right light to yellow
# The Colour class is optional
from colour import Colour
lp.setColour('top-right', Colour('yellow'))
sleep(1)
# Set left bottom and left right lights to two other colours
lp.setColours(('left-bottom', Colour('red')), ('left-top', Colour('goldenrod')))
sleep(1)
# Unlock to release control (the disconnect method actually calls this
# automatically, but it is often useful on its own so is here for informational
# purposes)
lp.unlock()
# Disconnect
lp.disconnect()