/
device.go
94 lines (79 loc) · 2.42 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package tile
import (
"context"
"fmt"
"net"
"time"
"go.yhsif.com/lifxlan"
"go.yhsif.com/lifxlan/light"
)
// Device is a wrapped lifxlan.Device that provides tile related APIs.
type Device interface {
light.Device
Board
// Tiles returns a copy of the tiles in this device.
Tiles() []Tile
// GetColors returns the current color board on this tile 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.
//
// This function will wait for len(Tiles()) response messages.
// In case of one or more of the responses get dropped on the network,
// this function will wait until context is cancelled.
// So it's important to set an appropriate timeout on the context.
GetColors(ctx context.Context, conn net.Conn) (ColorBoard, error)
// SetColors sets the tile device with the given color board.
//
// 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 all ack(s) from
// the device.
SetColors(ctx context.Context, conn net.Conn, cb ColorBoard, transition time.Duration, ack bool) error
// TileWidth returns the width of the i-th tile.
//
// If i is out of bound, it returns the width of the first tile (index 0)
// instead. If there's no known tiles, it returns 0.
TileWidth(i int) uint8
}
type device struct {
light.Device
startIndex uint8
tiles []*Tile
// parsed board data
board BoardData
}
var _ Device = (*device)(nil)
func (td *device) String() string {
if label := td.Label().String(); label != lifxlan.EmptyLabel {
return fmt.Sprintf("%s(%v)", label, td.Target())
}
if parsed := td.HardwareVersion().Parse(); parsed != nil {
return fmt.Sprintf("%s(%v)", parsed.ProductName, td.Target())
}
return fmt.Sprintf("TileDevice(%v)", td.Target())
}
func (td *device) Tiles() []Tile {
tiles := make([]Tile, len(td.tiles))
for i := range tiles {
tiles[i] = *td.tiles[i]
}
return tiles
}
func (td *device) TileWidth(i int) uint8 {
if len(td.tiles) == 0 {
return 0
}
if i < 0 || i >= len(td.tiles) {
i = 0
}
return td.tiles[i].Width
}