/
effects.go
174 lines (155 loc) · 3.67 KB
/
effects.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package razer
import (
"fmt"
"image/color"
"strings"
"github.com/godbus/dbus"
)
// Effects known by the keyboard hardware itself
const (
EffectNone = iota
EffectStatic
EffectReactive
EffectWave
EffectSpectrum
EffectBreath
EffectBreathDual
EffectBreathRandom
EffectStarlight
EffectStarlightDual
EffectStarlightRandom
EffectRipple
EffectRippleRandom
)
// Effect represents a hardware effect
type Effect struct {
Name string
Type EffectType
primary color.Color
secondary color.Color
}
// EffectType for type safety
type EffectType int
// NewEffect returns a new Effect
func NewEffect(e EffectType, primary, secondary color.Color) Effect {
return Effect{
Name: effectToDBusMethod(e),
Type: e,
primary: primary,
secondary: secondary,
}
}
// StringToEffectType converts a string to an Effect
func StringToEffectType(s string) EffectType {
switch strings.ToLower(s) {
case "static":
return EffectStatic
case "reactive":
return EffectReactive
case "spectrum":
return EffectSpectrum
case "breath":
return EffectBreath
case "breathdual":
return EffectBreathDual
case "breathrandom":
return EffectBreathRandom
case "starlight":
return EffectStarlight
case "starlightdual":
return EffectStarlightDual
case "starlightrandom":
return EffectStarlightRandom
case "ripple":
return EffectRipple
case "ripplerandom":
return EffectRippleRandom
case "wave":
return EffectWave
}
return EffectNone
}
// effectToDBusMethod returns the name of the DBus method matching the Effect
func effectToDBusMethod(e EffectType) string {
switch e {
case EffectStatic:
return "Static"
case EffectReactive:
return "Reactive"
case EffectSpectrum:
return "Spectrum"
case EffectBreath:
return "BreathSingle"
case EffectBreathDual:
return "BreathDual"
case EffectBreathRandom:
return "BreathRandom"
case EffectStarlight:
return "StarlightSingle"
case EffectStarlightDual:
return "StarlightDual"
case EffectStarlightRandom:
return "StarlightRandom"
case EffectRipple:
return "Ripple"
case EffectRippleRandom:
return "RippleRandomColour"
case EffectWave:
return "Wave"
}
return "None"
}
func (e Effect) arguments() []interface{} {
var a []interface{}
switch e.Type {
case EffectStatic:
fallthrough
case EffectBreath:
a = append(a, colorToEffectArg(e.primary)...)
case EffectReactive:
a = append(a, colorToEffectArg(e.primary)...)
a = append(a, 1)
case EffectBreathDual:
a = append(a, colorToEffectArg(e.primary)...)
a = append(a, colorToEffectArg(e.secondary)...)
case EffectStarlight:
a = append(a, 100)
a = append(a, colorToEffectArg(e.primary)...)
case EffectStarlightDual:
a = append(a, 100)
a = append(a, colorToEffectArg(e.primary)...)
a = append(a, colorToEffectArg(e.secondary)...)
case EffectStarlightRandom:
a = append(a, 100)
case EffectRipple:
a = append(a, colorToEffectArg(e.primary)...)
a = append(a, 0.0)
case EffectRippleRandom:
a = append(a, 0.0)
case EffectWave:
a = append(a, 1)
}
return a
}
// SetEffect activates an Effect
func (d *Device) SetEffect(effect Effect) {
var call *dbus.Call
switch effect.Type {
case EffectRipple:
fallthrough
case EffectRippleRandom:
call = d.dbusObject.Call(fmt.Sprintf("razer.device.lighting.custom.set%s", effect.Name), 0, effect.arguments()...)
default:
call = d.dbusObject.Call(fmt.Sprintf("razer.device.lighting.chroma.set%s", effect.Name), 0, effect.arguments()...)
}
dbusCall(call)
}
// colorToEffectArg converts a color to a valid effect argument
func colorToEffectArg(c color.Color) []interface{} {
var a []interface{}
r, g, b, _ := c.RGBA()
a = append(a, uint8(r>>8))
a = append(a, uint8(g>>8))
a = append(a, uint8(b>>8))
return a
}