跳转到主要内容

官方blink(1)控制库

项目描述

官方blink(1) USB RGB LED通知设备Python库 https://blink1.thingm.com/

关于此库

此库的功能

  • 对所有库组件的测试覆盖率

  • Python 3.x和Python 2.7.x兼容

  • 通过Python包索引自动安装

  • 对blink(1)的高级控制

  • 使用 cython-hidapi USB HID API 的单一实现(PyUSB 无法在所有操作系统上访问 HID 设备)

此库位于 https://github.com/todbot/blink1-python

最初由 @salimfadhley 编写,位于 https://github.com/salimfadhley/blink1/tree/master/python/pypi。由 @todbot 将其移至此存储库并重新编写以使用 cython-hidapi

安装

使用 pip 工具一次性获取此软件包的最新版本以及任何其他所需组件

pip install blink1

示例代码和已安装脚本

安装此库时,将安装两个命令行脚本 blink1-shineblink1-flash。 * blink1-shine – 告诉 blink(1) 显示特定的固定颜色 * blink1-flash – 以特定速率在 blink(1) 上闪烁两种不同的颜色

有关示例,请参阅 `blink1_demo` <./blink1_demo/>__ 目录中有关如何使用此库的多个示例。

操作系统特定说明

blink1-python 库依赖于 cython-hidapi 进行 USB HID 访问。在安装此库之前,可能需要安装 C 编译器和相关工具。

Linux

必须安装以下附加包

sudo apt-get install python-dev libusb-1.0-0-dev libudev-dev

以及非 root 用户访问 blink(1) 设备的 udev 规则

echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="27b8", ATTRS{idProduct}=="01ed", MODE:="666", GROUP="plugdev"' | sudo tee /etc/udev/rules.d/51-blink1.rules
sudo udevadm control --reload
sudo udevadm trigger

Mac OS X

使用命令行工具安装 Xcode

Windows

您将需要 Microsoft Visual C++ Compiler for Python 2.7

使用

使用此库的最简单方法是通过上下文管理器。

import time
from blink1.blink1 import blink1

with blink1() as b1:
  b1.fade_to_color(100, 'navy')
  time.sleep(10)

当 blink1() 块退出时,灯会自动关闭。也可以不使用上下文管理器访问完全相同的函数集

import time
from blink1.blink1 import Blink1

b1 = Blink1()
b1.fade_to_rgb(1000, 64, 64, 64)
time.sleep(3)
b1.fade_to_rgb(1000, 255, 255, 255)

与上下文管理器不同,此演示将在执行结束时留下 blink(1) 打开。要关闭它,请使用 b1.close() 方法。

要列出所有连接的 blink(1) 设备

from blink1.blink1 import Blink1
blink1_serials = Blink1.list()
print("blink(1) devices found: " + ','.join(blink1_serials))

要通过序列号打开特定的 blink(1) 设备,请传入其序列号作为 Unicode 字符串

from blink1.blink1 import blink1
blink1 = Blink1(serial_number=u'20002345')
blink1.fade_to_rgb(1000, 255,0,255)
blink1.close()

颜色

在此库中,有多种方式可以指定颜色

