mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-02 22:17:12 +08:00
sysfs: make PWMPinner interface more consistent
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
ce7b34d031
commit
56285df3d3
@ -14,14 +14,18 @@ type PWMPinner interface {
|
|||||||
Unexport() error
|
Unexport() error
|
||||||
// Enable enables/disables the PWM pin
|
// Enable enables/disables the PWM pin
|
||||||
Enable(bool) (err error)
|
Enable(bool) (err error)
|
||||||
|
// Polarity returns the polarity either normal or inverted
|
||||||
|
Polarity() (polarity string, err error)
|
||||||
|
// InvertPolarity sets the polarity to inverted if called with true
|
||||||
|
InvertPolarity(invert 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 uint32, err error)
|
||||||
// SetPeriod sets the current PWM period for pin
|
// SetPeriod sets the current PWM period for pin
|
||||||
SetPeriod(period string) (err error)
|
SetPeriod(period uint32) (err error)
|
||||||
// DutyCycle returns the duty cycle for the pin
|
// DutyCycle returns the duty cycle for the pin
|
||||||
DutyCycle() (duty float64, err error)
|
DutyCycle() (duty uint32, err error)
|
||||||
// SetDutyCycle writes the duty cycle to the pin
|
// SetDutyCycle writes the duty cycle to the pin
|
||||||
SetDutyCycle(duty float64) (err error)
|
SetDutyCycle(duty uint32) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PWMPin struct {
|
type PWMPin struct {
|
||||||
@ -67,8 +71,21 @@ func (p *PWMPin) Enable(enable bool) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPolarityInverted writes value to pwm polarity path
|
// Polarity returns current polarity value
|
||||||
func (p *PWMPin) SetPolarityInverted(invert bool) (err error) {
|
func (p *PWMPin) Polarity() (polarity string, err error) {
|
||||||
|
buf, err := p.read(p.pwmPolarityPath())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(buf) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(buf), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InvertPolarity writes value to pwm polarity path
|
||||||
|
func (p *PWMPin) InvertPolarity(invert bool) (err error) {
|
||||||
if p.enabled {
|
if p.enabled {
|
||||||
polarity := "normal"
|
polarity := "normal"
|
||||||
if invert {
|
if invert {
|
||||||
@ -79,16 +96,18 @@ func (p *PWMPin) SetPolarityInverted(invert bool) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Period reads from pwm period path and returns value
|
// Period reads from pwm period path and returns value in nanoseconds
|
||||||
func (p *PWMPin) Period() (period string, err error) {
|
func (p *PWMPin) Period() (period uint32, err error) {
|
||||||
buf, err := p.read(p.pwmPeriodPath())
|
buf, err := p.read(p.pwmPeriodPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
return "0", nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
return string(buf), nil
|
|
||||||
|
val, e := strconv.Atoi(string(buf))
|
||||||
|
return uint32(val), e
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPeriod sets pwm period in nanoseconds
|
// SetPeriod sets pwm period in nanoseconds
|
||||||
@ -97,13 +116,15 @@ func (p *PWMPin) SetPeriod(period uint32) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// DutyCycle reads from pwm duty cycle path and returns value
|
// DutyCycle reads from pwm duty cycle path and returns value in nanoseconds
|
||||||
func (p *PWMPin) DutyCycle() (duty string, err error) {
|
func (p *PWMPin) DutyCycle() (duty uint32, err error) {
|
||||||
buf, err := p.read(p.pwmDutyCyclePath())
|
buf, err := p.read(p.pwmDutyCyclePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return string(buf), nil
|
|
||||||
|
val, e := strconv.Atoi(string(buf))
|
||||||
|
return uint32(val), e
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDutyCycle writes value to pwm duty cycle path
|
// SetDutyCycle writes value to pwm duty cycle path
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"gobot.io/x/gobot/gobottest"
|
"gobot.io/x/gobot/gobottest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ PWMPinner = (*PWMPin)(nil)
|
||||||
|
|
||||||
func TestPwmPin(t *testing.T) {
|
func TestPwmPin(t *testing.T) {
|
||||||
fs := NewMockFilesystem([]string{
|
fs := NewMockFilesystem([]string{
|
||||||
"/sys/class/pwm/pwmchip0/export",
|
"/sys/class/pwm/pwmchip0/export",
|
||||||
@ -38,14 +40,14 @@ func TestPwmPin(t *testing.T) {
|
|||||||
|
|
||||||
fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6"
|
fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6"
|
||||||
data, _ := pin.Period()
|
data, _ := pin.Period()
|
||||||
gobottest.Assert(t, data, "6")
|
gobottest.Assert(t, data, uint32(6))
|
||||||
gobottest.Assert(t, pin.SetPeriod(100000), nil)
|
gobottest.Assert(t, pin.SetPeriod(100000), nil)
|
||||||
data, _ = pin.Period()
|
data, _ = pin.Period()
|
||||||
gobottest.Assert(t, data, "100000")
|
gobottest.Assert(t, data, uint32(100000))
|
||||||
|
|
||||||
gobottest.Assert(t, pin.SetPolarityInverted(true), nil)
|
gobottest.Assert(t, pin.InvertPolarity(true), nil)
|
||||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "inverted")
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "inverted")
|
||||||
gobottest.Assert(t, pin.SetPolarityInverted(false), nil)
|
gobottest.Assert(t, pin.InvertPolarity(false), nil)
|
||||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "normal")
|
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")
|
||||||
@ -53,7 +55,7 @@ func TestPwmPin(t *testing.T) {
|
|||||||
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")
|
||||||
data, _ = pin.DutyCycle()
|
data, _ = pin.DutyCycle()
|
||||||
gobottest.Assert(t, data, "100")
|
gobottest.Assert(t, data, uint32(100))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPwmPinExportError(t *testing.T) {
|
func TestPwmPinExportError(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user