2014-04-28 19:39:51 +08:00
|
|
|
package beaglebone
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-12-24 10:35:45 +08:00
|
|
|
"errors"
|
2014-04-26 18:11:51 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2014-12-24 10:35:45 +08:00
|
|
|
"time"
|
2014-11-23 10:59:27 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot/sysfs"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type pwmPin struct {
|
|
|
|
pinNum string
|
|
|
|
pwmDevice string
|
|
|
|
}
|
|
|
|
|
2014-10-16 23:59:23 +08:00
|
|
|
// newPwmPin creates a new pwm pin with specified pin number
|
2014-11-17 06:18:29 +08:00
|
|
|
func newPwmPin(pinNum string, ocp string) (p *pwmPin, err error) {
|
2014-12-24 10:35:45 +08:00
|
|
|
done := make(chan error, 0)
|
2014-11-17 06:18:29 +08:00
|
|
|
p = &pwmPin{
|
2014-07-01 10:50:28 +08:00
|
|
|
pinNum: strings.ToUpper(pinNum),
|
|
|
|
}
|
2014-06-12 03:59:06 +08:00
|
|
|
|
2014-11-17 06:18:29 +08:00
|
|
|
pwmDevice, err := glob(fmt.Sprintf("%v/pwm_test_%v.*", ocp, p.pinNum))
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
2014-11-17 06:18:29 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-07-01 10:50:28 +08:00
|
|
|
|
2014-11-17 06:18:29 +08:00
|
|
|
p.pwmDevice = pwmDevice[0]
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-12-24 10:35:45 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if _, err := sysfs.OpenFile(fmt.Sprintf("%v/period", p.pwmDevice), os.O_RDONLY, 0644); err == nil {
|
|
|
|
break
|
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-12-24 10:35:45 +08:00
|
|
|
for {
|
|
|
|
if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/duty", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
|
|
|
|
defer fi.Close()
|
|
|
|
if _, err = fi.WriteString("0"); err != nil {
|
|
|
|
done <- err
|
|
|
|
}
|
|
|
|
fi.Sync()
|
|
|
|
break
|
|
|
|
}
|
2014-07-01 10:50:28 +08:00
|
|
|
}
|
2014-12-24 10:35:45 +08:00
|
|
|
for {
|
|
|
|
if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/polarity", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
|
|
|
|
defer fi.Close()
|
|
|
|
if _, err = fi.WriteString("0"); err != nil {
|
|
|
|
done <- err
|
|
|
|
}
|
|
|
|
fi.Sync()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/run", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
|
|
|
|
defer fi.Close()
|
|
|
|
if _, err = fi.WriteString("1"); err != nil {
|
|
|
|
done <- err
|
|
|
|
}
|
|
|
|
fi.Sync()
|
|
|
|
break
|
|
|
|
}
|
2014-07-01 10:50:28 +08:00
|
|
|
}
|
2014-12-24 10:35:45 +08:00
|
|
|
done <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err = <-done:
|
|
|
|
return p, err
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
return p, errors.New("could not initialize pwm device")
|
2014-07-01 10:50:28 +08:00
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 23:59:23 +08:00
|
|
|
// pwmWrite writes to a pwm pin with specified period and duty
|
2014-11-17 06:18:29 +08:00
|
|
|
func (p *pwmPin) pwmWrite(period string, duty string) (err error) {
|
2014-11-10 06:35:36 +08:00
|
|
|
f1, err := sysfs.OpenFile(fmt.Sprintf("%v/period", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
|
defer f1.Close()
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
2014-11-17 06:18:29 +08:00
|
|
|
return
|
|
|
|
}
|
2014-12-24 10:35:45 +08:00
|
|
|
if _, err = f1.WriteString(period); err != nil {
|
2014-11-17 06:18:29 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-11-10 06:35:36 +08:00
|
|
|
f2, err := sysfs.OpenFile(fmt.Sprintf("%v/duty", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
|
defer f2.Close()
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
2014-11-17 06:18:29 +08:00
|
|
|
return
|
|
|
|
}
|
2014-12-24 10:35:45 +08:00
|
|
|
|
2014-11-17 06:18:29 +08:00
|
|
|
_, err = f2.WriteString(duty)
|
|
|
|
|
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-10-16 23:59:23 +08:00
|
|
|
// releae writes string to close a pwm pin
|
2014-11-17 06:18:29 +08:00
|
|
|
func (p *pwmPin) release() (err error) {
|
2014-11-10 06:35:36 +08:00
|
|
|
fi, err := sysfs.OpenFile(fmt.Sprintf("%v/run", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
|
defer fi.Close()
|
2014-04-26 18:11:51 +08:00
|
|
|
if err != nil {
|
2014-11-17 06:18:29 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-11-17 06:18:29 +08:00
|
|
|
_, err = fi.WriteString("0")
|
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|