-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathrgb_led_driver.go
139 lines (113 loc) · 3.11 KB
/
rgb_led_driver.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
package gpio
import (
"gobot.io/x/gobot/v2"
)
// RgbLedDriver represents a digital RGB Led
type RgbLedDriver struct {
*driver
pinRed string
redColor byte
pinGreen string
greenColor byte
pinBlue string
blueColor byte
high bool
}
// NewRgbLedDriver return a new RgbLedDriver given a PwmWriter and 3 pins: redPin, greenPin, and bluePin
//
// Supported options:
//
// "WithName"
//
// Adds the following API Commands:
//
// "SetRGB" - See RgbLedDriver.SetRGB
// "Toggle" - See RgbLedDriver.Toggle
// "On" - See RgbLedDriver.On
// "Off" - See RgbLedDriver.Off
func NewRgbLedDriver(a PwmWriter, redPin string, greenPin string, bluePin string, opts ...interface{}) *RgbLedDriver {
//nolint:forcetypeassert // no error return value, so there is no better way
d := &RgbLedDriver{
driver: newDriver(a.(gobot.Connection), "RGBLED", opts...),
pinRed: redPin,
pinGreen: greenPin,
pinBlue: bluePin,
}
//nolint:forcetypeassert // ok here
d.AddCommand("SetRGB", func(params map[string]interface{}) interface{} {
r := byte(params["r"].(int))
g := byte(params["g"].(int))
b := byte(params["b"].(int))
return d.SetRGB(r, g, b)
})
d.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
return d.Toggle()
})
d.AddCommand("On", func(params map[string]interface{}) interface{} {
return d.On()
})
d.AddCommand("Off", func(params map[string]interface{}) interface{} {
return d.Off()
})
return d
}
// Pin returns the RgbLedDrivers pins
func (d *RgbLedDriver) Pin() string {
return "r=" + d.pinRed + ", g=" + d.pinGreen + ", b=" + d.pinBlue
}
// RedPin returns the RgbLedDrivers redPin
func (d *RgbLedDriver) RedPin() string { return d.pinRed }
// GreenPin returns the RgbLedDrivers redPin
func (d *RgbLedDriver) GreenPin() string { return d.pinGreen }
// BluePin returns the RgbLedDrivers bluePin
func (d *RgbLedDriver) BluePin() string { return d.pinBlue }
// State return true if the led is On and false if the led is Off
func (d *RgbLedDriver) State() bool {
return d.high
}
// On sets the led's pins to their various states
func (d *RgbLedDriver) On() error {
if err := d.SetLevel(d.pinRed, d.redColor); err != nil {
return err
}
if err := d.SetLevel(d.pinGreen, d.greenColor); err != nil {
return err
}
if err := d.SetLevel(d.pinBlue, d.blueColor); err != nil {
return err
}
d.high = true
return nil
}
// Off sets the led to black.
func (d *RgbLedDriver) Off() error {
if err := d.SetLevel(d.pinRed, 0); err != nil {
return err
}
if err := d.SetLevel(d.pinGreen, 0); err != nil {
return err
}
if err := d.SetLevel(d.pinBlue, 0); err != nil {
return err
}
d.high = false
return nil
}
// Toggle sets the led to the opposite of it's current state
func (d *RgbLedDriver) Toggle() error {
if d.State() {
return d.Off()
}
return d.On()
}
// SetLevel sets the led to the specified color level
func (d *RgbLedDriver) SetLevel(pin string, level byte) error {
return d.pwmWrite(pin, level)
}
// SetRGB sets the Red Green Blue value of the LED.
func (d *RgbLedDriver) SetRGB(r, g, b byte) error {
d.redColor = r
d.greenColor = g
d.blueColor = b
return d.On()
}