raspi: add implementation for PWMPinner interface that wraps pi blaster
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
c9ec619219
commit
efb9f7647d
|
@ -0,0 +1,93 @@
|
||||||
|
package raspi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gobot.io/x/gobot"
|
||||||
|
"gobot.io/x/gobot/sysfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const piBlasterPeriod = 10000000
|
||||||
|
|
||||||
|
// PWMPin is the Raspberry Pi implementation of the PWMPinner interface.
|
||||||
|
// It uses Pi Blaster.
|
||||||
|
type PWMPin struct {
|
||||||
|
pin string
|
||||||
|
dc uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPwmPin returns a new PWMPin
|
||||||
|
func NewPWMPin(pin string) *PWMPin {
|
||||||
|
return &PWMPin{
|
||||||
|
pin: pin}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export exports the pin for use by the Raspberry Pi
|
||||||
|
func (p *PWMPin) Export() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unexport unexports the pin and releases the pin from the operating system
|
||||||
|
func (p *PWMPin) Unexport() error {
|
||||||
|
return p.piBlaster(fmt.Sprintf("release %v\n", p.pin))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable enables/disables the PWM pin
|
||||||
|
func (p *PWMPin) Enable(e bool) (err error) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Polarity returns the polarity either normal or inverted
|
||||||
|
func (p *PWMPin) Polarity() (polarity string, err error) {
|
||||||
|
return "normal", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InvertPolarity does not do anything when using PiBlaster
|
||||||
|
func (p *PWMPin) InvertPolarity(invert bool) (err error) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Period returns the current PWM period for pin
|
||||||
|
func (p *PWMPin) Period() (period uint32, err error) {
|
||||||
|
return piBlasterPeriod, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPeriod does not do anything when using PiBlaster
|
||||||
|
func (p *PWMPin) SetPeriod(period uint32) (err error) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DutyCycle returns the duty cycle for the pin
|
||||||
|
func (p *PWMPin) DutyCycle() (duty uint32, err error) {
|
||||||
|
return p.dc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDutyCycle writes the duty cycle to the pin
|
||||||
|
func (p *PWMPin) SetDutyCycle(duty uint32) (err error) {
|
||||||
|
if duty > piBlasterPeriod {
|
||||||
|
return errors.New("Duty cycle exceeds period.")
|
||||||
|
}
|
||||||
|
p.dc = duty
|
||||||
|
|
||||||
|
val := gobot.FromScale(float64(p.dc), 0, piBlasterPeriod)
|
||||||
|
|
||||||
|
// never go below minimum allowed duty for pi blaster
|
||||||
|
if val < 0.05 {
|
||||||
|
val = 0.05
|
||||||
|
}
|
||||||
|
return p.piBlaster(fmt.Sprintf("%v=%v\n", p.pin, val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PWMPin) piBlaster(data string) (err error) {
|
||||||
|
fi, err := sysfs.OpenFile("/dev/pi-blaster", os.O_WRONLY|os.O_APPEND, 0644)
|
||||||
|
defer fi.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fi.WriteString(data)
|
||||||
|
return
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package raspi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gobot.io/x/gobot/gobottest"
|
||||||
|
"gobot.io/x/gobot/sysfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ sysfs.PWMPinner = (*PWMPin)(nil)
|
||||||
|
|
||||||
|
func TestPwmPin(t *testing.T) {
|
||||||
|
pin := NewPWMPin("1")
|
||||||
|
gobottest.Assert(t, pin.Export(), nil)
|
||||||
|
gobottest.Assert(t, pin.Enable(true), nil)
|
||||||
|
val, _ := pin.Polarity()
|
||||||
|
gobottest.Assert(t, val, "normal")
|
||||||
|
gobottest.Assert(t, pin.InvertPolarity(true), nil)
|
||||||
|
val, _ = pin.Polarity()
|
||||||
|
gobottest.Assert(t, val, "normal")
|
||||||
|
|
||||||
|
period, _ := pin.Period()
|
||||||
|
gobottest.Assert(t, period, uint32(10000000))
|
||||||
|
gobottest.Assert(t, pin.SetPeriod(1000), nil)
|
||||||
|
period, _ = pin.Period()
|
||||||
|
gobottest.Assert(t, period, uint32(10000000))
|
||||||
|
|
||||||
|
dc, _ := pin.DutyCycle()
|
||||||
|
gobottest.Assert(t, dc, uint32(0))
|
||||||
|
|
||||||
|
// call currently fails in test
|
||||||
|
gobottest.Refute(t, pin.SetDutyCycle(10000), nil)
|
||||||
|
dc, _ = pin.DutyCycle()
|
||||||
|
gobottest.Assert(t, dc, uint32(10000))
|
||||||
|
|
||||||
|
// call currently fails in test
|
||||||
|
gobottest.Refute(t, pin.Unexport(), nil)
|
||||||
|
}
|
|
@ -23,7 +23,7 @@ type Adaptor struct {
|
||||||
name string
|
name string
|
||||||
revision string
|
revision string
|
||||||
digitalPins map[int]*sysfs.DigitalPin
|
digitalPins map[int]*sysfs.DigitalPin
|
||||||
pwmPins []int
|
pwmPins map[int]*PWMPin
|
||||||
i2cDefaultBus int
|
i2cDefaultBus int
|
||||||
i2cBuses [2]sysfs.I2cDevice
|
i2cBuses [2]sysfs.I2cDevice
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func NewAdaptor() *Adaptor {
|
||||||
r := &Adaptor{
|
r := &Adaptor{
|
||||||
name: gobot.DefaultName("RaspberryPi"),
|
name: gobot.DefaultName("RaspberryPi"),
|
||||||
digitalPins: make(map[int]*sysfs.DigitalPin),
|
digitalPins: make(map[int]*sysfs.DigitalPin),
|
||||||
pwmPins: []int{},
|
pwmPins: make(map[int]*PWMPin),
|
||||||
}
|
}
|
||||||
content, _ := readFile()
|
content, _ := readFile()
|
||||||
for _, v := range strings.Split(string(content), "\n") {
|
for _, v := range strings.Split(string(content), "\n") {
|
||||||
|
@ -77,8 +77,10 @@ func (r *Adaptor) Finalize() (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, pin := range r.pwmPins {
|
for _, pin := range r.pwmPins {
|
||||||
if perr := r.piBlaster(fmt.Sprintf("release %v\n", pin)); err != nil {
|
if pin != nil {
|
||||||
err = multierror.Append(err, perr)
|
if perr := pin.Unexport(); err != nil {
|
||||||
|
err = multierror.Append(err, perr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, bus := range r.i2cBuses {
|
for _, bus := range r.i2cBuses {
|
||||||
|
@ -150,23 +152,24 @@ func (r *Adaptor) GetDefaultBus() int {
|
||||||
|
|
||||||
// PwmWrite writes a PWM signal to the specified pin
|
// PwmWrite writes a PWM signal to the specified pin
|
||||||
func (r *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
func (r *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
||||||
sysfsPin, err := r.pwmPin(pin)
|
sysfsPin, err := r.PWMPin(pin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, gobot.FromScale(float64(val), 0, 255)))
|
|
||||||
|
duty := uint32(gobot.FromScale(float64(val), 0, 255) * piBlasterPeriod)
|
||||||
|
return sysfsPin.SetDutyCycle(duty)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServoWrite writes a servo signal to the specified pin
|
// ServoWrite writes a servo signal to the specified pin
|
||||||
func (r *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
func (r *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
||||||
sysfsPin, err := r.pwmPin(pin)
|
sysfsPin, err := r.PWMPin(pin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
val := (gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), 0, 200) / 1000.0) + 0.05
|
duty := uint32(gobot.FromScale(float64(angle), 0, 180) * piBlasterPeriod)
|
||||||
|
return sysfsPin.SetDutyCycle(duty)
|
||||||
return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, val))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Adaptor) translatePin(pin string) (i int, err error) {
|
func (r *Adaptor) translatePin(pin string) (i int, err error) {
|
||||||
|
@ -181,25 +184,20 @@ func (r *Adaptor) translatePin(pin string) (i int, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Adaptor) pwmPin(pin string) (i int, err error) {
|
func (r *Adaptor) PWMPin(pin string) (raspiPWMPin *PWMPin, err error) {
|
||||||
i, err = r.translatePin(pin)
|
i, err := r.translatePin(pin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newPin := true
|
if r.pwmPins[i] == nil {
|
||||||
for _, pin := range r.pwmPins {
|
r.pwmPins[i] = NewPWMPin(strconv.Itoa(i))
|
||||||
if i == pin {
|
if err = r.pwmPins[i].Export(); err != nil {
|
||||||
newPin = false
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if newPin {
|
return r.pwmPins[i], nil
|
||||||
r.pwmPins = append(r.pwmPins, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Adaptor) piBlaster(data string) (err error) {
|
func (r *Adaptor) piBlaster(data string) (err error) {
|
||||||
|
|
|
@ -110,9 +110,9 @@ func TestAdaptorDigitalPWM(t *testing.T) {
|
||||||
|
|
||||||
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "4=1")
|
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "4=1")
|
||||||
|
|
||||||
gobottest.Assert(t, a.ServoWrite("11", 255), nil)
|
gobottest.Assert(t, a.ServoWrite("11", 90), nil)
|
||||||
|
|
||||||
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "17=0.25")
|
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "17=0.5")
|
||||||
|
|
||||||
gobottest.Assert(t, a.PwmWrite("notexist", 1), errors.New("Not a valid pin"))
|
gobottest.Assert(t, a.PwmWrite("notexist", 1), errors.New("Not a valid pin"))
|
||||||
gobottest.Assert(t, a.ServoWrite("notexist", 1), errors.New("Not a valid pin"))
|
gobottest.Assert(t, a.ServoWrite("notexist", 1), errors.New("Not a valid pin"))
|
||||||
|
|
Loading…
Reference in New Issue