-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.go
More file actions
114 lines (89 loc) · 2.23 KB
/
types.go
File metadata and controls
114 lines (89 loc) · 2.23 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package lightpack
import (
"errors"
"strconv"
"strings"
)
type Status string
const (
StatusOn Status = "on"
StatusOff Status = "off"
StatusUnknown Status = "unknown"
StatusDeviceError Status = "device_error"
)
func ParseStatus(response string) (Status, error) {
if strings.HasPrefix(response, "status:") {
response = response[7:]
}
ret := Status(response)
if ret != StatusOn && ret != StatusOff && ret != StatusDeviceError && ret != StatusUnknown {
return StatusUnknown, errors.New("invalid status")
}
return ret, nil
}
type StatusAPI string
const (
StatusAPIBusy StatusAPI = "busy"
StatusAPIIdle StatusAPI = "idle"
)
func ParseStatusAPI(response string) (StatusAPI, error) {
if strings.HasPrefix(response, "statusapi:") {
response = response[10:]
}
ret := StatusAPI(response)
if ret != StatusAPIIdle && ret != StatusAPIBusy {
return StatusAPIBusy, errors.New("invalid status")
}
return ret, nil
}
// I really hate spelling it wrong.
type Color struct {
R, G, B uint8
}
func ParseColors(response string) ([]Color, error) {
if strings.HasPrefix(response, "colors:") {
response = response[7:]
}
ledColors := strings.Split(response, ";")
ret := make([]Color, len(ledColors))
for _, v := range ledColors {
// Fixes trailing ; from making this fail.
if v == "" {
continue
}
led, color, err := ParseColor(v)
if err != nil {
return ret, err
}
ret[led] = color
}
return ret, nil
}
func ParseColor(color string) (int, Color, error) {
colorParts := strings.SplitN(color, "-", 2)
ledNumber, err := strconv.Atoi(colorParts[0])
if err != nil {
return ledNumber, Color{}, err
}
RGBVals := strings.Split(colorParts[1], ",")
if len(RGBVals) != 3 {
return ledNumber, Color{}, errors.New("invalid RGB values provided")
}
retColor := Color{}
if r, err := strconv.ParseInt(RGBVals[0], 10, 16); err != nil {
return ledNumber, Color{}, err
} else {
retColor.R = uint8(r)
}
if g, err := strconv.ParseInt(RGBVals[1], 10, 16); err != nil {
return ledNumber, Color{}, err
} else {
retColor.G = uint8(g)
}
if b, err := strconv.ParseInt(RGBVals[2], 10, 16); err != nil {
return ledNumber, Color{}, err
} else {
retColor.B = uint8(b)
}
return ledNumber, retColor, nil
}