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



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

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)

    Percentage 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,
                      duty_cycle: duty_cycle,
                      stream: stream,
                      transient: transient,
                      period: period)
end

#reboot!Light, LightCollection

Attempts to reboots the light(s). This method cannot guarantee the message was received.

Returns:



185
186
187
# File 'lib/lifx/light_target.rb', line 185

def reboot!
  send_message(Protocol::Device::Reboot.new)
end

#refreshLight, LightCollection

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

Returns:



157
158
159
160
# File 'lib/lifx/light_target.rb', line 157

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/lifx/light_target.rb', line 120

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(value) ⇒ Light, LightCollection

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

Parameters:

  • value (0, 1)

    0 for off, 1 for on

Returns:



135
136
137
138
# File 'lib/lifx/light_target.rb', line 135

def set_power(value)
  send_message(Protocol::Device::SetPower.new(level: value))
  self
end

#set_site_id(site_id) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Don't use this unless you know what you're doing.

This method returns an undefined value.

Attempts to set the site id of the light. Will clear label and tags. This method cannot guarantee message receipt.

Parameters:

  • site_id (String)

    Site ID



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

def set_site_id(site_id)
  send_message(Protocol::Device::SetSite.new(site: [site_id].pack('H*')))
end

#set_time(time = Time.now) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Attempts to set the device time on the targets

Parameters:

  • time (Time) (defaults to: Time.now)

    The time to set



177
178
179
# File 'lib/lifx/light_target.rb', line 177

def set_time(time = Time.now)
  send_message(Protocol::Device::SetTime.new(time: (time.to_f * NSEC_IN_SEC).round))
end

#set_waveform(color, waveform:, cycles:, stream: 0, transient: true, period: 1.0, duty_cycle: 0.5, acknowledge: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Don't use this directly.

Attempts to apply a waveform to the light(s) asynchronously.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lifx/light_target.rb', line 24

def set_waveform(color, waveform:,
                        cycles:,
                        stream: 0,
                        transient: true,
                        period: 1.0,
                        duty_cycle: 0.5,
                        acknowledge: false)
  send_message(Protocol::Light::SetWaveform.new(
    color: color.to_hsbk,
    waveform: waveform,
    cycles: cycles,
    stream: stream,
    transient: transient,
    period: (period * 1_000).to_i,
    duty_cycle: (duty_cycle * 65535).round - 32768
  ), acknowledge: acknowledge)
end

#sine(color, cycles: 1, period: 1.0, 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

  • 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



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

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

#triangle(color, cycles: 1, period: 1.0, 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



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

def triangle(color, cycles: 1,
                 period: 1.0,
                 transient: true,
                 stream: 0)
  set_waveform(color, waveform: Protocol::Light::Waveform::TRIANGLE,
                      cycles: cycles,
                      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:



150
151
152
# File 'lib/lifx/light_target.rb', line 150

def turn_off
  set_power(0)
end

#turn_onLight, LightCollection

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

Returns:



143
144
145
# File 'lib/lifx/light_target.rb', line 143

def turn_on
  set_power(1)
end