Skip to content

Commit abaadb6

Browse files
committed
feat: improve effect parsing error handling
1 parent 8b0b82b commit abaadb6

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

features/light/effect.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package light
22

33
import (
44
"math"
5+
6+
"github.com/pkg/errors"
57
)
68

79
// EffectParams represents keyboard effect parameters.
@@ -14,6 +16,23 @@ type EffectParams struct {
1416
Brightness uint8 `json:"brightness"`
1517
}
1618

19+
func NewEffectParams(color uint8, speed uint8, brightness uint8) (*EffectParams, error) {
20+
if color > 7 {
21+
return nil, errors.Wrap(ErrOutOfRange, "color")
22+
}
23+
if speed > 4 {
24+
return nil, errors.Wrap(ErrOutOfRange, "speed")
25+
}
26+
if brightness > 4 {
27+
return nil, errors.Wrap(ErrOutOfRange, "brightness")
28+
}
29+
return &EffectParams{
30+
Color: color,
31+
Speed: speed,
32+
Brightness: brightness,
33+
}, nil
34+
}
35+
1736
// Effects represents keyboard effects state.
1837
type Effects struct {
1938
Backlight BacklightEffect `json:"backlight"`
@@ -63,7 +82,10 @@ func (b *Effects) Bytes() []byte {
6382
}
6483

6584
// ParseEffects parses raw bytes to effects struct.
66-
func ParseEffects(data []byte) *Effects {
85+
func ParseEffects(data []byte) (*Effects, error) {
86+
if len(data) < 80 {
87+
return nil, ErrOutOfBounds
88+
}
6789
result := &Effects{}
6890
result.Debounce = data[2]
6991
result.Sidelight = MiscEffect{
@@ -86,12 +108,25 @@ func ParseEffects(data []byte) *Effects {
86108
result.Backlight.Params = make([]EffectParams, 29)
87109
for i := range result.Backlight.Params {
88110
offset := colorParamsOffset + (i * 2)
89-
value := data[offset+1]
90-
result.Backlight.Params[i] = EffectParams{
91-
Color: data[offset],
92-
Speed: uint8(math.Floor(float64(value) / 16)),
93-
Brightness: value % 16,
111+
112+
if data[offset] == 255 {
113+
continue
114+
}
115+
p := effectAppearance(data[offset+1])
116+
_, err := NewEffectParams(data[offset], p.Speed(), p.Brightness())
117+
if err != nil {
118+
return nil, err
94119
}
95120
}
96-
return result
121+
return result, nil
122+
}
123+
124+
type effectAppearance uint8
125+
126+
func (e effectAppearance) Brightness() uint8 {
127+
return uint8(math.Floor(float64(uint8(e)) / 16))
128+
}
129+
130+
func (e effectAppearance) Speed() uint8 {
131+
return uint8(e) % 16
97132
}

features/light/feature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (f *Feature) GetEffects() (*Effects, error) {
4949
if err != nil {
5050
return nil, err
5151
}
52-
return ParseEffects(raw), err
52+
return ParseEffects(raw)
5353
}
5454

5555
// SetEffects sets keyboard effects.

features/light/state.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ func (s *StateData) Parse(tpl *layout.Template) (*State, error) {
4141
if err != nil {
4242
return nil, errors.Wrap(err, "colors")
4343
}
44-
effects := ParseEffects(s.Params)
44+
effects, err := ParseEffects(s.Params)
45+
if err != nil {
46+
return nil, errors.Wrap(err, "effects")
47+
}
4548
var customEffect *CustomEffectMap
4649
if tpl != nil {
4750
custom, err := ParseCustomEffect(s.CustomEffect, tpl)

hid/deviceinfo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
"strings"
77

8+
"github.com/mishamyrt/nuga-lib/device"
89
"github.com/sstallion/go-hid"
910
)
1011

@@ -16,7 +17,7 @@ var ErrWrongVendor = errors.New("device vendor is not NuPhy")
1617

1718
// DeviceInfo represents NuPhy keyboard HID information
1819
type DeviceInfo struct {
19-
Model string
20+
Model device.Model
2021
Firmware string
2122
Path string
2223
}
@@ -36,7 +37,7 @@ func GetDeviceInfo(d *hid.Device) (*DeviceInfo, error) {
3637
return nil, err
3738
}
3839
return &DeviceInfo{
39-
Model: model,
40+
Model: device.Model(model),
4041
Path: info.Path,
4142
Firmware: FormatVersion(info.ReleaseNbr),
4243
}, nil

0 commit comments

Comments
 (0)