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

core: Refactor C.H.I.P. platform for new Adaptor creation signature

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-09-25 20:19:19 +02:00
parent 11fded18d5
commit cd6d46d8a4
3 changed files with 29 additions and 27 deletions

View File

@ -42,14 +42,14 @@ import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/chip"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/drivers/gpio"
)
func main() {
gbot := gobot.NewGobot()
chipAdaptor := chip.NewChipAdaptor("chip")
button := gpio.NewButtonDriver(chipAdaptor, "button", "XIO-P0")
chipAdaptor := chip.NewAdaptor()
button := gpio.NewButtonDriver(chipAdaptor, "XIO-P0")
work := func() {
gobot.On(button.Event("push"), func(data interface{}) {

View File

@ -6,7 +6,7 @@ import (
"github.com/hybridgroup/gobot/sysfs"
)
type ChipAdaptor struct {
type Adaptor struct {
name string
digitalPins map[int]sysfs.DigitalPin
i2cDevice sysfs.I2cDevice
@ -23,25 +23,27 @@ var pins = map[string]int{
"XIO-P7": 415,
}
// NewChipAdaptor creates a ChipAdaptor with the specified name
func NewChipAdaptor(name string) *ChipAdaptor {
c := &ChipAdaptor{
name: name,
// NewAdaptor creates a C.H.I.P. Adaptor
func NewAdaptor() *Adaptor {
c := &Adaptor{
digitalPins: make(map[int]sysfs.DigitalPin),
}
return c
}
// Name returns the name of the ChipAdaptor
func (c *ChipAdaptor) Name() string { return c.name }
// Name returns the name of the Adaptor
func (c *Adaptor) Name() string { return c.name }
// SetName sets the name of the Adaptor
func (c *Adaptor) SetName(n string) { c.name = n }
// Connect initializes the board
func (c *ChipAdaptor) Connect() (errs []error) {
func (c *Adaptor) Connect() (errs []error) {
return
}
// Finalize closes connection to board and pins
func (c *ChipAdaptor) Finalize() (errs []error) {
func (c *Adaptor) Finalize() (errs []error) {
for _, pin := range c.digitalPins {
if pin != nil {
if err := pin.Unexport(); err != nil {
@ -57,7 +59,7 @@ func (c *ChipAdaptor) Finalize() (errs []error) {
return errs
}
func (c *ChipAdaptor) translatePin(pin string) (i int, err error) {
func (c *Adaptor) translatePin(pin string) (i int, err error) {
if val, ok := pins[pin]; ok {
i = val
} else {
@ -67,7 +69,7 @@ func (c *ChipAdaptor) translatePin(pin string) (i int, err error) {
}
// digitalPin returns matched digitalPin for specified values
func (c *ChipAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
func (c *Adaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
i, err := c.translatePin(pin)
if err != nil {
@ -90,7 +92,7 @@ func (c *ChipAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.Digital
// DigitalRead reads digital value from the specified pin.
// Valids pins are XIO-P0 through XIO-P7 (pins 13-20 on header 14).
func (c *ChipAdaptor) DigitalRead(pin string) (val int, err error) {
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.IN)
if err != nil {
return
@ -100,7 +102,7 @@ func (c *ChipAdaptor) DigitalRead(pin string) (val int, err error) {
// DigitalWrite writes digital value to the specified pin.
// Valids pins are XIO-P0 through XIO-P7 (pins 13-20 on header 14).
func (c *ChipAdaptor) DigitalWrite(pin string, val byte) (err error) {
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
sysfsPin, err := c.digitalPin(pin, sysfs.OUT)
if err != nil {
return err
@ -111,7 +113,7 @@ func (c *ChipAdaptor) DigitalWrite(pin string, val byte) (err error) {
// I2cStart starts an i2c device in specified address.
// This assumes that the bus used is /dev/i2c-1, which corresponds to
// pins labeled TWI1-SDA and TW1-SCK (pins 9 and 11 on header 13).
func (c *ChipAdaptor) I2cStart(address int) (err error) {
func (c *Adaptor) I2cStart(address int) (err error) {
if c.i2cDevice == nil {
c.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)
}
@ -119,7 +121,7 @@ func (c *ChipAdaptor) I2cStart(address int) (err error) {
}
// I2cWrite writes data to i2c device
func (c *ChipAdaptor) I2cWrite(address int, data []byte) (err error) {
func (c *Adaptor) I2cWrite(address int, data []byte) (err error) {
if err = c.i2cDevice.SetAddress(address); err != nil {
return
}
@ -128,7 +130,7 @@ func (c *ChipAdaptor) I2cWrite(address int, data []byte) (err error) {
}
// I2cRead returns value from i2c device using specified size
func (c *ChipAdaptor) I2cRead(address int, size int) (data []byte, err error) {
func (c *Adaptor) I2cRead(address int, size int) (data []byte, err error) {
if err = c.i2cDevice.SetAddress(address); err != nil {
return
}

View File

@ -5,18 +5,18 @@ import (
"testing"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/drivers/gpio"
"github.com/hybridgroup/gobot/drivers/i2c"
"github.com/hybridgroup/gobot/gobottest"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/sysfs"
)
var _ gobot.Adaptor = (*ChipAdaptor)(nil)
var _ gobot.Adaptor = (*Adaptor)(nil)
var _ gpio.DigitalReader = (*ChipAdaptor)(nil)
var _ gpio.DigitalWriter = (*ChipAdaptor)(nil)
var _ gpio.DigitalReader = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*Adaptor)(nil)
var _ i2c.I2c = (*ChipAdaptor)(nil)
var _ i2c.I2c = (*Adaptor)(nil)
type NullReadWriteCloser struct {
contents []byte
@ -44,8 +44,8 @@ func (n *NullReadWriteCloser) Close() error {
return closeErr
}
func initTestChipAdaptor() *ChipAdaptor {
a := NewChipAdaptor("myAdaptor")
func initTestChipAdaptor() *Adaptor {
a := NewAdaptor()
a.Connect()
return a
}