lifxlan: github.com/fishy/lifxlan/light Index | Examples | Files

package light

import "github.com/fishy/lifxlan/light"

Package light implements LIFX LAN Protocol for LIFX light devices:

https://lan.developer.lifx.com/v2.0/docs/light-messages

Please refer to its parent package for more background/context.

This example demonstrates how to set color on a light device.

Code:

// Need proper initialization on real code.
var (
    device light.Device
    // Important to set timeout to context when requiring ack.
    timeout time.Duration
    // Color to set.
    color lifxlan.Color
)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := device.SetColor(
    ctx,
    nil, // conn, use nil so that SetColors will maintain it for us
    &color,
    0,    // fade in duration
    true, // ack
); err != nil {
    log.Fatal(err)
}

Index

Examples

Package Files

color.go device.go doc.go messages.go waveform.go wrap.go

Constants

const (
    Get                 lifxlan.MessageType = 101
    SetColor            lifxlan.MessageType = 102
    State               lifxlan.MessageType = 107
    SetWaveformOptional lifxlan.MessageType = 119
)

Light related MessageType values.

func ConvertSkewRatio Uses

func ConvertSkewRatio(v float64) int16

ConvertSkewRatio scales [0, 1] into [-32768, 32767].

type BoolUint8 Uses

type BoolUint8 uint8

BoolUint8 is the uint8 value used to represent bool.

func Bool2Uint8 Uses

func Bool2Uint8(b bool) BoolUint8

Bool2Uint8 converts bool value into BoolUint8 value.

type Device Uses

type Device interface {
    lifxlan.Device

    // GetColor returns the current color on this light device.
    //
    // If conn is nil,
    // a new connection will be made and guaranteed to be closed before returning.
    // You should pre-dial and pass in the conn if you plan to call APIs on this
    // device repeatedly.
    GetColor(ctx context.Context, conn net.Conn) (*lifxlan.Color, error)

    // SetColor sets the light device with the given color.
    //
    // If conn is nil,
    // a new connection will be made and guaranteed to be closed before returning.
    // You should pre-dial and pass in the conn if you plan to call APIs on this
    // device repeatedly.
    //
    // If ack is false,
    // this function returns nil error after the API is sent successfully.
    // If ack is true,
    // this function will only return nil error after it received ack from the
    // device.
    SetColor(ctx context.Context, conn net.Conn, color *lifxlan.Color, transition time.Duration, ack bool) error

    // SetWaveform sends SetWaveformOptional message as defined in
    //
    // https://lan.developer.lifx.com/docs/light-messages#section-setwaveformoptional-119
    SetWaveform(ctx context.Context, conn net.Conn, args *SetWaveformArgs, ack bool) error
}

Device is a wrapped lifxlan.Device that provides light related APIs.

func Wrap Uses

func Wrap(ctx context.Context, d lifxlan.Device, force bool) (Device, error)

Wrap tries to wrap a lifxlan.Device into a light device.

When force is false and d is already a light device, d will be casted and returned directly. Otherwise, this function might call a device API to determine whether it's a light device.

If the device is not a light device, the function might block until ctx is cancelled.

Currently all lifx devices are light device, so this function doesn't really call an API and just do a naive wrapping. But that might change in the future so you shouldn't be relying on that.

type RawSetColorPayload Uses

type RawSetColorPayload struct {
    Color    lifxlan.Color
    Duration lifxlan.TransitionTime
    // contains filtered or unexported fields
}

RawSetColorPayload defines the struct to be used for encoding and decoding.

https://lan.developer.lifx.com/v2.0/docs/light-messages#section-setcolor-102

type RawSetWaveformOptionalPayload Uses

type RawSetWaveformOptionalPayload struct {
    Transient     BoolUint8
    Color         lifxlan.Color
    Period        lifxlan.TransitionTime
    Cycles        float32
    SkewRatio     int16
    Waveform      Waveform
    SetHue        BoolUint8
    SetSaturation BoolUint8
    SetBrightness BoolUint8
    SetKelvin     BoolUint8
    // contains filtered or unexported fields
}

RawSetWaveformOptionalPayload defines the struct to be used for encoding and decoding.

https://lan.developer.lifx.com/v2.0/docs/light-messages#section-setwaveformoptional-119

type RawStatePayload Uses

type RawStatePayload struct {
    Color lifxlan.Color

    Power lifxlan.Power
    Label lifxlan.Label
    // contains filtered or unexported fields
}

RawStatePayload defines the struct to be used for encoding and decoding.

https://lan.developer.lifx.com/v2.0/docs/light-messages#section-state-107

type SetWaveformArgs Uses

type SetWaveformArgs struct {
    // True means that after the waveform it should go back to its original color.
    Transient bool

    // The target color.
    Color *lifxlan.Color

    // Duration of a cycle.
    Period time.Duration

    // Number of cycles.
    Cycles float32

    // Type of waveform.
    Waveform Waveform

    // SkewRatio should be in range [0, 1] and it is only used with WaveformPulse.
    //
    // https://lan.developer.lifx.com/v2.0/docs/waveforms#section-pulse
    SkewRatio float64

    // The color args with Keep* set to true will not be changed.
    //
    // For example the current color has H 255, S 255, B 255 and K 255,
    // and the target color has H 0, S 0, B 0, K 0.
    // When KeepHue is false and all else are true,
    // Only H will be changed during the waveform,
    // S, B, and K won't be changed.
    //
    // Please note this is the reverse of the set_* definitions of
    // RawSetWaveformOptionalPayload.
    // The reason is to make sure that when they are all zero values,
    // it behaves the same as SetWaveform message as defined in:
    // https://lan.developer.lifx.com/v2.0/docs/light-messages#section-setwaveform-103
    KeepHue        bool
    KeepSaturation bool
    KeepBrightness bool
    KeepKelvin     bool
}

SetWaveformArgs is the args to be translated into RawSetWaveformOptionalPayload.

type Waveform Uses

type Waveform uint8

Waveform defines the type of the waveform.

https://lan.developer.lifx.com/v2.0/docs/waveforms

const (
    // https://lan.developer.lifx.com/v2.0/docs/waveforms#section-saw
    WaveformSaw Waveform = 0

    // https://lan.developer.lifx.com/v2.0/docs/waveforms#section-sine
    WaveformSine Waveform = 1

    // https://lan.developer.lifx.com/v2.0/docs/waveforms#section-half-sine
    WaveformHalfSine Waveform = 2

    // https://lan.developer.lifx.com/v2.0/docs/waveforms#section-triangle
    WaveformTriangle Waveform = 3

    // https://lan.developer.lifx.com/v2.0/docs/waveforms#section-pulse
    WaveformPulse Waveform = 4
)

Waveform values.

https://lan.developer.lifx.com/v2.0/docs/waveforms

Package light imports 8 packages (graph) and is imported by 2 packages. Updated 2020-04-21. Refresh now. Tools for package owners.