blink1.fade_to_color(1000, '#ffffff') # Hexdecimal RGB as a string
blink1.fade_to_color(1000, 'green') # Named color - any color name understood by css3
blink1.fade_to_color(1000, (22,33,44) # RGB as a tuple. Luminance values are 0 <= lum <= 255

尝试在可能的范围内选择颜色将生成 InvalidColor 异常。

模式播放

blink(1) 设备具有一个 16 行的非易失性颜色模式内存。如果施加电源但未连接到计算机,则该颜色模式将自动播放。您还可以通过 USB 触发此模式(或子模式),使您的应用程序可以自由地执行除闪烁灯光之外的其他操作。

颜色模式中的每一行都由一个 R、G、B 三联体和一个到达该颜色的淡入时间组成。

要播放 blink(1) 中的模式或子模式

blink1.play()  # play entire color pattern, infinitely looped
blink1.stop()  # stop a color pattern playing (if playing)

blink1.play(2,3, count=7)  # play color pattern lines 2,3 in a loop 7 times

要更改模式内存中的行

# write 100msec fades to green then yellow then black at lines 3,4,5
blink1.write_pattern_line( 100, 'green',  3)
blink1.write_pattern_line( 100, 'yellow', 4)
blink1.write_pattern_line( 100, 'black',  5)

blink1.play( 3,5, 4)  # play that sub-loop 4 times

要将模式保存到非易失性内存(覆盖出厂模式)

blink1.savePattern()

要快速以 Blink1Control 样式的字符串格式播放模式

# play purple on LED1 in 300ms, green on LED2 in 100ms, then swap, for 10 times
pattern_str = '10, #ff00ff,0.3,1, #00ff00,0.1,2,  #ff00ff,0.3,2, #00ff00,0.1,1'
blink1.play_pattern(pattern_str)
# wait 5 seconds while the pattern plays on the blink1
# (or go do something more useful)
time.sleep(5.0)
# flash red-off 5 times fast on all LEDs
blink1.play_pattern('5, #FF0000,0.2,0,#000000,0.2,0')

服务器监视狗

blink(1) 还有一种类似于“看门狗”的机制,称为“servertickle”。当启用时,您必须定期将其发送到 blink(1),否则它将触发,播放存储的颜色模式。这对于宣布已崩溃的计算机很有用。blink(1) 将自行闪烁,直到被告知 otherwise。

要使用,启用 servertickle 并指定超时值(最大超时 62 秒)

blink1.server_tickle(enable=True, timeout_millis=2000)

伽玛校正

上下文管理器支持一个“gamma”参数,允许您提供每个通道的伽玛校正值。

from blink1.blink1 import blink1

with blink1(gamma=(2, 2, 2)) as b1:
  b1.fade_to_color(100, 'pink')
  time.sleep(10)

此示例为三个颜色通道提供伽玛校正 2。

伽玛值越高,blink(1) 的颜色看起来就越丰富,但颜色的亮度会降低。

白点校正

环境光线会影响人眼对颜色的感知。在某些情况下,可能需要应用小的颜色校正,以使颜色看起来更准确。例如,如果我们在一个主要由蜡烛照明的小屋里操作 blink(1)

with blink1(white_point='candle', switch_off) as b1:
  b1.fade_to_color(100, 'white')

在日光下观察,这会使Blink(1)看起来呈黄色,然而在烛光下房间中,这会被感知为更自然的白色。如果我们不应用这种颜色校正,Blink(1)将呈现出蓝色。

以下值是可接受的白色点

  • 任何三原色(r,g,b)。每个0 <= 亮度 <= 255

  • 任何以开尔文整数或浮点数表示的颜色温度

  • 一个颜色温度名称。

库支持以下温度名称

  • 蜡烛

  • 日出

  • 白炽灯

  • 钨丝

  • 卤素

  • 日光

  • 阴天

  • 阴凉处

  • 蓝天

API参考

Help on class Blink1 in blink1.blink1:

blink1.blink1.Blink1 = class Blink1
 |  Light controller class, sends messages to the blink(1) via USB HID.
 |
 |  Methods defined here:
 |
 |  __init__(self, serial_number=None, gamma=None, white_point=None)
 |      :param serial_number: serial number of blink(1) to open, otherwise first found
 |      :param gamma: Triple of gammas for each channel e.g. (2, 2, 2)
 |
 |  clear_pattern(self)
 |      Clear entire color pattern in blink(1)
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  close(self)
 |
 |  fade_to_color(self, fade_milliseconds, color, ledn=0)
 |      Fade the light to a known colour
 |      :param fade_milliseconds: Duration of the fade in milliseconds
 |      :param color: Named color to fade to (e.g. "#FF00FF", "red")
 |      :param ledn: which led to control
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  fade_to_rgb(self, fade_milliseconds, red, green, blue, ledn=0)
 |      Command blink(1) to fade to RGB color
 |      :param fade_milliseconds: millisecs duration of fade
 |      :param red: 0-255
 |      :param green: 0-255
 |      :param blue: 0-255
 |      :param ledn: which LED to control (0=all, 1=LED A, 2=LED B)
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  fade_to_rgb_uncorrected(self, fade_milliseconds, red, green, blue, ledn=0)
 |      Command blink(1) to fade to RGB color, no color correction applied.
 |      :raises: Blink1ConnectionFailed if blink(1) is disconnected
 |
 |  get_serial_number(self)
 |      Get blink(1) serial number
 |      :return blink(1) serial number as string
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  get_version(self)
 |      Get blink(1) firmware version
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  notfound(self)
 |
 |  off(self)
 |      Switch the blink(1) off instantly
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  play(self, start_pos=0, end_pos=0, count=0)
 |      Play internal color pattern
 |      :param start_pos: pattern line to start from
 |      :param end_pos: pattern line to end at
 |      :param count: number of times to play, 0=play forever
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  play_pattern(self, pattern_str, onDevice=True)
 |      Play a Blink1Control-style pattern string
 |      :param pattern_str: The Blink1Control-style pattern string to play
 |      :param onDevice: True (default) to run pattern on blink(1),
 |                       otherwise plays in Python process
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  play_pattern_local(self, pattern_str)
 |      Play a Blink1Control pattern string in Python process
 |          (plays in blink1-python, so blocks)
 |      :param pattern_str: The Blink1Control-style pattern string to play
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  read(self)
 |      Read command result from blink(1), low-level internal use
 |      Receive USB Feature Report 0x01 from blink(1) with 8-byte payload
 |      Note: buf must be 8 bytes or bad things happen
 |
 |  read_pattern(self)
 |      Read the entire color pattern
 |      :return List of pattern line tuples
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  read_pattern_line(self, pos)
 |      Read a color pattern line at position
 |      :param pos: pattern line to read
 |      :return pattern line data as tuple (r,g,b, step_millis) or False on err
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  save_pattern(self)
 |      Save internal RAM pattern to flash
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  server_tickle(self, enable, timeout_millis=0, stay_lit=False, start_pos=0, end_pos=16)
 |      Enable/disable servertickle / serverdown watchdog
 |      :param: enable: Set True to enable serverTickle
 |      :param: timeout_millis: millisecs until servertickle is triggered
 |      :param: stay_lit: Set True to keep current color of blink(1), False to turn off
 |      :param: start_pos: Sub-pattern start position in whole color pattern
 |      :param: end_pos: Sub-pattern end position in whole color pattern
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  set_ledn(self, ledn=0)
 |      Set the 'current LED' value for writePatternLine
 |      :param ledn: LED to adjust, 0=all, 1=LEDA, 2=LEDB
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  stop(self)
 |      Stop internal color pattern playing
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  write(self, buf)
 |      Write command to blink(1), low-level internal use
 |      Send USB Feature Report 0x01 to blink(1) with 8-byte payload
 |      Note: arg 'buf' must be 8 bytes or bad things happen
 |      :raises: Blink1ConnectionFailed if blink(1) is disconnected
 |
 |  write_pattern_line(self, step_milliseconds, color, pos, ledn=0)
 |      Write a color & step time color pattern line to RAM
 |      :param step_milliseconds: how long for this pattern line to take
 |      :param color: LED color
 |      :param pos: color pattern line number (0-15)
 |      :param ledn: LED number to adjust, 0=all, 1=LEDA, 2=LEDB
 |      :raises: Blink1ConnectionFailed: if blink(1) is disconnected
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  color_to_rgb(color)
 |      Convert color name or hexcode to (r,g,b) tuple
 |      :param color: a color string, e.g. "#FF00FF" or "red"
 |      :raises: InvalidColor: if color string is bad
 |
 |  find(serial_number=None)
 |      Find a praticular blink(1) device, or the first one
 |      :param serial_number: serial number of blink(1) device (from Blink1.list())
 |      :raises: Blink1ConnectionFailed: if blink(1) is not present
 |
 |  list()
 |      List blink(1) devices connected, by serial number
 |      :return: List of blink(1) device serial numbers
 |
 |  parse_pattern(pattern_str)
 |      Parse a Blink1Control pattern string to a list of pattern lines
 |          e.g. of the form '10,#ff00ff,0.1,0,#00ff00,0.1,0'
 |      :param pattern_str: The Blink1Control-style pattern string to parse
 |      :returns: an list of dicts of the parsed out pieces

开发者安装

检查了blink1-python库后,切换到其目录并运行安装脚本

git clone https://github.com/todbot/blink1-python
cd blink1-python
python3 setup.py develop
python3 ./blink1_demo/demo1.py

pip3 install --editable .

现在您可以在系统上使用blink1包并对其进行编辑。

要获取内部blink1库调试信息,设置环境变量DEBUGBLINK1

DEBUGBLINK1=1 python3 ./blink1_demo/demo1.py

要卸载开发版本

python3 setup.py develop --uninstall

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

blink1-0.3.1.tar.gz (18.0 kB 查看哈希值)

上传时间

由以下支持