forked from kidoman/embd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
led.go
94 lines (72 loc) · 1.62 KB
/
led.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
// LED support.
package embd
// The LED interface is used to control a led on the prototyping board.
type LED interface {
// On switches the LED on.
On() error
// Off switches the LED off.
Off() error
// Toggle toggles the LED.
Toggle() error
// Close releases resources associated with the LED.
Close() error
}
// LEDDriver interface interacts with the host descriptors to allow us
// control of the LEDs.
type LEDDriver interface {
LED(key interface{}) (LED, error)
Close() error
}
var ledDriverInitialized bool
var ledDriverInstance LEDDriver
// InitLED initializes the LED driver.
func InitLED() error {
if ledDriverInitialized {
return nil
}
desc, err := DescribeHost()
if err != nil {
return err
}
if desc.LEDDriver == nil {
return ErrFeatureNotSupported
}
ledDriverInstance = desc.LEDDriver()
ledDriverInitialized = true
return nil
}
// CloseLED releases resources associated with the LED driver.
func CloseLED() error {
return ledDriverInstance.Close()
}
// NewLED returns a LED interface which allows control over the LED.
func NewLED(key interface{}) (LED, error) {
if err := InitLED(); err != nil {
return nil, err
}
return ledDriverInstance.LED(key)
}
// LEDOn switches the LED on.
func LEDOn(key interface{}) error {
led, err := NewLED(key)
if err != nil {
return err
}
return led.On()
}
// LEDOff switches the LED off.
func LEDOff(key interface{}) error {
led, err := NewLED(key)
if err != nil {
return err
}
return led.Off()
}
// LEDToggle toggles the LED.
func LEDToggle(key interface{}) error {
led, err := NewLED(key)
if err != nil {
return err
}
return led.Toggle()
}