-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
led.go
158 lines (139 loc) · 4.01 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
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
// Copyright ©2016 The ev3go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ev3dev
import (
"errors"
"fmt"
"path/filepath"
"strconv"
"time"
)
// LED represents a handle to an ev3 LED.
//
// Interaction with shared physical resources is intrinsically
// subject to race conditions without a transactional model,
// which is not provided here. If concurrent access to LEDs is
// needed, the user is required to establish this model.
type LED struct {
Name fmt.Stringer
err error
}
// ledDevice is used to fake a Device. The Type method do not
// have meaningful semantics.
type ledDevice struct {
*LED
}
// Path returns the LED sysfs path.
func (l *LED) Path() string { return filepath.Join(prefix, LEDPath) }
func (ledDevice) Type() string { panic("ev3dev: unexpected call of ledDevice Type") }
// String satisfies the fmt.Stringer interface.
func (l *LED) String() string { return l.Name.String() }
// Err returns the error state of the LED and clears it.
func (l *LED) Err() error {
err := l.err
l.err = nil
return err
}
// MaxBrightness returns the maximum brightness value for the LED.
func (l *LED) MaxBrightness() (int, error) {
return intFrom(attributeOf(ledDevice{l}, maxBrightness))
}
// Brightness returns the current brightness value for the LED.
func (l *LED) Brightness() (int, error) {
return intFrom(attributeOf(ledDevice{l}, brightness))
}
// SetBrightness sets the brightness of the LED.
func (l *LED) SetBrightness(bright int) *LED {
if l.err != nil {
return l
}
max, err := l.MaxBrightness()
if err != nil {
l.err = err
return l
}
if bright < 0 || bright > max {
l.err = newValueOutOfRangeError(ledDevice{l}, brightness, bright, 0, max)
return l
}
l.err = setAttributeOf(ledDevice{l}, brightness, strconv.Itoa(bright))
return l
}
// Trigger returns the current and available triggers for the LED.
func (l *LED) Trigger() (current string, available []string, err error) {
all, err := stringSliceFrom(attributeOf(ledDevice{l}, trigger))
if err != nil {
return "", nil, err
}
for i, t := range all {
if t[0] == '[' && t[len(t)-1] == ']' {
all[i] = t[1 : len(t)-1]
current = all[i]
}
}
if current == "" {
return "", available, errors.New("ev3dev: could not find current trigger")
}
return current, all, err
}
// SetTrigger sets the trigger for the LED.
func (l *LED) SetTrigger(trig string) *LED {
if l.err != nil {
return l
}
_, avail, err := l.Trigger()
if err != nil {
l.err = err
return l
}
ok := false
for _, t := range avail {
if t == trig {
ok = true
break
}
}
if !ok {
l.err = newInvalidValueError(ledDevice{l}, trigger, "", trig, avail)
return l
}
l.err = setAttributeOf(ledDevice{l}, trigger, trig)
return l
}
// DelayOff returns the duration for which the LED is off when using the timer trigger.
func (l *LED) DelayOff() (time.Duration, error) {
return durationFrom(attributeOf(ledDevice{l}, delayOff))
}
// SetDelayOff sets the duration for which the LED is off when using the timer trigger.
func (l *LED) SetDelayOff(d time.Duration) *LED {
if l.err != nil {
return l
}
if d < 0 {
l.err = newNegativeDurationError(ledDevice{l}, delayOff, d)
return l
}
l.err = setAttributeOf(ledDevice{l}, delayOff, strconv.Itoa(int(d/time.Millisecond)))
return l
}
// DelayOn returns the duration for which the LED is on when using the timer trigger.
func (l *LED) DelayOn() (time.Duration, error) {
return durationFrom(attributeOf(ledDevice{l}, delayOn))
}
// SetDelayOn sets the duration for which the LED is on when using the timer trigger.
func (l *LED) SetDelayOn(d time.Duration) *LED {
if l.err != nil {
return l
}
if d < 0 {
l.err = newNegativeDurationError(ledDevice{l}, delayOn, d)
return l
}
l.err = setAttributeOf(ledDevice{l}, delayOn, strconv.Itoa(int(d/time.Millisecond)))
return l
}
// Uevent returns the current uevent state for the LED.
func (l *LED) Uevent() (map[string]string, error) {
return ueventFrom(attributeOf(ledDevice{l}, uevent))
}