Skip to content

Commit

Permalink
sysfs: make PWMPinner interface more consistent
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 25, 2017
1 parent ce7b34d commit 56285df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
47 changes: 34 additions & 13 deletions sysfs/pwm_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions sysfs/pwm_pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -38,22 +40,22 @@ 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")
err = pin.SetDutyCycle(100)
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) {
Expand Down

0 comments on commit 56285df

Please sign in to comment.