-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwm.go
148 lines (134 loc) · 3.43 KB
/
pwm.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
package gogadgets
import (
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"path"
"path/filepath"
)
const (
NANO = 1000000000.0
)
var (
PWMMode = os.ModeDevice
PWM_DEVPATH = "/sys/devices/ocp.*/pwm_test_P%s_%s.*"
TREEPATH = "/sys/devices/bone_capemgr.*/slots"
)
// echo am33xx_pwm > /sys/devices/bone_capemgr.9/slots
// echo bone_pwm_P8_13 > /sys/devices/bone_capemgr.9/slots
// /sys/devices/ocp.3/pwm_test_P8_13.15
type PWM struct {
period int
duty []byte
status bool
runPath string
dutyPath string
periodPath string
}
func NewPWM(pin *Pin) (OutputDevice, error) {
// err := writePWMDeviceTree(pin.Port, pin.Pin)
// if err != nil {
// return nil, err
// }
devPath, period, err := setupPWM(pin)
pwm := &PWM{
period: period,
duty: []byte(fmt.Sprintf("%d", period)),
runPath: path.Join(devPath, "run"),
dutyPath: path.Join(devPath, "duty"),
periodPath: path.Join(devPath, "period"),
}
return pwm, err
}
func (p *PWM) Commands(location, name string) *Commands {
return nil
}
func (p *PWM) Update(msg *Message) bool {
return false
}
func (p *PWM) On(val *Value) error {
if val != nil && val.Units == "%" {
ioutil.WriteFile(p.runPath, []byte("0"), PWMMode)
p.duty = p.getDuty(val.Value)
ioutil.WriteFile(p.dutyPath, p.duty, PWMMode)
} else {
ioutil.WriteFile(p.runPath, []byte("0"), PWMMode)
ioutil.WriteFile(p.dutyPath, []byte(fmt.Sprintf("%d", p.period)), PWMMode)
}
return ioutil.WriteFile(p.runPath, []byte("1"), PWMMode)
}
func (p *PWM) Off() error {
ioutil.WriteFile(p.dutyPath, []byte("0"), PWMMode)
return ioutil.WriteFile(p.runPath, []byte("0"), PWMMode)
}
func (p *PWM) Status() map[string]bool {
return map[string]bool{"pwm": p.status}
}
func (p *PWM) getDuty(val interface{}) []byte {
d, ok := val.(float64)
if !ok {
return []byte("0")
}
d = math.Abs(d)
f := (d / 100.0) * float64(p.period)
return []byte(fmt.Sprintf("%d", int(f)))
}
func setupPWM(pin *Pin) (devPath string, period int, err error) {
g, e := filepath.Glob(fmt.Sprintf(PWM_DEVPATH, pin.Port, pin.Pin))
if e != nil {
return devPath, period, e
}
if len(g) != 1 {
return devPath, period, errors.New(fmt.Sprintf("couldn't find device path for PWM port %s pin %s path %s", pin.Port, pin.Pin, PWM_DEVPATH))
}
devPath = g[0]
period = int(NANO / float32(pin.Frequency))
p := path.Join(devPath, "period")
err = ioutil.WriteFile(p, []byte(fmt.Sprintf("%d", period)), PWMMode)
if err != nil {
return devPath, period, err
}
p = path.Join(devPath, "duty")
err = ioutil.WriteFile(p, []byte(fmt.Sprintf("%d", period)), PWMMode)
if err != nil {
return devPath, period, err
}
p = path.Join(devPath, "polarity")
err = ioutil.WriteFile(p, []byte("0"), PWMMode)
if err != nil {
return devPath, period, err
}
return devPath, period, err
}
func writePWMDeviceTree(port, pin string) error {
treePath, err := getTreePath()
if err != nil {
return err
}
pwm := Pins["pwm"]
p, ok := pwm[port]
if !ok {
return errors.New(fmt.Sprintf("invalid port: %s", p))
}
val, ok := p[pin]
if !ok {
return errors.New(fmt.Sprintf("invalid pin: %s", pin))
}
err = ioutil.WriteFile(treePath, []byte("am33xx_pwm"), PWMMode)
if err != nil {
return err
}
return ioutil.WriteFile(treePath, []byte(val), PWMMode)
}
func getTreePath() (string, error) {
g, err := filepath.Glob(TREEPATH)
if err != nil {
return "", err
}
if len(g) != 1 {
return "", errors.New("couldn't find device tree path for slots")
}
return g[0], nil
}