1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +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"
"github.com/hybridgroup/gobot/platforms/chip" "github.com/hybridgroup/gobot/platforms/chip"
"github.com/hybridgroup/gobot/platforms/gpio" "github.com/hybridgroup/gobot/drivers/gpio"
) )
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
chipAdaptor := chip.NewChipAdaptor("chip") chipAdaptor := chip.NewAdaptor()
button := gpio.NewButtonDriver(chipAdaptor, "button", "XIO-P0") button := gpio.NewButtonDriver(chipAdaptor, "XIO-P0")
work := func() { work := func() {
gobot.On(button.Event("push"), func(data interface{}) { gobot.On(button.Event("push"), func(data interface{}) {

View File

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

View File

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