Skip to content

Commit

Permalink
sysfs: refactor interface for better match with actual interface
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 21, 2017
1 parent 9356573 commit 0eff347
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
79 changes: 63 additions & 16 deletions sysfs/pwm_pin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sysfs

import (
"fmt"
"os"
"strconv"
)
Expand All @@ -12,27 +13,33 @@ type PWMPinner interface {
// Unexport unexports the pin and releases the pin from the operating system
Unexport() error
// Enable enables/disables the PWM pin
Enable(val string) (err error)
Enable(bool) (err error)
// Period returns the current PWM period for pin
Period() (period string, err error)
// WriteDuty writes the duty cycle to the pin
WriteDuty(duty string) (err error)
// SetPeriod sets the current PWM period for pin
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 {
pin string
Chip string
write func(path string, data []byte) (i int, err error)
read func(path string) ([]byte, error)
pin string
Chip string
enabled bool
write func(path string, data []byte) (i int, err error)
read func(path string) ([]byte, error)
}

// NewPwmPin returns a new pwmPin
func NewPWMPin(pin int) *PWMPin {
return &PWMPin{
pin: strconv.Itoa(pin),
Chip: "0",
read: readPwmFile,
write: writePwmFile}
pin: strconv.Itoa(pin),
enabled: false,
Chip: "0",
read: readPwmFile,
write: writePwmFile}
}

// Export writes pin to pwm export path
Expand All @@ -48,8 +55,27 @@ func (p *PWMPin) Unexport() (err error) {
}

// Enable writes value to pwm enable path
func (p *PWMPin) Enable(val string) (err error) {
_, err = p.write(p.pwmEnablePath(), []byte(val))
func (p *PWMPin) Enable(enable bool) (err error) {
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
}

Expand All @@ -62,9 +88,25 @@ func (p *PWMPin) Period() (period string, err error) {
return string(buf), nil
}

// WriteDuty writes value to pwm duty cycle path
func (p *PWMPin) WriteDuty(duty string) (err error) {
_, err = p.write(p.pwmDutyCyclePath(), []byte(duty))
// SetPeriod sets pwm period in nanoseconds
func (p *PWMPin) SetPeriod(period uint32) (err error) {
_, 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
}

Expand Down Expand Up @@ -98,6 +140,11 @@ func (p *PWMPin) pwmEnablePath() string {
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) {
file, err := OpenFile(path, os.O_WRONLY, 0644)
defer file.Close()
Expand Down
10 changes: 8 additions & 2 deletions sysfs/pwm_pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestPwmPin(t *testing.T) {
"/sys/class/pwm/pwmchip0/pwm10/enable",
"/sys/class/pwm/pwmchip0/pwm10/period",
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
"/sys/class/pwm/pwmchip0/pwm10/polarity",
})

SetFilesystem(fs)
Expand All @@ -31,16 +32,21 @@ func TestPwmPin(t *testing.T) {
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")
err = pin.Enable("1")
err = pin.Enable(true)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1")

fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6"
data, _ := pin.Period()
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")
err = pin.WriteDuty("100")
err = pin.SetDutyCycle(100)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "100")
}
Expand Down

0 comments on commit 0eff347

Please sign in to comment.