light

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

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.

Example

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

package main

import (
	"context"
	"log"
	"time"

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

func main() {
	// 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)
	}
}
Output:

Index

Examples

Constants

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

Light related MessageType values.

Variables

This section is empty.

Functions

func ConvertSkewRatio

func ConvertSkewRatio(v float64) int16

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

Types

type BoolUint8

type BoolUint8 uint8

BoolUint8 is the uint8 value used to represent bool.

func Bool2Uint8

func Bool2Uint8(b bool) BoolUint8

Bool2Uint8 converts bool value into BoolUint8 value.

type Device

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

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 calls a light device API, and only returns a non-nil Device if it supports the API.

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

When returning a valid light device, the device's Label is guaranteed to be cached.

type RawSetColorPayload

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

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

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL