mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-02 22:17:12 +08:00
90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
package edison
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"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 {
|
|
p := &pwmPin{pin: strconv.Itoa(pin)}
|
|
p.export()
|
|
p.enable("1")
|
|
return p
|
|
}
|
|
|
|
// enable writes value to pwm enable path
|
|
func (p *pwmPin) enable(val string) {
|
|
err := writeFile(pwmEnablePath(p.pin), val)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// period reads from pwm period path and returns value
|
|
func (p *pwmPin) period() string {
|
|
buf, err := ioutil.ReadFile(pwmPeriodPath(p.pin))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return string(buf[0 : len(buf)-1])
|
|
}
|
|
|
|
// writeDuty writes value to pwm duty cycle path
|
|
func (p *pwmPin) writeDuty(duty string) {
|
|
err := writeFile(pwmDutyCyclePath(p.pin), duty)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// export writes pin to pwm export path
|
|
func (p *pwmPin) export() {
|
|
writeFile(pwmExportPath(), p.pin)
|
|
}
|
|
|
|
// export writes pin to pwm unexport path
|
|
func (p *pwmPin) unexport() {
|
|
writeFile(pwmUnExportPath(), p.pin)
|
|
}
|
|
|
|
// close disables and unexports pin
|
|
func (p *pwmPin) close() {
|
|
p.enable("0")
|
|
p.unexport()
|
|
}
|