Module: LIFX::LightTarget

Included in:
Light, LightCollection
Defined in:
lib/lifx/light_target.rb

Overview

LightTarget is a module that contains Light commands that can work with either a single Light or multiple Lights via a LightCollection

Constant Summary collapse

MSEC_PER_SEC =
1000
NSEC_IN_SEC =
1_000_000_000

Instance Method Summary collapse

Instance Method Details

#half_sine(color, cycles: 1, period: 1.0, transient: true, stream: 0) ⇒ Object

Attempts to make the light(s) transition to color smoothly, then immediately back to its original color. Asynchronous.


88
89
90
91
92
93
94
95
96
97
# File 'lib/lifx/light_target.rb', line 88

def half_sine(color, cycles: 1,
                     period: 1.0,
                     transient: true,
                     stream: 0)
  set_waveform(color, waveform: Protocol::Light::Waveform::HALF_SINE,
                      cycles: cycles,
                      stream: stream,
                      transient: transient,
                      period: period)
end

#pulse(color, cycles: 1, duty_cycle: 0.5, transient: true, period: 1.0, stream: 0) ⇒ Object

Attempts to make the light(s) pulse color and then back to its original color. Asynchronous.


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lifx/light_target.rb', line 49

def pulse(color, cycles: 1,
                 duty_cycle: 0.5,
                 transient: true,
                 period: 1.0,
                 stream: 0)
  set_waveform(color, waveform: Protocol::Light::Waveform::PULSE,
                      cycles: cycles,
                      skew_ratio: 1 - duty_cycle,
                      stream: stream,
                      transient: transient,
                      period: period)
end

#refreshLight, LightCollection

Requests light(s) to report their state This method cannot guarantee the message was received.


169
170
171
172
# File 'lib/lifx/light_target.rb', line 169

def refresh
  send_message(Protocol::Light::Get.new)
  self
end

#saw(color, cycles: 1, period: 1.0, transient: true, stream: 0) ⇒ Object

Attempts to make the light(s) transition to color linearly, then instantly back. Asynchronous.


124
125
126
127
128
129
130
131
132
133
# File 'lib/lifx/light_target.rb', line 124

def saw(color, cycles: 1,
               period: 1.0,
               transient: true,
               stream: 0)
  set_waveform(color, waveform: Protocol::Light::Waveform::SAW,
                      cycles: cycles,
                      stream: stream,
                      transient: transient,
                      period: period)
end

#set_color(color, duration: LIFX::Config.default_duration) ⇒ Light, LightCollection

Attempts to set the color of the light(s) to color asynchronously. This method cannot guarantee that the message was received.


12
13
14
15
16
17
18
19
# File 'lib/lifx/light_target.rb', line 12

def set_color(color, duration: LIFX::Config.default_duration)
  send_message(Protocol::Light::Set.new(
    color: color.to_hsbk,
    duration: (duration * MSEC_PER_SEC).to_i,
    stream: 0,
  ))
  self
end

#set_power(state) ⇒ Light, LightCollection

Attempts to set the power state to state asynchronously. This method cannot guarantee the message was received.


139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lifx/light_target.rb', line 139

def set_power(state)
  level = case state
  when :on
    1
  when :off
    0
  else
    raise ArgumentError.new("Must pass in either :on or :off")
  end
  send_message(Protocol::Device::SetPower.new(level: level))
  self
end

#sine(color, cycles: 1, period: 1.0, peak: 0.5, transient: true, stream: 0) ⇒ Object

Attempts to make the light(s) transition to color and back in a smooth sine wave. Asynchronous.


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lifx/light_target.rb', line 69

def sine(color, cycles: 1,
                period: 1.0,
                peak: 0.5,
                transient: true,
                stream: 0)
  set_waveform(color, waveform: Protocol::Light::Waveform::SINE,
                      cycles: cycles,
                      skew_ratio: peak,
                      stream: stream,
                      transient: transient,
                      period: period)
end

#triangle(color, cycles: 1, period: 1.0, peak: 0.5, transient: true, stream: 0) ⇒ Object

Attempts to make the light(s) transition to color linearly and back. Asynchronous.


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lifx/light_target.rb', line 105

def triangle(color, cycles: 1,
                 period: 1.0,
                 peak: 0.5,
                 transient: true,
                 stream: 0)
  set_waveform(color, waveform: Protocol::Light::Waveform::TRIANGLE,
                      cycles: cycles,
                      skew_ratio: peak,
                      stream: stream,
                      transient: transient,
                      period: period)
end

#turn_offLight, LightCollection

Attempts to turn the light(s) off asynchronously. This method cannot guarantee the message was received.


162
163
164
# File 'lib/lifx/light_target.rb', line 162

def turn_off
  set_power(:off)
end

#turn_onLight, LightCollection

Attempts to turn the light(s) on asynchronously. This method cannot guarantee the message was received.


155
156
157
# File 'lib/lifx/light_target.rb', line 155

def turn_on
  set_power(:on)
end