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.

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


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.

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


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.

Returns:


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.

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


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.

Parameters:

  • color (Color)

    The color to be set

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

    Transition time in seconds

Returns:


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.

Parameters:

  • state (:on, :off)

Returns:


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.

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


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.

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


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.

Returns:


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.

Returns:


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

def turn_on
  set_power(:on)
end