forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm_pin.go
75 lines (61 loc) · 1.75 KB
/
pwm_pin.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
package edison
import "strconv"
// pwmPath returns pwm base path
func pwmPath() string {
return "/sys/class/pwm/pwmchip0"
}
// pwmExportPath returns export path
func pwmExportPath() string {
return pwmPath() + "/export"
}
// pwmUnExportPath returns unexport path
func pwmUnExportPath() string {
return pwmPath() + "/unexport"
}
// pwmDutyCyclePath returns duty_cycle path for specified pin
func pwmDutyCyclePath(pin string) string {
return pwmPath() + "/pwm" + pin + "/duty_cycle"
}
// pwmPeriodPath returns period path for specified pin
func pwmPeriodPath(pin string) string {
return pwmPath() + "/pwm" + pin + "/period"
}
// pwmEnablePath returns enable path for specified pin
func pwmEnablePath(pin string) string {
return pwmPath() + "/pwm" + pin + "/enable"
}
type pwmPin struct {
pin string
}
// newPwmPin returns an exported and enabled pwmPin
func newPwmPin(pin int) *pwmPin {
return &pwmPin{pin: strconv.Itoa(pin)}
}
// enable writes value to pwm enable path
func (p *pwmPin) enable(val string) (err error) {
_, err = writeFile(pwmEnablePath(p.pin), []byte(val))
return
}
// period reads from pwm period path and returns value
func (p *pwmPin) period() (period string, err error) {
buf, err := readFile(pwmPeriodPath(p.pin))
if err != nil {
return
}
return string(buf[0 : len(buf)-1]), nil
}
// writeDuty writes value to pwm duty cycle path
func (p *pwmPin) writeDuty(duty string) (err error) {
_, err = writeFile(pwmDutyCyclePath(p.pin), []byte(duty))
return
}
// export writes pin to pwm export path
func (p *pwmPin) export() (err error) {
_, err = writeFile(pwmExportPath(), []byte(p.pin))
return
}
// export writes pin to pwm unexport path
func (p *pwmPin) unexport() (err error) {
_, err = writeFile(pwmUnExportPath(), []byte(p.pin))
return
}