forked from tsileo/pyblinkm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyblinkm.py
169 lines (129 loc) · 4.38 KB
/
pyblinkm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- encoding: utf-8 -*-
GO_TO_RGB = 0x6e
FADE_TO_RGB = 0x63
FADE_TO_HSB = 0x68
FADE_TO_RANDOM_RGB = 0x43
FADE_TO_RANDOM_HSB = 0x48
PLAY_LIGHT_SCRIPT = 0x70
STOP_SCRIPT = 0x6f
SET_FADE_SPEED = 0x66
SET_TIME_ADJUST = 0x74
GET_CURRENT_RGB = 0x67
WRITE_SCRIPT_LINE = 0x57
READ_SCRIPT_LINE = 0x52
SET_SCRIPT_LENGTH_AND_REPEATS = 0x4c
SET_BLINKM_ADDRESS = 0x41
GET_BLINKM_ADDRESS = 0x61
GET_BLINKM_FIRMWARE_VERSION = 0x5a
SET_STARTUP_PARAMETERS = 0x42
class Scripts:
"""Default BlinkM Scripts Reference."""
STARTUP = 0
RGB = 1
WHITE_FLASH = 2
RED_FLASH = 3
GREEN_FLASH = 4
BLUE_FLASH = 5
CYAN_FLASH = 6
MAGENTA_FLASH = 7
YELLOW_FLASH = 8
BLACK = 9
HUE_CYCLE = 10
MOOD_LIGHT = 11
VIRTUAL_CANDLE = 12
WATER_REFLECTIONS = 13
OLD_NEON = 14
THE_SEASONS = 15
THUNDERSTORM = 16
STOP_LIGHT = 17
MORSE_CODE = 18
class I2C:
"""I2C Connection Manager
:param bus: I2C Bus
:param addr: I2C address
"""
def __init__(self, bus=1, addr=0x09):
# import on top makes readthedocs build fail
import smbus
self.bus = smbus.SMBus(bus)
self.addr = addr
def _write_bytes(self, *bytes):
"""Write bytes at I2C address."""
for byte in bytes:
self.bus.write_byte(self.addr, byte)
def _read_bytes(self, nb_bytes=1):
"""Read bytes at I2C address
:param nb_bytes: Number of bytes to read
"""
for i in range(nb_bytes):
yield self.bus.read_byte(self.addr)
class BlinkM(I2C):
"""Drop dead simple BlinkM control.
:param bus: I2C Bus
:param addr: I2C address
"""
def reset(self):
"""Stop script and fade to black."""
self.stop_script()
self.fade_to()
def stop_script(self):
"""Stop playing script."""
self._write_bytes(STOP_SCRIPT)
def go_to(self, r=0, g=0, b=0):
"""Go to RGB Color Now."""
self._write_bytes(GO_TO_RGB, r, g, b)
def go_to_hex(self, hex_color):
"""Go to Hexadecimal Color Now."""
r, g, b = tuple(ord(c) for c in hex_color.decode('hex'))
self.go_to(r, g ,b)
def fade_to(self, r=0, g=0, b=0):
"""Fade to RGB Color."""
self._write_bytes(FADE_TO_RGB, r, g, b)
def fade_to_hex(self, hex_color):
"""Fade to Hexadecimal Color."""
r, g ,b = tuple(ord(c) for c in hex_color.decode('hex'))
self.fade_to(r, g, b)
def fade_to_hsb(self, h=0, s=0, b=0):
"""Fade to HSB Color.
:param h: Hue (0-360°)
:param s: Saturation (0-100%)
:param b: Brightness (0-100%)
"""
h = h * 255 / 360
s = s * 255 / 100
b = b * 255 / 100
self._write_bytes(FADE_TO_HSB, h, s, b)
def fade_to_percent(self, percent=0):
"""Fade to color from Blue (Cold) to Red (Hot).
Takes an input from 0-100 and convert it to HSB from 180/100/100 to 0/100/100.
"""
h = 180 - 180 * percent / 100
self.fade_to_hsb(h, 100, 100)
def fade_to_random_rgb(self, r=0, g=0, b=0):
"""Fade to Random RGB Color."""
self._write_bytes(FADE_TO_RANDOM_RGB, r, g, b)
def play_script(self, script_number, repeat=0, start_line=0):
"""Play Light Script."""
self._write_bytes(PLAY_LIGHT_SCRIPT, script_number, repeat, start_line)
def set_fade_speed(self, fade_speed):
"""Set Fade Speed.
:param fade_speed: fade speed from 1-255.
"""
self._write_bytes(SET_FADE_SPEED, fade_speed)
def set_time_adjust(self, adjust):
"""Set Time Adjust.
Adjusts the playback speed of a light script.
:param adjust: Relative adjustement between -128 and 127.
"""
def get_rgb_color(self):
"""Get Current RGB Color.
Returns current red, green and blue channels."""
self._write_bytes(GET_CURRENT_RGB)
r, g, b = self._read_bytes(3)
return r, g ,b
def write_script_line(self, script_number, line_number, duration, command, value1=0, value2=0, value3=0):
"""Write Light Script Line"""
self._write_bytes(WRITE_SCRIPT_LINE, script_number, line_number, duration, command, value1, value2, value3)
def set_script_length_and_repeats(self, script_number, length, repeats):
"""Set Light Script Length"""
self._write_bytes(SET_SCRIPT_LENGTH_AND_REPEATS, script_number, length, repeats)