-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathled.go
257 lines (225 loc) · 5.31 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package sysfs
import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"sync"
"time"
"periph.io/x/conn/v3"
"periph.io/x/conn/v3/driver/driverreg"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/pin"
"periph.io/x/host/v3/fs"
)
// LEDs is all the leds discovered on this host via sysfs.
//
// Depending on the user context, the LEDs may be read-only or writeable.
var LEDs []*LED
// LEDByName returns a *LED for the LED name, if any.
//
// For all practical purpose, a LED is considered an output-only gpio.PinOut.
func LEDByName(name string) (*LED, error) {
// TODO(maruel): Use a bisect or a map. For now we don't expect more than a
// handful of LEDs so it doesn't matter.
for _, led := range LEDs {
if led.name == name {
if err := led.open(); err != nil {
return nil, err
}
return led, nil
}
}
return nil, errors.New("sysfs-led: invalid LED name")
}
// LED represents one LED on the system.
type LED struct {
number int
name string
root string
mu sync.Mutex
fBrightness *fs.File // handle to /sys/class/gpio/gpio*/direction; never closed
}
// String implements conn.Resource.
func (l *LED) String() string {
return fmt.Sprintf("%s(%d)", l.name, l.number)
}
// Halt implements conn.Resource.
//
// It turns the light off.
func (l *LED) Halt() error {
return l.Out(gpio.Low)
}
// Name implements pin.Pin.
func (l *LED) Name() string {
return l.name
}
// Number implements pin.Pin.
func (l *LED) Number() int {
return l.number
}
// Function implements pin.Pin.
func (l *LED) Function() string {
return string(l.Func())
}
// Func implements pin.PinFunc.
func (l *LED) Func() pin.Func {
if l.Read() {
return "LED/On"
}
return "LED/Off"
}
// SupportedFuncs implements pin.PinFunc.
func (l *LED) SupportedFuncs() []pin.Func {
return []pin.Func{"LED"}
}
// SetFunc implements pin.PinFunc.
func (l *LED) SetFunc(f pin.Func) error {
return errors.New("sysfs-led: not implemented")
}
// In implements gpio.PinIn.
func (l *LED) In(pull gpio.Pull, edge gpio.Edge) error {
if pull != gpio.Float && pull != gpio.PullNoChange {
return errors.New("sysfs-led: pull is not supported on LED")
}
if edge != gpio.NoEdge {
return errors.New("sysfs-led: edge is not supported on LED")
}
return nil
}
// Read implements gpio.PinIn.
func (l *LED) Read() gpio.Level {
err := l.open()
if err != nil {
return gpio.Low
}
l.mu.Lock()
defer l.mu.Unlock()
if _, err := l.fBrightness.Seek(0, 0); err != nil {
return gpio.Low
}
var b [4]byte
if _, err := l.fBrightness.Read(b[:]); err != nil {
return gpio.Low
}
if b[0] != '0' {
return gpio.High
}
return gpio.Low
}
// WaitForEdge implements gpio.PinIn.
func (l *LED) WaitForEdge(timeout time.Duration) bool {
return false
}
// Pull implements gpio.PinIn.
func (l *LED) Pull() gpio.Pull {
return gpio.PullNoChange
}
// DefaultPull implements gpio.PinIn.
func (l *LED) DefaultPull() gpio.Pull {
return gpio.PullNoChange
}
// Out implements gpio.PinOut.
func (l *LED) Out(level gpio.Level) error {
err := l.open()
if err != nil {
return err
}
l.mu.Lock()
defer l.mu.Unlock()
if _, err = l.fBrightness.Seek(0, 0); err != nil {
return err
}
if level {
_, err = l.fBrightness.Write([]byte("255"))
} else {
_, err = l.fBrightness.Write([]byte("0"))
}
return err
}
// PWM implements gpio.PinOut.
//
// This sets the intensity level, if supported. The frequency is ignored.
func (l *LED) PWM(d gpio.Duty, f physic.Frequency) error {
err := l.open()
if err != nil {
return err
}
l.mu.Lock()
defer l.mu.Unlock()
if _, err = l.fBrightness.Seek(0, 0); err != nil {
return err
}
v := (d + gpio.DutyMax/512) / (gpio.DutyMax / 256)
_, err = l.fBrightness.Write([]byte(strconv.Itoa(int(v))))
return err
}
//
func (l *LED) open() error {
l.mu.Lock()
defer l.mu.Unlock()
// trigger, max_brightness.
var err error
if l.fBrightness == nil {
p := l.root + "brightness"
if l.fBrightness, err = fs.Open(p, os.O_RDWR); err != nil {
// Retry with read-only. This is the default setting.
l.fBrightness, err = fs.Open(p, os.O_RDONLY)
}
}
return err
}
// driverLED implements periph.Driver.
type driverLED struct {
}
func (d *driverLED) String() string {
return "sysfs-led"
}
func (d *driverLED) Prerequisites() []string {
return nil
}
func (d *driverLED) After() []string {
return nil
}
// Init initializes LEDs sysfs handling code.
//
// Uses led sysfs as described* at
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-led
//
// * for the most minimalistic meaning of 'described'.
func (d *driverLED) Init() (bool, error) {
items, err := filepath.Glob("/sys/class/leds/*")
if err != nil {
return true, err
}
if len(items) == 0 {
return false, errors.New("no LED found")
}
// This make the LEDs in deterministic order.
sort.Strings(items)
for i, item := range items {
LEDs = append(LEDs, &LED{
number: i,
name: filepath.Base(item),
root: item + "/",
})
}
return true, nil
}
func init() {
if isLinux {
driverreg.MustRegister(&drvLED)
}
}
var drvLED driverLED
var _ conn.Resource = &LED{}
var _ gpio.PinIn = &LED{}
var _ gpio.PinOut = &LED{}
var _ gpio.PinIO = &LED{}
var _ pin.PinFunc = &LED{}