2014-09-19 03:45:54 +08:00
|
|
|
package edison
|
|
|
|
|
2014-11-05 04:21:18 +08:00
|
|
|
import "strconv"
|
2014-09-19 03:45:54 +08:00
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// pwmPath returns pwm base path
|
2014-09-19 03:45:54 +08:00
|
|
|
func pwmPath() string {
|
|
|
|
return "/sys/class/pwm/pwmchip0"
|
|
|
|
}
|
2014-10-21 02:17:47 +08:00
|
|
|
|
|
|
|
// pwmExportPath returns export path
|
2014-09-19 03:45:54 +08:00
|
|
|
func pwmExportPath() string {
|
|
|
|
return pwmPath() + "/export"
|
|
|
|
}
|
2014-10-21 02:17:47 +08:00
|
|
|
|
|
|
|
// pwmUnExportPath returns unexport path
|
2014-09-19 03:45:54 +08:00
|
|
|
func pwmUnExportPath() string {
|
|
|
|
return pwmPath() + "/unexport"
|
|
|
|
}
|
2014-10-21 02:17:47 +08:00
|
|
|
|
|
|
|
// pwmDutyCyclePath returns duty_cycle path for specified pin
|
2014-09-19 03:45:54 +08:00
|
|
|
func pwmDutyCyclePath(pin string) string {
|
|
|
|
return pwmPath() + "/pwm" + pin + "/duty_cycle"
|
|
|
|
}
|
2014-10-21 02:17:47 +08:00
|
|
|
|
|
|
|
// pwmPeriodPath returns period path for specified pin
|
2014-09-19 03:45:54 +08:00
|
|
|
func pwmPeriodPath(pin string) string {
|
|
|
|
return pwmPath() + "/pwm" + pin + "/period"
|
|
|
|
}
|
2014-10-21 02:17:47 +08:00
|
|
|
|
|
|
|
// pwmEnablePath returns enable path for specified pin
|
2014-09-19 03:45:54 +08:00
|
|
|
func pwmEnablePath(pin string) string {
|
|
|
|
return pwmPath() + "/pwm" + pin + "/enable"
|
|
|
|
}
|
|
|
|
|
|
|
|
type pwmPin struct {
|
|
|
|
pin string
|
|
|
|
}
|
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// newPwmPin returns an exported and enabled pwmPin
|
2014-09-19 03:45:54 +08:00
|
|
|
func newPwmPin(pin int) *pwmPin {
|
2014-11-20 03:53:05 +08:00
|
|
|
return &pwmPin{pin: strconv.Itoa(pin)}
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// enable writes value to pwm enable path
|
2014-11-20 03:53:05 +08:00
|
|
|
func (p *pwmPin) enable(val string) (err error) {
|
|
|
|
_, err = writeFile(pwmEnablePath(p.pin), []byte(val))
|
|
|
|
return
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// period reads from pwm period path and returns value
|
2014-11-20 03:53:05 +08:00
|
|
|
func (p *pwmPin) period() (period string, err error) {
|
2014-11-05 04:21:18 +08:00
|
|
|
buf, err := readFile(pwmPeriodPath(p.pin))
|
2014-09-19 03:45:54 +08:00
|
|
|
if err != nil {
|
2014-11-20 03:53:05 +08:00
|
|
|
return
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|
2014-11-20 03:53:05 +08:00
|
|
|
return string(buf[0 : len(buf)-1]), nil
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// writeDuty writes value to pwm duty cycle path
|
2014-11-20 03:53:05 +08:00
|
|
|
func (p *pwmPin) writeDuty(duty string) (err error) {
|
|
|
|
_, err = writeFile(pwmDutyCyclePath(p.pin), []byte(duty))
|
|
|
|
return
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// export writes pin to pwm export path
|
2014-11-20 03:53:05 +08:00
|
|
|
func (p *pwmPin) export() (err error) {
|
|
|
|
_, err = writeFile(pwmExportPath(), []byte(p.pin))
|
|
|
|
return
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 02:17:47 +08:00
|
|
|
// export writes pin to pwm unexport path
|
2014-11-20 03:53:05 +08:00
|
|
|
func (p *pwmPin) unexport() (err error) {
|
|
|
|
_, err = writeFile(pwmUnExportPath(), []byte(p.pin))
|
|
|
|
return
|
2014-09-19 03:45:54 +08:00
|
|
|
}
|