Blinkenhub Python API#
Install the module and the blinkenhub command from PyPI:
pip install blinkenhub
Control library and command-line interface for the Blinkenhub Mac mini stand.
Blinkenhub is a USB-attached ring of 17 RGB LEDs. This module talks to it over
a serial port using a simple framed protocol: each command is serialized to
little-endian binary and followed by a CRC-8 (CCITT) checksum. ESC and
ETX bytes are escaped, and the frame ends with ETX. The device answers
each frame with a single A (ACK) or N (NACK) byte.
Colors are expressed in HSV: hue in degrees [0.0, 360.0], and saturation
and value (brightness) in [0.0, 1.0].
Example
Show a dim rainbow and spin it clockwise:
from blinkenhub import Blinkenhub, SetPixels, Rotate, mkrainbow
bh = Blinkenhub() # auto-detect the device
bh.send(SetPixels(mkrainbow(0.25), 0))
bh.send(Rotate(Rotate.CW, 2.0, -1))
The blinkenhub console script (see main()) exposes the same
functionality from the shell; run blinkenhub --help for details.
- blinkenhub.device = None#
Serial port used by
stop_animations()and the CLI commands.Nonemeans auto-detect. The CLI sets it from--port/BLINKENHUB_PORT.
- exception blinkenhub.TimeoutException[source]#
Bases:
ExceptionRaised when the Blinkenhub does not reply to a command before the serial timeout.
- class blinkenhub.HSV_Color(hue, saturation, value)[source]#
Bases:
objectA single pixel color in the HSV color space.
- h#
Hue in degrees,
[0.0, 360.0].- Type:
float
- s#
Saturation,
[0.0, 1.0].- Type:
float
- v#
Value (brightness),
[0.0, 1.0].- Type:
float
Create a color.
- Parameters:
hue (float) – Hue in degrees,
[0.0, 360.0].saturation (float) – Saturation,
[0.0, 1.0].value (float) – Value (brightness),
[0.0, 1.0].
- class blinkenhub.Blinkenhub(port=None)[source]#
Bases:
objectSerial connection to a Blinkenhub device.
Handles device discovery, framing, and the ACK/NACK handshake. Commands are built from
BlinkenhubCmdsubclasses and sent withsend().- ESC#
Escape byte; precedes any
ESCorETXbyte in the payload.- Type:
int
- ACK#
Reply sent by the device when it accepts a command.
- Type:
str
- NACK#
Reply sent by the device when it rejects a command.
- Type:
str
- ETX#
End-of-frame byte.
- Type:
int
- calculator#
CRC-8 (CCITT) calculator for frame checksums.
- Type:
crc.Calculator
- ser#
The open serial port.
- Type:
serial.Serial
Open a connection to a Blinkenhub.
- Parameters:
port (str, optional) – Serial device path, such as
/dev/cu.usbmodem1101. If omitted, the first USB serial port whose manufacturer isSystemixand product isBlinkenhubis used.- Raises:
Exception – If
portis omitted and no Blinkenhub is found.serial.SerialException – If the serial port cannot be opened.
- prepare_buf(buf)[source]#
Frame a raw payload for transmission.
Appends a CRC-8 checksum, escapes every
ESCandETXbyte by prefixing it withESC, and terminates the frame withETX.- Parameters:
buf (bytes) – The serialized command.
- Returns:
The framed bytes, ready to write to the serial port.
- Return type:
bytearray
- send(item, reply=True)[source]#
Send a command to the device.
- Parameters:
item (BlinkenhubCmd) – The command to send. Any object that supports
bytes()is accepted.reply (bool) – If
True(the default), wait for the device’s ACK/NACK reply.
- Returns:
Trueif the device acknowledged the command, or ifreplyisFalse.Falseif the device replied with anything other than ACK.- Return type:
bool
- Raises:
TimeoutException – If
replyisTrueand no reply arrives within the serial timeout (0.5 s).
- class blinkenhub.BlinkenhubCmd(cmd)[source]#
Bases:
objectBase class for all device commands.
Each command is sent as a 4-byte header (command ID, protocol version, and two padding bytes) followed by a command-specific payload. Subclasses must set
self.bufto that payload.- VERSION#
Protocol version sent in every command header.
- Type:
int
- CMD_ANIMATE_HUE#
Command ID for
HueAnimation.- Type:
int
- CMD_ANIMATE_SATURATION#
Command ID for
SaturationAnimation.- Type:
int
- CMD_ANIMATE_VALUE#
Command ID for
ValueAnimation.- Type:
int
- cmd#
This command’s ID.
- Type:
int
- buf#
This command’s payload, set by the subclass.
- Type:
bytes
Initialize the command header.
- Parameters:
cmd (int) – One of the
CMD_*command IDs.
- class blinkenhub.Animation(cmd, typ, rate, top, bottom, count)[source]#
Bases:
BlinkenhubCmdBase class for commands that animate one HSV channel of every pixel.
Use one of the subclasses:
HueAnimation,SaturationAnimation, orValueAnimation.- ANIMATE_NONE#
No animation.
- Type:
int
- ANIMATE_UP#
Ramp from
bottomtotop.- Type:
int
- ANIMATE_DOWN#
Ramp from
toptobottom.- Type:
int
- ANIMATE_UPDOWN#
Oscillate between
bottomandtop.- Type:
int
Build an animation command.
- Parameters:
cmd (int) – The
CMD_ANIMATE_*command ID.typ (int) – One of the
ANIMATE_*constants.rate (float) – Speed in cycles per second.
top (float) – Upper limit of the animated channel.
bottom (float) – Lower limit of the animated channel.
count (int) – Number of cycles to run;
-1runs forever.
- class blinkenhub.SetGamma(gamma)[source]#
Bases:
BlinkenhubCmdSet the gamma correction the device applies to pixel brightness.
Build a set-gamma command.
- Parameters:
gamma (float) – Gamma exponent;
1.0means no correction.
- class blinkenhub.SetPixels(colors, fade_time)[source]#
Bases:
BlinkenhubCmdSet the color of each pixel, with an optional fade.
- LEN_LIMIT#
Maximum number of pixels (the ring has 17).
- Type:
int
Build a set-pixels command.
- class blinkenhub.HueAnimation(typ=0, rate=0.5, top=1, bottom=0.1, count=-1)[source]#
Bases:
AnimationAnimate the hue of every pixel.
Build a hue animation command.
- Parameters:
typ (int) – One of the
Animation.ANIMATE_*constants.rate (float) – Speed in cycles per second.
top (float) – Upper hue limit in degrees,
[0.0, 360.0].bottom (float) – Lower hue limit in degrees,
[0.0, 360.0].count (int) – Number of cycles to run;
-1runs forever.
- class blinkenhub.SaturationAnimation(typ=0, rate=0.5, top=1, bottom=0.1, count=-1)[source]#
Bases:
AnimationAnimate the saturation of every pixel.
Build a saturation animation command.
- Parameters:
typ (int) – One of the
Animation.ANIMATE_*constants.rate (float) – Speed in cycles per second.
top (float) – Upper saturation limit,
[0.0, 1.0].bottom (float) – Lower saturation limit,
[0.0, 1.0].count (int) – Number of cycles to run;
-1runs forever.
- class blinkenhub.ValueAnimation(typ=0, rate=0.5, top=1, bottom=0.1, count=-1)[source]#
Bases:
AnimationAnimate the value (brightness) of every pixel.
Build a value (brightness) animation command.
- Parameters:
typ (int) – One of the
Animation.ANIMATE_*constants.rate (float) – Speed in cycles per second.
top (float) – Upper brightness limit,
[0.0, 1.0].bottom (float) – Lower brightness limit,
[0.0, 1.0].count (int) – Number of cycles to run;
-1runs forever.
- class blinkenhub.Rotate(typ, rate, limit)[source]#
Bases:
BlinkenhubCmdRotate the current pixel pattern around the ring.
- NONE#
Stop rotating.
- Type:
int
- CW#
Rotate clockwise.
- Type:
int
- CCW#
Rotate counterclockwise.
- Type:
int
- CYLON#
Sweep back and forth over
limitpixels.- Type:
int
Build a rotate command.
- blinkenhub.mkrainbow(value)[source]#
Build a rainbow that spans the full hue circle across the ring.
- Parameters:
value (float) – Brightness of every pixel,
[0.0, 1.0].- Returns:
17 fully saturated colors with evenly spaced hues.
- Return type:
list[HSV_Color]
- blinkenhub.stop_animations()[source]#
Stop any rotation and channel animations running on the device.
Connects to the port in the module-level
devicevariable, which the CLI sets from--port.
- blinkenhub.mk_random_colors(value)[source]#
Build a random pattern in which about half the pixels are lit.
- Parameters:
value (float) – Brightness of the lit pixels,
[0.0, 1.0].- Returns:
17 colors. Each is either off or a fully saturated random hue.
- Return type:
list[HSV_Color]
- blinkenhub.are_we_awake(cg)[source]#
Report whether any active display on this Mac is awake (macOS only).
- Parameters:
cg (ctypes.CDLL) – The loaded CoreGraphics framework, with
restypeset toctypes.c_intforCGDisplayIsAsleepandCGGetActiveDisplayList.- Returns:
Trueif at least one active display is awake, otherwiseFalse.- Return type:
bool