-
Notifications
You must be signed in to change notification settings - Fork 16
/
bulb.rb
421 lines (338 loc) · 9.12 KB
/
bulb.rb
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
module Hue
class Bulb
attr_accessor :id, :stash, :options
def initialize(light_num, options = {})
self.id = light_num
self.options = options
end
def status
JSON.parse Net::HTTP.get(Bridge.uri('lights', id))
end
def states
status['state']
end
def [](item)
states[item.to_s]
end
def update(settings = {})
puts @options.merge(settings).inspect
Bridge.update Bridge.uri('lights', id, 'state'), @options.merge(settings)
end
def name
status['name']
end
def name=(_name)
Bridge.update uri('lights', light_id), name: _name
end
def on?
self[:on]
end
def off?
!on?
end
def on
update on: true
on?
end
def off
update on: false
off?
end
def brightness
self[:bri]
end
def brightness=(bri)
update bri: bri
brightness
end
def hue
self[:hue]
end
def hue=(_hue)
_hue = (_hue * (65536.0 / 360)).to_i
update hue: _hue
hue
end
def sat
self[:sat]
end
def sat=(_sat)
update sat: _sat
sat
end
def transition_time
# transition time in seconds
(options[:transitiontime] || 1).to_f / 10
end
def transition_time=(time)
# transition time in seconds
self.options[:transitiontime] = (time * 10).to_i
end
def colortemp
self[:ct]
end
alias :ct :colortemp
def colortemp=(_ct)
update ct: [[_ct, 154].max, 500].min
colortemp
end
alias :ct= :colortemp=
def colormode
self[:colormode]
end
def blinking?
!!(self['alert'] =~ /l?select/)
end
def blink(start = true)
update(alert: (start ? 'lselect' : 'none'))
end
def solid
update alert: 'none'
end
def flash
update alert: 'select'
update alert: 'none'
end
def settings
state = states
options.merge case state['colormode']
when 'ct'
{'ct' => state['ct']}
when 'xy'
{'xy' => state['xy']}
when 'hs'
{'hue' => state['hue'], 'sat' => state['sat']}
end.merge('on' => state['on'], 'bri' => state['bri'])
end
def rgb
send %(#{colormode}_to_rgb)
end
def red
rgb[:red]
end
def green
rgb[:green]
end
def blue
rgb[:blue]
end
def red=(_red)
self.rgb = [_red, green, blue]
end
def green=(_green)
self.rgb = [red, _green, blue]
end
def blue=(_blue)
self.rgb = [red, green, _blue]
end
def kelvin
# convert colortemp setting to Kelvin
1000000 / self['ct']
end
def kelvin=(_temp)
self.colortemp = 1000000 / [_temp, 1].max
end
def ct_to_rgb
# using method described at
# http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
temp = kelvin / 100
red = temp <= 66 ? 255 : 329.698727446 * ((temp - 60) ** -0.1332047592)
green = if temp <= 66
99.4708025861 * Math.log(temp) - 161.1195681661
else
288.1221695283 * ((temp - 60) ** -0.0755148492)
end
blue = if temp >= 66
255
elsif temp <= 19
0
else
138.5177312231 * Math.log(temp - 10) - 305.0447927307
end
{ red: [[red, 0].max, 255].min.to_i,
green: [[green, 0].max, 255].min.to_i,
blue: [[blue, 0].max, 255].min.to_i
}
end
def xyz
vals = states['xy']
vals + [1 - vals.first - vals.last]
end
def xy_to_rgb
values = (RGB_MATRIX * Matrix[xyz].transpose).to_a.flatten.map{|x| [[x * 255, 0].max, 255].min.to_i}
{ red: values[0],
green: values[1],
blue: values[2]
}
end
def hue_in_degrees
self['hue'].to_f / (65536.0 / 360)
end
def hue_as_decimal
hue_in_degrees / 360
end
def sat_as_decimal
self['sat'] / 255.0
end
def brightness_as_decimal
brightness / 255.0
end
def hs_to_rgb
h, s, v = hue_as_decimal, sat_as_decimal, brightness_as_decimal
if s == 0 #monochromatic
red = green = blue = v
else
v = 1.0 # We are setting the value to 1. Don't count brightness here
i = (h * 6).floor
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
case i % 6
when 0
red, green, blue = v, t, p
when 1
red, green, blue = q, v, p
when 2
red, green, blue = p, v, t
when 3
red, green, blue = p, q, v
when 4
red, green, blue = t, p, v
when 5
red, green, blue = v, p, q
end
end
{ red: [[red * 255, 0].max, 255].min.to_i,
green: [[green * 255, 0].max, 255].min.to_i,
blue: [[blue * 255, 0].max, 255].min.to_i
}
end
def rgb=(colors)
red, green, blue = colors[0] / 255.0, colors[1] / 255.0, colors[2] / 255.0
max = [red, green, blue].max
min = [red, green, blue].min
h, s, l = 0, 0, ((max + min) / 2 * 255)
d = max - min
s = max == 0 ? 0 : (d / max * 255)
h = case max
when min
0 # monochromatic
when red
(green - blue) / d + (green < blue ? 6 : 0)
when green
(blue - red) / d + 2
when blue
(red - green) / d + 4
end * 60 # / 6 * 360
h = (h * (65536.0 / 360)).to_i
update hue: h, sat: s.to_i#, bri: l.to_i
[h, s, 1.0]
end
def stash!
self.stash ||= settings
end
def restore!
if stash
update stash
unstash!
end
end
def unstash!
self.stash = nil
end
def candle(repeat = 15)
# 0-65536 for hue, 182 per deg. Ideal 30-60 deg (5460-10920)
stash!
on if off?
repeat.times do
hue = ((rand * 3460) + 5460).to_i
sat = rand(64) + 170
bri = rand(32) + 16
delay = (rand * 0.35) + (@delay ||= 0)
update(hue: hue, sat: sat, bri: bri, transitiontime: (delay * 10).to_i)
sleep delay
end
restore!
end
# Experimental Sunrise/Sunset action
# this will transition from off and warm light to on and daytime light
# in a curve that mimics the actual sunrise.
def perform_sunrise(total_time_in_minutes = 18)
# total_time / 18 steps == time_per_step
# the multiplier should be 600 * time per step
minutes_per_step = total_time_in_minutes / 18.0
multiplier = (minutes_per_step * 60 * 10).to_i
perform_sun_transition total_time_in_minutes, sunrise_steps(multiplier)
end
def perform_sunrise(total_time_in_minutes = 18)
multiplier = sunrise_multiplier total_time_in_minutes
steps = sunrise_steps(multiplier)
if on?
puts "ON! #{steps[0][:bri]} :: #{brightness} :: #{brightness > steps[0][:bri]}"
while brightness >= steps[0][:bri]
steps.shift
end
end
steps.each_with_index do |step, i|
update step.merge(on: true)
sleep(step[:transitiontime] / 10.0)
end
end
def perform_sunset(total_time_in_minutes = 18)
multiplier = sunrise_multiplier total_time_in_minutes
steps = sunset_steps(multiplier)
if on?
puts "ON! #{steps[0][:bri]} :: #{brightness} :: #{brightness > steps[0][:bri]}"
while brightness <= steps[0][:bri]
steps.shift
end
end
steps.each_with_index do |step, i|
update step.merge(on: true)
sleep(step[:transitiontime] / 10.0)
end
off
end
SUN_STEPS = [ 1.5, 2, 3, 1, 4, 2.5 ]
SUN_TIMES = [ 3, 3, 3, 1, 2, 1]
def sunrise_multiplier(total_time_in_minutes)
# total_time / 18 steps == time_per_step
# the multiplier should be 600 * time per step
minutes_per_step = total_time_in_minutes / 18.0
(minutes_per_step * 60 * 10).to_i
end
def sunrise_brightness
sun_bri_unit = 10
SUN_STEPS.inject([0]){|all, i| all << ((i * sun_bri_unit) + all[-1]).to_i } << 255
end
def sunrise_temps
sun_temp_unit = 16
SUN_STEPS.inject([500]){|all, i| all << (all[-1] - (i * sun_temp_unit)).to_i} << 200
end
def sunrise_times
[0, SUN_TIMES, 5].flatten
end
def sunset_times
[0, 5, SUN_TIMES.reverse].flatten
end
def sunrise_steps(multiplier = 600)
bri_steps = sunrise_brightness
tmp_steps = sunrise_temps
steps = []
sunrise_times.each_with_index do |t, i|
steps << {bri: bri_steps[i], ct: tmp_steps[i], transitiontime: (t * multiplier)}
end
steps
end
def sunset_steps(multiplier = 600)
bri_steps = sunrise_brightness.reverse
tmp_steps = sunrise_temps.reverse
steps = []
sunset_times.each_with_index do |t, i|
steps << {bri: bri_steps[i], ct: tmp_steps[i], transitiontime: (t * multiplier)}
end
steps
end
end
end # Hue