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

sysfs: changes needed to allow use of PWMPin from Adaptors

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-21 19:50:17 +02:00
parent 05aeaf7d79
commit 0d1362bef1
4 changed files with 19 additions and 170 deletions

View File

@ -1,75 +0,0 @@
package edison
import "strconv"
// pwmPath returns pwm base path
func pwmPath() string {
return "/sys/class/pwm/pwmchip0"
}
// pwmExportPath returns export path
func pwmExportPath() string {
return pwmPath() + "/export"
}
// pwmUnExportPath returns unexport path
func pwmUnExportPath() string {
return pwmPath() + "/unexport"
}
// pwmDutyCyclePath returns duty_cycle path for specified pin
func pwmDutyCyclePath(pin string) string {
return pwmPath() + "/pwm" + pin + "/duty_cycle"
}
// pwmPeriodPath returns period path for specified pin
func pwmPeriodPath(pin string) string {
return pwmPath() + "/pwm" + pin + "/period"
}
// pwmEnablePath returns enable path for specified pin
func pwmEnablePath(pin string) string {
return pwmPath() + "/pwm" + pin + "/enable"
}
type pwmPin struct {
pin string
}
// newPwmPin returns an exported and enabled pwmPin
func newPwmPin(pin int) *pwmPin {
return &pwmPin{pin: strconv.Itoa(pin)}
}
// enable writes value to pwm enable path
func (p *pwmPin) enable(val string) (err error) {
_, err = writeFile(pwmEnablePath(p.pin), []byte(val))
return
}
// period reads from pwm period path and returns value
func (p *pwmPin) period() (period string, err error) {
buf, err := readFile(pwmPeriodPath(p.pin))
if err != nil {
return
}
return string(buf[0 : len(buf)-1]), nil
}
// writeDuty writes value to pwm duty cycle path
func (p *pwmPin) writeDuty(duty string) (err error) {
_, err = writeFile(pwmDutyCyclePath(p.pin), []byte(duty))
return
}
// export writes pin to pwm export path
func (p *pwmPin) export() (err error) {
_, err = writeFile(pwmExportPath(), []byte(p.pin))
return
}
// export writes pin to pwm unexport path
func (p *pwmPin) unexport() (err error) {
_, err = writeFile(pwmUnExportPath(), []byte(p.pin))
return
}

View File

@ -1,76 +0,0 @@
// Package joule pwm implementation
package joule
import "strconv"
// pwmPath returns pwm base path
func pwmPath() string {
return "/sys/class/pwm/pwmchip0"
}
// pwmExportPath returns export path
func pwmExportPath() string {
return pwmPath() + "/export"
}
// pwmUnExportPath returns unexport path
func pwmUnExportPath() string {
return pwmPath() + "/unexport"
}
// pwmDutyCyclePath returns duty_cycle path for specified pin
func pwmDutyCyclePath(pin string) string {
return pwmPath() + "/pwm" + pin + "/duty_cycle"
}
// pwmPeriodPath returns period path for specified pin
func pwmPeriodPath(pin string) string {
return pwmPath() + "/pwm" + pin + "/period"
}
// pwmEnablePath returns enable path for specified pin
func pwmEnablePath(pin string) string {
return pwmPath() + "/pwm" + pin + "/enable"
}
type pwmPin struct {
pin string
}
// newPwmPin returns an exported and enabled pwmPin
func newPwmPin(pin int) *pwmPin {
return &pwmPin{pin: strconv.Itoa(pin)}
}
// enable writes value to pwm enable path
func (p *pwmPin) enable(val string) (err error) {
_, err = writeFile(pwmEnablePath(p.pin), []byte(val))
return
}
// period reads from pwm period path and returns value
func (p *pwmPin) period() (period string, err error) {
buf, err := readFile(pwmPeriodPath(p.pin))
if err != nil {
return
}
return string(buf[0 : len(buf)-1]), nil
}
// writeDuty writes value to pwm duty cycle path
func (p *pwmPin) writeDuty(duty string) (err error) {
_, err = writeFile(pwmDutyCyclePath(p.pin), []byte(duty))
return
}
// export writes pin to pwm export path
func (p *pwmPin) export() (err error) {
_, err = writeFile(pwmExportPath(), []byte(p.pin))
return
}
// export writes pin to pwm unexport path
func (p *pwmPin) unexport() (err error) {
_, err = writeFile(pwmUnExportPath(), []byte(p.pin))
return
}

View File

