Module: LIFX::LAN::LightTarget

Included in:
Light, LightCollection
Defined in:
lib/lifx/lan/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.

Parameters:

  • color (Color)

    Color

  • cycles: (Integer) (defaults to: 1)

    Number of cycles

  • transient: (Boolean) (defaults to: true)

    If false, the light will remain at the color the waveform is at when it ends

  • period: (Integer) (defaults to: 1.0)

    Number of seconds a cycle. Must be above 1.0 (?)

  • stream: (Integer) (defaults to: 0)

    Unused



143
144
145
146
147
148
149
150
151
152
# File 'lib/lifx/lan/light_target.rb', line 143

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.

Parameters:

  • color (Color)

    Color to pulse

  • duty_cycle: (Float) (defaults to: 0.5)

    Ratio of a cycle the light(s) is set to color

  • cycles: (Integer) (defaults to: 1)

    Number of cycles

  • transient: (Boolean) (defaults to: true)

    If false, the light will remain at the color the waveform is at when it ends

  • period: (Integer) (defaults to: 1.0)

    Number of seconds a cycle. Must be above 1.0 (?)

  • stream: (Integer) (defaults to: 0)

    Unused



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

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.

Returns:



224
225
226
227
# File 'lib/lifx/lan/light_target.rb', line 224

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.

Parameters:

  • color (Color)

    Color to saw wave

  • cycles: (Integer) (defaults to: 1)

    Number of cycles

  • transient: (Boolean) (defaults to: true)

    If false, the light will remain at the color the waveform is at when it ends

  • period: (Integer) (defaults to: 1.0)

    Number of seconds a cycle. Must be above 1.0 (?)

  • stream: (Integer) (defaults to: 0)

    Unused



179
180
181
182
183
184
185
186
187
188
# File 'lib/lifx/lan/light_target.rb', line 179

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::LAN::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.

Parameters:

  • color (Color)

    The color to be set

  • duration: (Numeric) (defaults to: LIFX::LAN::Config.default_duration)

    Transition time in seconds

Returns:



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

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

#set_multizone_color(color, start_index: 0, end_index: 7, duration: LIFX::LAN::Config.default_duration) ⇒ Light, LightCollection

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

Parameters:

  • color (Color)

    The color to be set

  • start_index (Numeric) (defaults to: 0)

    The index of the first zone to be set

  • end_index (Numeric) (defaults to: 7)

    The index of the last zone to be set

  • duration: (Numeric) (defaults to: LIFX::LAN::Config.default_duration)

    Transition time in seconds

Returns:



29
30
31
32
33
34
35
36
37
38
# File 'lib/lifx/lan/light_target.rb', line 29

def set_multizone_color(color, start_index: 0, end_index: 7, duration: LIFX::LAN::Config.default_duration)
  send_message(Protocol::MultiZone::SetColorZones.new(
    start_index: start_index,
    end_index: end_index,
    color: color.to_hsbk,
    duration: (duration * MSEC_PER_SEC).to_i,
    apply: 1,
  ))
  self
end

#set_power(state) ⇒ Light, LightCollection

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

Parameters:

  • state (:on, :off)

Returns:



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/lifx/lan/light_target.rb', line 194

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

#set_tile_colors(colors, tile_start_index: 0, length: 1, duration: LIFX::LAN::Config.default_duration) ⇒ Light, LightCollection

Attempts to set every pixel on the tile(s) to colors asynchronously. This method cannot guarantee that the message was received.

Parameters:

  • colors (Array<Color>)

    The 64 Colors to be set as a 1-d array

  • tile_start_index (Numeric) (defaults to: 0)

    The index of the first tile to be set

  • length (Numeric) (defaults to: 1)

    The number of tiles to set starting from the index

  • duration: (Numeric) (defaults to: LIFX::LAN::Config.default_duration)

    Transition time in seconds

Returns:



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

def set_tile_colors(colors, tile_start_index: 0, length: 1, duration: LIFX::LAN::Config.default_duration)
  send_message(Protocol::Tile::SetTileState64.new(
    colors: colors.map(&:to_hsbk),
    tile_start_index: tile_start_index,
    tile_length: [length, 1].min,
    x: 0,
    y: 0,
    width: 8,
    duration: (duration * MSEC_PER_SEC).to_i
  ))
  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.

Parameters:

  • color (Color)

    Color

  • cycles: (Integer) (defaults to: 1)

    Number of cycles

  • peak: (Float) (defaults to: 0.5)

    Defines the peak point of the wave. Defaults to 0.5 which is a standard sine

  • transient: (Boolean) (defaults to: true)

    If false, the light will remain at the color the waveform is at when it ends

  • period: (Integer) (defaults to: 1.0)

    Number of seconds a cycle. Must be above 1.0 (?)

  • stream: (Integer) (defaults to: 0)

    Unused



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

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

#tile_infoHash

Returns Raw tile info.

Returns:

  • (Hash)

    Raw tile info



69
70
71
72
73
74
# File 'lib/lifx/lan/light_target.rb', line 69

def tile_info
  send_message!(Protocol::Tile::GetDeviceChain.new,
      wait_for: Protocol::Tile::StateDeviceChain) do |payload|
    payload
  end
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.

Parameters:

  • color (Color)

    Color to pulse

  • cycles: (Integer) (defaults to: 1)

    Number of cycles

  • transient: (Boolean) (defaults to: true)

    If false, the light will remain at the color the waveform is at when it ends

  • period: (Integer) (defaults to: 1.0)

    Number of seconds a cycle. Must be above 1.0 (?)

  • stream: (Integer) (defaults to: 0)

    Unused



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/lifx/lan/light_target.rb', line 160

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.

Returns:



217
218
219
# File 'lib/lifx/lan/light_target.rb', line 217

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.

Returns:



210
211
212
# File 'lib/lifx/lan/light_target.rb', line 210

def turn_on
  set_power(:on)
end

#zone_countNumeric

Returns Number of zones supported.

Returns:

  • (Numeric)

    Number of zones supported



61
62
63
64
65
66
# File 'lib/lifx/lan/light_target.rb', line 61

def zone_count
  send_message!(Protocol::MultiZone::GetColorZones.new(start_index: 0, end_index: 0),
      wait_for: Protocol::MultiZone::StateZone) do |payload|
    payload.total_zones
  end
end