mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +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
|
||||
// Enable enables/disables the PWM pin
|
||||
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() (period string, err error)
|
||||
Period() (period uint32, err error)
|
||||
// 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() (duty float64, err error)
|
||||
DutyCycle() (duty uint32, err error)
|
||||
// SetDutyCycle writes the duty cycle to the pin
|
||||
SetDutyCycle(duty float64) (err error)
|
||||
SetDutyCycle(duty uint32) (err error)
|
||||
}
|
||||
|
||||
type PWMPin struct {
|
||||
@ -67,8 +71,21 @@ func (p *PWMPin) Enable(enable bool) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// SetPolarityInverted writes value to pwm polarity path
|
||||
func (p *PWMPin) SetPolarityInverted(invert bool) (err error) {
|
||||
// Polarity returns current polarity value
|
||||
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 {
|
||||
polarity := "normal"
|
||||
if invert {
|
||||
@ -79,16 +96,18 @@ func (p *PWMPin) SetPolarityInverted(invert bool) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Period reads from pwm period path and returns value
|
||||
func (p *PWMPin) Period() (period string, err error) {
|
||||
// Period reads from pwm period path and returns value in nanoseconds
|
||||
func (p *PWMPin) Period() (period uint32, err error) {
|
||||
buf, err := p.read(p.pwmPeriodPath())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
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
|
||||
@ -97,13 +116,15 @@ func (p *PWMPin) SetPeriod(period uint32) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// DutyCycle reads from pwm duty cycle path and returns value
|
||||
func (p *PWMPin) DutyCycle() (duty string, err error) {
|
||||
// DutyCycle reads from pwm duty cycle path and returns value in nanoseconds
|
||||
func (p *PWMPin) DutyCycle() (duty uint32, err error) {
|
||||
buf, err := p.read(p.pwmDutyCyclePath())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return string(buf), nil
|
||||
|
||||
val, e := strconv.Atoi(string(buf))
|
||||
return uint32(val), e
|
||||
}
|
||||
|
||||
// SetDutyCycle writes value to pwm duty cycle path
|
||||
|
@ -8,6 +8,8 @@ import (
|
||||
"gobot.io/x/gobot/gobottest"
|
||||
)
|
||||
|
||||
var _ PWMPinner = (*PWMPin)(nil)
|
||||
|
||||
func TestPwmPin(t *testing.T) {
|
||||
fs := NewMockFilesystem([]string{
|
||||
"/sys/class/pwm/pwmchip0/export",
|
||||
@ -38,14 +40,14 @@ func TestPwmPin(t *testing.T) {
|
||||
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6"
|
||||
data, _ := pin.Period()
|
||||
gobottest.Assert(t, data, "6")
|
||||
gobottest.Assert(t, data, uint32(6))
|
||||
gobottest.Assert(t, pin.SetPeriod(100000), nil)
|
||||
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, 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.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, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "100")
|
||||
data, _ = pin.DutyCycle()
|
||||
gobottest.Assert(t, data, "100")
|
||||
gobottest.Assert(t, data, uint32(100))
|
||||
}
|
||||
|
||||
func TestPwmPinExportError(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user