Skip to content

Commit

Permalink
tinkerboard: initial PWM implementation
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 0843ca3 commit 927293c
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 81 deletions.
160 changes: 118 additions & 42 deletions platforms/tinkerboard/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ package tinkerboard
import (
"errors"
"fmt"
"strconv"

multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/sysfs"
)

type sysfsPin struct {
pin int
pwmPin int
}

// Adaptor represents a Gobot Adaptor for the ASUS Tinker Board
type Adaptor struct {
name string
pinmap map[string]sysfsPin
digitalPins map[int]sysfs.DigitalPin
pinMap map[string]int
pwmPins map[int]*sysfs.PWMPin
i2cBuses [2]sysfs.I2cDevice
// pwm *pwmControl
}

// NewAdaptor creates a Tinkerboard Adaptor
Expand All @@ -38,24 +44,29 @@ func (c *Adaptor) SetName(n string) { c.name = n }

// Connect initializes the board
func (c *Adaptor) Connect() (err error) {
c.pwmPins = make(map[int]*sysfs.PWMPin)
return nil
}

// Finalize closes connection to board and pins
func (c *Adaptor) Finalize() (err error) {
// if c.pwm != nil {
// if e := c.closePWM(); e != nil {
// err = multierror.Append(err, e)
// }
// c.pwm = nil
// }
for _, pin := range c.digitalPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, pin := range c.pwmPins {
if pin != nil {
if errs := pin.Enable(false); errs != nil {
err = multierror.Append(err, errs)
}
if errs := pin.Unexport(); errs != nil {
err = multierror.Append(err, errs)
}
}
}
for _, bus := range c.i2cBuses {
if bus != nil {
if e := bus.Close(); e != nil {
Expand All @@ -66,13 +77,85 @@ func (c *Adaptor) Finalize() (err error) {
return
}

// DigitalRead reads digital value from the specified pin.
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.IN)
if err != nil {
return
}
return sysfsPin.Read()
}

// DigitalWrite writes digital value to the specified pin.
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.OUT)
if err != nil {
return err
}
return sysfsPin.Write(int(val))
}

// PwmWrite writes a PWM signal to the specified pin
func (c *Adaptor) PwmWrite(pin string, val byte) (err error) {
pwmPin, err := c.pwmPin(pin)
if err != nil {
return
}
p, err := pwmPin.Period()
if err != nil {
return err
}
period, err := strconv.Atoi(p)
if err != nil {
return err
}
duty := gobot.FromScale(float64(val), 0, 255.0)
return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
}

// TODO: take into account the actual period setting, not just assume default
const pwmPeriod = 10000000

// ServoWrite writes a servo signal to the specified pin
func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) {
pwmPin, err := c.pwmPin(pin)
if err != nil {
return
}

// 0.5 ms => -90
// 1.5 ms => 0
// 2.0 ms => 90
const minDuty = 100 * 0.0005 * pwmPeriod
const maxDuty = 100 * 0.0020 * pwmPeriod
duty := uint32(gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), minDuty, maxDuty))
return pwmPin.SetDutyCycle(duty)
}

// GetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
if (bus < 0) || (bus > 1) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
if c.i2cBuses[bus] == nil {
c.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
}
return i2c.NewConnection(c.i2cBuses[bus], address), err
}

// GetDefaultBus returns the default i2c bus for this platform
func (c *Adaptor) GetDefaultBus() int {
return 1
}

func (c *Adaptor) setPins() {
c.pinMap = fixedPins
c.pinmap = fixedPins
}

func (c *Adaptor) translatePin(pin string) (i int, err error) {
if val, ok := c.pinMap[pin]; ok {
i = val
if val, ok := c.pinmap[pin]; ok {
i = val.pin
} else {
err = errors.New("Not a valid pin")
}
Expand Down Expand Up @@ -101,37 +184,30 @@ func (c *Adaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin,
return c.digitalPins[i], nil
}

// DigitalRead reads digital value from the specified pin.
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.IN)
if err != nil {
return
}
return sysfsPin.Read()
}

// DigitalWrite writes digital value to the specified pin.
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.OUT)
if err != nil {
return err
}
return sysfsPin.Write(int(val))
}
// pwmPin returns matched pwmPin for specified pin number
func (c *Adaptor) pwmPin(pin string) (sysfsPin *sysfs.PWMPin, err error) {
sysPin := c.pinmap[pin]
if sysPin.pwmPin != -1 {
if c.pwmPins[sysPin.pwmPin] == nil {
newPin := sysfs.NewPWMPin(sysPin.pwmPin)
if err = newPin.Export(); err != nil {
return
}
if err = newPin.Enable(true); err != nil {
return
}
if err = newPin.SetPeriod(10000000); err != nil {
return
}
if err = newPin.SetPolarityInverted(false); err != nil {
return
}
c.pwmPins[sysPin.pwmPin] = newPin
}

// GetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
if (bus < 0) || (bus > 1) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
if c.i2cBuses[bus] == nil {
c.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
sysfsPin = c.pwmPins[sysPin.pwmPin]
return
}
return i2c.NewConnection(c.i2cBuses[bus], address), err
}

// GetDefaultBus returns the default i2c bus for this platform
func (c *Adaptor) GetDefaultBus() int {
return 1
err = errors.New("Not a PWM pin")
return
}
44 changes: 44 additions & 0 deletions platforms/tinkerboard/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
var _ gobot.Adaptor = (*Adaptor)(nil)
var _ gpio.DigitalReader = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*Adaptor)(nil)
var _ gpio.PwmWriter = (*Adaptor)(nil)
var _ gpio.ServoWriter = (*Adaptor)(nil)
var _ i2c.Connector = (*Adaptor)(nil)

func initTestTinkerboardAdaptor() *Adaptor {
Expand Down Expand Up @@ -73,6 +75,48 @@ func TestTinkerboardAdaptorI2c(t *testing.T) {
gobottest.Assert(t, a.Finalize(), nil)
}

func TestTinkerboardAdaptorInvalidPWMPin(t *testing.T) {
a := initTestTinkerboardAdaptor()

err := a.PwmWrite("666", 42)
gobottest.Refute(t, err, nil)

err = a.ServoWrite("666", 120)
gobottest.Refute(t, err, nil)
}

func TestTinkerboardAdaptorPWM(t *testing.T) {
a := initTestTinkerboardAdaptor()
fs := sysfs.NewMockFilesystem([]string{
"/sys/class/pwm/pwmchip0/export",
"/sys/class/pwm/pwmchip0/unexport",
"/sys/class/pwm/pwmchip0/pwm0/enable",
"/sys/class/pwm/pwmchip0/pwm0/period",
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
"/sys/class/pwm/pwmchip0/pwm0/polarity",
})
sysfs.SetFilesystem(fs)

err := a.PwmWrite("PWM0", 100)
gobottest.Assert(t, err, nil)

gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "0")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/enable"].Contents, "1")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "3921568")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")

err = a.ServoWrite("PWM0", 0)
gobottest.Assert(t, err, nil)

gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "500000")

err = a.ServoWrite("PWM0", 180)
gobottest.Assert(t, err, nil)

gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "2000000")
gobottest.Assert(t, a.Finalize(), nil)
}

func TestTinkerboardDefaultBus(t *testing.T) {
a := initTestTinkerboardAdaptor()
gobottest.Assert(t, a.GetDefaultBus(), 1)
Expand Down
Loading

0 comments on commit 927293c

Please sign in to comment.