Skip to content

Commit

Permalink
Add raspi pwm pi-blaster support
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 30, 2015
1 parent 10b2de6 commit 93a4c45
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
76 changes: 72 additions & 4 deletions platforms/raspi/raspi_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package raspi

import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"

Expand All @@ -29,6 +31,7 @@ type RaspiAdaptor struct {
revision string
i2cLocation string
digitalPins map[int]sysfs.DigitalPin
pwmPins []int
i2cDevice io.ReadWriteCloser
}

Expand Down Expand Up @@ -124,6 +127,7 @@ func NewRaspiAdaptor(name string) *RaspiAdaptor {
r := &RaspiAdaptor{
name: name,
digitalPins: make(map[int]sysfs.DigitalPin),
pwmPins: []int{},
}
content, _ := readFile()
for _, v := range strings.Split(string(content), "\n") {
Expand Down Expand Up @@ -161,6 +165,11 @@ func (r *RaspiAdaptor) Finalize() (errs []error) {
}
}
}
for _, pin := range r.pwmPins {
if err := r.piBlaster(fmt.Sprintf("%v=release\n", pin)); err != nil {
errs = append(errs, err)
}
}
if r.i2cDevice != nil {
if err := r.i2cDevice.Close(); err != nil {
errs = append(errs, err)
Expand All @@ -169,10 +178,7 @@ func (r *RaspiAdaptor) Finalize() (errs []error) {
return errs
}

// digitalPin returns matched digitalPin for specified values
func (r *RaspiAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
var i int

func (r *RaspiAdaptor) translatePin(pin string) (i int, err error) {
if val, ok := pins[pin][r.revision]; ok {
i = val
} else if val, ok := pins[pin]["*"]; ok {
Expand All @@ -181,6 +187,37 @@ func (r *RaspiAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.Digita
err = errors.New("Not a valid pin")
return
}
return
}

func (r *RaspiAdaptor) pwmPin(pin string) (i int, err error) {
i, err = r.translatePin(pin)
if err != nil {
return
}

newPin := true
for _, pin := range r.pwmPins {
if i == pin {
newPin = false
return
}
}

if newPin {
r.pwmPins = append(r.pwmPins, i)
}

return
}

// digitalPin returns matched digitalPin for specified values
func (r *RaspiAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
i, err := r.translatePin(pin)

if err != nil {
return
}

if r.digitalPins[i] == nil {
r.digitalPins[i] = sysfs.NewDigitalPin(i)
Expand Down Expand Up @@ -232,3 +269,34 @@ func (r *RaspiAdaptor) I2cRead(size uint) (data []byte, err error) {
_, err = r.i2cDevice.Read(data)
return
}

func (r *RaspiAdaptor) PwmWrite(pin string, val byte) (err error) {
sysfsPin, err := r.pwmPin(pin)
if err != nil {
return err
}
return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, gobot.FromScale(float64(val), 0, 255)))
}

func (r *RaspiAdaptor) ServoWrite(pin string, angle byte) (err error) {
sysfsPin, err := r.pwmPin(pin)
if err != nil {
return err
}

val := (gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), 0, 200) / 1000.0) + 0.05

return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, val))
}

func (r *RaspiAdaptor) 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
}
22 changes: 22 additions & 0 deletions platforms/raspi/raspi_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package raspi

import (
"strings"
"testing"

"github.com/hybridgroup/gobot"
Expand Down Expand Up @@ -73,14 +74,35 @@ func TestRaspiAdaptorFinalize(t *testing.T) {
fs := sysfs.NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/dev/pi-blaster",
})

sysfs.SetFilesystem(fs)
a.DigitalWrite("3", 1)
a.PwmWrite("7", 255)
a.i2cDevice = new(NullReadWriteCloser)
gobot.Assert(t, len(a.Finalize()), 0)
}

func TestRaspiAdaptorDigitalPWM(t *testing.T) {
a := initTestRaspiAdaptor()

gobot.Assert(t, a.PwmWrite("7", 4), nil)

fs := sysfs.NewMockFilesystem([]string{
"/dev/pi-blaster",
})
sysfs.SetFilesystem(fs)

gobot.Assert(t, a.PwmWrite("7", 255), nil)

gobot.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "4=1")

gobot.Assert(t, a.ServoWrite("11", 255), nil)

gobot.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "17=0.25")
}

func TestRaspiAdaptorDigitalIO(t *testing.T) {
a := initTestRaspiAdaptor()
fs := sysfs.NewMockFilesystem([]string{
Expand Down

0 comments on commit 93a4c45

Please sign in to comment.