@ -6,7 +6,7 @@ import (
)
// PWMPin is the interface for sysfs PWM interactions
type PWMPin interface {
type PWMPinner interface {
// Export exports the pin for use by the operating system
Export() error
// Unexport unexports the pin and releases the pin from the operating system
@ -19,7 +19,7 @@ type PWMPin interface {
WriteDuty(duty string) (err error)
}
type pwmPin struct {
type PWMPin struct {
pin string
Chip string
write func(path string, data []byte) (i int, err error)
@ -27,8 +27,8 @@ type pwmPin struct {
}
// NewPwmPin returns a new pwmPin
func NewPwmPin(pin int) *pwmPin {
return &pwmPin{
func NewPWMPin(pin int) *PWMPin {
return &PWMPin{
pin: strconv.Itoa(pin),
Chip: "0",
read: readPwmFile,
@ -36,25 +36,25 @@ func NewPwmPin(pin int) *pwmPin {
}
// Export writes pin to pwm export path
func (p *pwmPin) Export() (err error) {
func (p *PWMPin) Export() (err error) {
_, err = p.write(p.pwmExportPath(), []byte(p.pin))
return
}
// Unexport writes pin to pwm unexport path
func (p *pwmPin) Unexport() (err error) {
func (p *PWMPin) Unexport() (err error) {
_, err = p.write(p.pwmUnexportPath(), []byte(p.pin))
return
}
// Enable writes value to pwm enable path
func (p *pwmPin) Enable(val string) (err error) {
func (p *PWMPin) Enable(val string) (err error) {
_, err = p.write(p.pwmEnablePath(), []byte(val))
return
}
// Period reads from pwm period path and returns value
func (p *pwmPin) Period() (period string, err error) {
func (p *PWMPin) Period() (period string, err error) {
buf, err := p.read(p.pwmPeriodPath())
if err != nil {
return
@ -63,38 +63,38 @@ func (p *pwmPin) Period() (period string, err error) {
}
// WriteDuty writes value to pwm duty cycle path
func (p *pwmPin) WriteDuty(duty string) (err error) {
func (p *PWMPin) WriteDuty(duty string) (err error) {
_, err = p.write(p.pwmDutyCyclePath(), []byte(duty))
return
}
// pwmPath returns pwm base path
func (p *pwmPin) pwmPath() string {
func (p *PWMPin) pwmPath() string {
return "/sys/class/pwm/pwmchip" + p.Chip
}
// pwmExportPath returns export path
func (p *pwmPin) pwmExportPath() string {
func (p *PWMPin) pwmExportPath() string {
return p.pwmPath() + "/export"
}
// pwmUnexportPath returns unexport path
func (p *pwmPin) pwmUnexportPath() string {
func (p *PWMPin) pwmUnexportPath() string {
return p.pwmPath() + "/unexport"
}
// pwmDutyCyclePath returns duty_cycle path for specified pin
func (p *pwmPin) pwmDutyCyclePath() string {
func (p *PWMPin) pwmDutyCyclePath() string {
return p.pwmPath() + "/pwm" + p.pin + "/duty_cycle"
}
// pwmPeriodPath returns period path for specified pin
func (p *pwmPin) pwmPeriodPath() string {
func (p *PWMPin) pwmPeriodPath() string {
return p.pwmPath() + "/pwm" + p.pin + "/period"
}
// pwmEnablePath returns enable path for specified pin
func (p *pwmPin) pwmEnablePath() string {
func (p *PWMPin) pwmEnablePath() string {
return p.pwmPath() + "/pwm" + p.pin + "/enable"
}

View File

@ -19,7 +19,7 @@ func TestPwmPin(t *testing.T) {
SetFilesystem(fs)
pin := NewPwmPin(10)
pin := NewPWMPin(10)
gobottest.Assert(t, pin.pin, "10")
err := pin.Unexport()
@ -56,7 +56,7 @@ func TestPwmPinExportError(t *testing.T) {
SetFilesystem(fs)
pin := NewPwmPin(10)
pin := NewPWMPin(10)
pin.write = func(string, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
@ -75,7 +75,7 @@ func TestPwmPinUnxportError(t *testing.T) {
SetFilesystem(fs)
pin := NewPwmPin(10)
pin := NewPWMPin(10)
pin.write = func(string, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
@ -94,7 +94,7 @@ func TestPwmPinPeriodError(t *testing.T) {
SetFilesystem(fs)
pin := NewPwmPin(10)
pin := NewPWMPin(10)
pin.read = func(string) ([]byte, error) {
return nil, &os.PathError{Err: syscall.EBUSY}
}