Class: CheekyPi::Light
- Inherits:
-
Object
- Object
- CheekyPi::Light
- Defined in:
- lib/cheeky-pi/light.rb
Constant Summary collapse
- MAX =
90
Instance Method Summary collapse
- #animation ⇒ Object
- #animation=(new_animation) ⇒ Object
- #building ⇒ Object
- #color ⇒ Object
- #failure ⇒ Object
-
#initialize(path) ⇒ Light
constructor
Initialize with a path and optional color.
- #off ⇒ Object
-
#set(color) ⇒ Object
Set light to a given color.
- #stop_animation ⇒ Object
- #success ⇒ Object
- #throb(period, from, to) ⇒ Object
- #throb!(period, from, to) ⇒ Object
- #transition(to, duration = 2) ⇒ Object
- #transition!(to) ⇒ Object
Constructor Details
#initialize(path) ⇒ Light
Initialize with a path and optional color
11 12 13 14 |
# File 'lib/cheeky-pi/light.rb', line 11 def initialize(path) @light = LightInterface.new(path) @last_color = @light.fetch end |
Instance Method Details
#animation ⇒ Object
16 17 18 |
# File 'lib/cheeky-pi/light.rb', line 16 def animation @animation end |
#animation=(new_animation) ⇒ Object
20 21 22 23 |
# File 'lib/cheeky-pi/light.rb', line 20 def animation=(new_animation) stop_animation @animation = new_animation end |
#building ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/cheeky-pi/light.rb', line 29 def building start_color = [5, 5, 25] end_color = [10, 15, MAX] transition!(start_color).then do throb!(3, start_color, end_color) end end |
#color ⇒ Object
89 90 91 |
# File 'lib/cheeky-pi/light.rb', line 89 def color set Color.create(color) end |
#failure ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/cheeky-pi/light.rb', line 38 def failure start_color = [30, 0, 0] end_color = [MAX, 25, 5] transition!(start_color).then do throb!(1, start_color, end_color) end end |
#off ⇒ Object
51 52 53 |
# File 'lib/cheeky-pi/light.rb', line 51 def off transition!(COLOR[:black]) end |
#set(color) ⇒ Object
Set light to a given color
94 95 96 97 |
# File 'lib/cheeky-pi/light.rb', line 94 def set(color) @last_color = color @light.set(color) end |
#stop_animation ⇒ Object
25 26 27 |
# File 'lib/cheeky-pi/light.rb', line 25 def stop_animation self.animation.halt if self.animation end |
#success ⇒ Object
47 48 49 |
# File 'lib/cheeky-pi/light.rb', line 47 def success transition!([0, MAX, 0]) end |
#throb(period, from, to) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/cheeky-pi/light.rb', line 55 def throb (period, from, to) Animation::Throb.new nil, self, { from: Color.create(from), to: Color.create(to), period: period, type: :sine } end |
#throb!(period, from, to) ⇒ Object
64 65 66 67 68 |
# File 'lib/cheeky-pi/light.rb', line 64 def throb! (period, from, to) self.animation = throb(period, from, to) self.animation.promise end |
#transition(to, duration = 2) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cheeky-pi/light.rb', line 71 def transition(to, duration=2) to = Color.create(to) #Set color transition time based on distance, with a maximum of 2 seconds #(number from 0-255) distance = @last_color.distance(to) seconds = duration * [distance, MAX].max / MAX Animation::Fade.new seconds, self, { from: @last_color, to: to } end |
#transition!(to) ⇒ Object
83 84 85 86 87 |
# File 'lib/cheeky-pi/light.rb', line 83 def transition!(to) self.animation = transition to self.animation.promise end |