1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00

sysfs: refactor interface for better match with actual interface

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-21 23:06:45 +02:00
parent 93565733e0
commit 0eff347997
2 changed files with 71 additions and 18 deletions

View File

@ -1,6 +1,7 @@
package sysfs package sysfs
import ( import (
"fmt"
"os" "os"
"strconv" "strconv"
) )
@ -12,16 +13,21 @@ type PWMPinner interface {
// Unexport unexports the pin and releases the pin from the operating system // Unexport unexports the pin and releases the pin from the operating system
Unexport() error Unexport() error
// Enable enables/disables the PWM pin // Enable enables/disables the PWM pin
Enable(val string) (err error) Enable(bool) (err error)
// Period returns the current PWM period for pin // Period returns the current PWM period for pin
Period() (period string, err error) Period() (period string, err error)
// WriteDuty writes the duty cycle to the pin // SetPeriod sets the current PWM period for pin
WriteDuty(duty string) (err error) SetPeriod(period string) (err error)
// DutyCycle returns the duty cycle for the pin
DutyCycle() (duty float64, err error)
// SetDutyCycle writes the duty cycle to the pin
SetDutyCycle(duty float64) (err error)
} }
type PWMPin struct { type PWMPin struct {
pin string pin string
Chip string Chip string
enabled bool
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)
} }
@ -30,6 +36,7 @@ type PWMPin struct {
func NewPWMPin(pin int) *PWMPin { func NewPWMPin(pin int) *PWMPin {
return &PWMPin{ return &PWMPin{
pin: strconv.Itoa(pin), pin: strconv.Itoa(pin),
enabled: false,
Chip: "0", Chip: "0",
read: readPwmFile, read: readPwmFile,
write: writePwmFile} write: writePwmFile}
@ -48,8 +55,27 @@ func (p *PWMPin) Unexport() (err error) {
} }
// 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(enable bool) (err error) {
_, err = p.write(p.pwmEnablePath(), []byte(val)) if p.enabled != enable {
p.enabled = enable
enableVal := 0
if enable {
enableVal = 1
}
_, err = p.write(p.pwmEnablePath(), []byte(fmt.Sprintf("%v", enableVal)))
}
return
}
// SetPolarityInverted writes value to pwm polarity path
func (p *PWMPin) SetPolarityInverted(invert bool) (err error) {
if p.enabled {
polarity := "normal"
if invert {
polarity = "inverted"
}
_, err = p.write(p.pwmPolarityPath(), []byte(polarity))
}
return return
} }
@ -62,9 +88,25 @@ func (p *PWMPin) Period() (period string, err error) {
return string(buf), nil return string(buf), nil
} }
// WriteDuty writes value to pwm duty cycle path // SetPeriod sets pwm period in nanoseconds
func (p *PWMPin) WriteDuty(duty string) (err error) { func (p *PWMPin) SetPeriod(period uint32) (err error) {
_, err = p.write(p.pwmDutyCyclePath(), []byte(duty)) _, err = p.write(p.pwmPeriodPath(), []byte(fmt.Sprintf("%v", period)))
return
}
// DutyCycle reads from pwm duty cycle path and returns value
func (p *PWMPin) DutyCycle() (duty string, err error) {
buf, err := p.read(p.pwmDutyCyclePath())
if err != nil {
return
}
return string(buf), nil
}
// SetDutyCycle writes value to pwm duty cycle path
// duty is in nanoseconds
func (p *PWMPin) SetDutyCycle(duty uint32) (err error) {
_, err = p.write(p.pwmDutyCyclePath(), []byte(fmt.Sprintf("%v", duty)))
return return
} }
@ -98,6 +140,11 @@ func (p *PWMPin) pwmEnablePath() string {
return p.pwmPath() + "/pwm" + p.pin + "/enable" return p.pwmPath() + "/pwm" + p.pin + "/enable"
} }
// pwmPolarityPath returns polarity path for specified pin
func (p *PWMPin) pwmPolarityPath() string {
return p.pwmPath() + "/pwm" + p.pin + "/polarity"
}
func writePwmFile(path string, data []byte) (i int, err error) { func writePwmFile(path string, data []byte) (i int, err error) {
file, err := OpenFile(path, os.O_WRONLY, 0644) file, err := OpenFile(path, os.O_WRONLY, 0644)
defer file.Close() defer file.Close()

View File

@ -15,6 +15,7 @@ func TestPwmPin(t *testing.T) {
"/sys/class/pwm/pwmchip0/pwm10/enable", "/sys/class/pwm/pwmchip0/pwm10/enable",
"/sys/class/pwm/pwmchip0/pwm10/period", "/sys/class/pwm/pwmchip0/pwm10/period",
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle", "/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
"/sys/class/pwm/pwmchip0/pwm10/polarity",
}) })
SetFilesystem(fs) SetFilesystem(fs)
@ -31,7 +32,7 @@ func TestPwmPin(t *testing.T) {
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "10") gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "10")
gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1") gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1")
err = pin.Enable("1") err = pin.Enable(true)
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1") gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1")
@ -39,8 +40,13 @@ func TestPwmPin(t *testing.T) {
data, _ := pin.Period() data, _ := pin.Period()
gobottest.Assert(t, data, "6") gobottest.Assert(t, data, "6")
gobottest.Assert(t, pin.SetPolarityInverted(true), nil)
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "inverted")
gobottest.Assert(t, pin.SetPolarityInverted(false), nil)
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "normal")
gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "1") gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "1")
err = pin.WriteDuty("100") err = pin.SetDutyCycle(100)
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "100") gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "100")
} }