mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-29 13:49:14 +08:00
sysfs: add Chip to be able to change pwmchip, and some related refactoring
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
79288f837b
commit
05aeaf7d79
@ -21,39 +21,41 @@ type PWMPin interface {
|
|||||||
|
|
||||||
type pwmPin struct {
|
type pwmPin struct {
|
||||||
pin string
|
pin string
|
||||||
|
Chip string
|
||||||
write func(path string, data []byte) (i int, err error)
|
write func(path string, data []byte) (i int, err error)
|
||||||
read func(path string) ([]byte, error)
|
read func(path string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPwmPin returns a new pwmPin
|
// NewPwmPin returns a new pwmPin
|
||||||
func NewPwmPin(pin int) *pwmPin {
|
func NewPwmPin(pin int) *pwmPin {
|
||||||
p := &pwmPin{pin: strconv.Itoa(pin)}
|
return &pwmPin{
|
||||||
p.read = readPwmFile
|
pin: strconv.Itoa(pin),
|
||||||
p.write = writePwmFile
|
Chip: "0",
|
||||||
return p
|
read: readPwmFile,
|
||||||
|
write: writePwmFile}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export writes pin to pwm export path
|
// Export writes pin to pwm export path
|
||||||
func (p *pwmPin) Export() (err error) {
|
func (p *pwmPin) Export() (err error) {
|
||||||
_, err = p.write(pwmExportPath(), []byte(p.pin))
|
_, err = p.write(p.pwmExportPath(), []byte(p.pin))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unexport writes pin to pwm unexport path
|
// Unexport writes pin to pwm unexport path
|
||||||
func (p *pwmPin) Unexport() (err error) {
|
func (p *pwmPin) Unexport() (err error) {
|
||||||
_, err = p.write(pwmUnexportPath(), []byte(p.pin))
|
_, err = p.write(p.pwmUnexportPath(), []byte(p.pin))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable writes value to pwm enable path
|
// Enable writes value to pwm enable path
|
||||||
func (p *pwmPin) Enable(val string) (err error) {
|
func (p *pwmPin) Enable(val string) (err error) {
|
||||||
_, err = p.write(pwmEnablePath(p.pin), []byte(val))
|
_, err = p.write(p.pwmEnablePath(), []byte(val))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Period reads from pwm period path and returns value
|
// Period reads from pwm period path and returns value
|
||||||
func (p *pwmPin) Period() (period string, err error) {
|
func (p *pwmPin) Period() (period string, err error) {
|
||||||
buf, err := p.read(pwmPeriodPath(p.pin))
|
buf, err := p.read(p.pwmPeriodPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -62,38 +64,38 @@ func (p *pwmPin) Period() (period string, err error) {
|
|||||||
|
|
||||||
// WriteDuty writes value to pwm duty cycle path
|
// WriteDuty writes value to pwm duty cycle path
|
||||||
func (p *pwmPin) WriteDuty(duty string) (err error) {
|
func (p *pwmPin) WriteDuty(duty string) (err error) {
|
||||||
_, err = p.write(pwmDutyCyclePath(p.pin), []byte(duty))
|
_, err = p.write(p.pwmDutyCyclePath(), []byte(duty))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// pwmPath returns pwm base path
|
// pwmPath returns pwm base path
|
||||||
func pwmPath() string {
|
func (p *pwmPin) pwmPath() string {
|
||||||
return "/sys/class/pwm/pwmchip0"
|
return "/sys/class/pwm/pwmchip" + p.Chip
|
||||||
}
|
}
|
||||||
|
|
||||||
// pwmExportPath returns export path
|
// pwmExportPath returns export path
|
||||||
func pwmExportPath() string {
|
func (p *pwmPin) pwmExportPath() string {
|
||||||
return pwmPath() + "/export"
|
return p.pwmPath() + "/export"
|
||||||
}
|
}
|
||||||
|
|
||||||
// pwmUnexportPath returns unexport path
|
// pwmUnexportPath returns unexport path
|
||||||
func pwmUnexportPath() string {
|
func (p *pwmPin) pwmUnexportPath() string {
|
||||||
return pwmPath() + "/unexport"
|
return p.pwmPath() + "/unexport"
|
||||||
}
|
}
|
||||||
|
|
||||||
// pwmDutyCyclePath returns duty_cycle path for specified pin
|
// pwmDutyCyclePath returns duty_cycle path for specified pin
|
||||||
func pwmDutyCyclePath(pin string) string {
|
func (p *pwmPin) pwmDutyCyclePath() string {
|
||||||
return pwmPath() + "/pwm" + pin + "/duty_cycle"
|
return p.pwmPath() + "/pwm" + p.pin + "/duty_cycle"
|
||||||
}
|
}
|
||||||
|
|
||||||
// pwmPeriodPath returns period path for specified pin
|
// pwmPeriodPath returns period path for specified pin
|
||||||
func pwmPeriodPath(pin string) string {
|
func (p *pwmPin) pwmPeriodPath() string {
|
||||||
return pwmPath() + "/pwm" + pin + "/period"
|
return p.pwmPath() + "/pwm" + p.pin + "/period"
|
||||||
}
|
}
|
||||||
|
|
||||||
// pwmEnablePath returns enable path for specified pin
|
// pwmEnablePath returns enable path for specified pin
|
||||||
func pwmEnablePath(pin string) string {
|
func (p *pwmPin) pwmEnablePath() string {
|
||||||
return pwmPath() + "/pwm" + pin + "/enable"
|
return p.pwmPath() + "/pwm" + p.pin + "/enable"
|
||||||
}
|
}
|
||||||
|
|
||||||
func writePwmFile(path string, data []byte) (i int, err error) {
|
func writePwmFile(path string, data []byte) (i int, err error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user