mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
core: Refactor DigiSpark platform for new Adaptor creation signature
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
cd6d46d8a4
commit
12a5cec209
@ -40,14 +40,14 @@ import (
|
|||||||
|
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot/platforms/digispark"
|
"github.com/hybridgroup/gobot/platforms/digispark"
|
||||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
gbot := gobot.NewGobot()
|
gbot := gobot.NewGobot()
|
||||||
|
|
||||||
digisparkAdaptor := digispark.NewDigisparkAdaptor("Digispark")
|
digisparkAdaptor := digispark.NewAdaptor()
|
||||||
led := gpio.NewLedDriver(digisparkAdaptor, "led", "0")
|
led := gpio.NewLedDriver(digisparkAdaptor, "0")
|
||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
gobot.Every(1*time.Second, func() {
|
gobot.Every(1*time.Second, func() {
|
||||||
|
@ -8,20 +8,19 @@ import (
|
|||||||
// ErrConnection is the error resulting of a connection error with the digispark
|
// ErrConnection is the error resulting of a connection error with the digispark
|
||||||
var ErrConnection = errors.New("connection error")
|
var ErrConnection = errors.New("connection error")
|
||||||
|
|
||||||
// DigisparkAdaptor is the Gobot Adaptor for the Digispark
|
// Adaptor is the Gobot Adaptor for the Digispark
|
||||||
type DigisparkAdaptor struct {
|
type Adaptor struct {
|
||||||
name string
|
name string
|
||||||
littleWire lw
|
littleWire lw
|
||||||
servo bool
|
servo bool
|
||||||
pwm bool
|
pwm bool
|
||||||
connect func(*DigisparkAdaptor) (err error)
|
connect func(*Adaptor) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDigisparkAdaptor returns a new DigisparkAdaptor with specified name
|
// NewAdaptor returns a new Digispark Adaptor
|
||||||
func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
func NewAdaptor() *Adaptor {
|
||||||
return &DigisparkAdaptor{
|
return &Adaptor{
|
||||||
name: name,
|
connect: func(d *Adaptor) (err error) {
|
||||||
connect: func(d *DigisparkAdaptor) (err error) {
|
|
||||||
d.littleWire = littleWireConnect()
|
d.littleWire = littleWireConnect()
|
||||||
if d.littleWire.(*littleWire).lwHandle == nil {
|
if d.littleWire.(*littleWire).lwHandle == nil {
|
||||||
return ErrConnection
|
return ErrConnection
|
||||||
@ -31,11 +30,14 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the DigisparkAdaptors name
|
// Name returns the Digispark Adaptors name
|
||||||
func (d *DigisparkAdaptor) Name() string { return d.name }
|
func (d *Adaptor) Name() string { return d.name }
|
||||||
|
|
||||||
|
// SetName sets the Digispark Adaptors name
|
||||||
|
func (d *Adaptor) SetName(n string) { d.name = n }
|
||||||
|
|
||||||
// Connect starts a connection to the digispark
|
// Connect starts a connection to the digispark
|
||||||
func (d *DigisparkAdaptor) Connect() (errs []error) {
|
func (d *Adaptor) Connect() (errs []error) {
|
||||||
if err := d.connect(d); err != nil {
|
if err := d.connect(d); err != nil {
|
||||||
return []error{err}
|
return []error{err}
|
||||||
}
|
}
|
||||||
@ -43,10 +45,10 @@ func (d *DigisparkAdaptor) Connect() (errs []error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Finalize implements the Adaptor interface
|
// Finalize implements the Adaptor interface
|
||||||
func (d *DigisparkAdaptor) Finalize() (errs []error) { return }
|
func (d *Adaptor) Finalize() (errs []error) { return }
|
||||||
|
|
||||||
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
|
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
|
||||||
func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) {
|
func (d *Adaptor) DigitalWrite(pin string, level byte) (err error) {
|
||||||
p, err := strconv.Atoi(pin)
|
p, err := strconv.Atoi(pin)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -61,7 +63,7 @@ func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PwmWrite writes the 0-254 value to the specified pin
|
// PwmWrite writes the 0-254 value to the specified pin
|
||||||
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
|
func (d *Adaptor) PwmWrite(pin string, value byte) (err error) {
|
||||||
if !d.pwm {
|
if !d.pwm {
|
||||||
if err = d.littleWire.pwmInit(); err != nil {
|
if err = d.littleWire.pwmInit(); err != nil {
|
||||||
return
|
return
|
||||||
@ -77,7 +79,7 @@ func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ServoWrite writes the 0-180 degree val to the specified pin.
|
// ServoWrite writes the 0-180 degree val to the specified pin.
|
||||||
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) (err error) {
|
func (d *Adaptor) ServoWrite(pin string, angle uint8) (err error) {
|
||||||
if !d.servo {
|
if !d.servo {
|
||||||
if err = d.littleWire.servoInit(); err != nil {
|
if err = d.littleWire.servoInit(); err != nil {
|
||||||
return
|
return
|
||||||
|
@ -5,15 +5,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
|
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||||
"github.com/hybridgroup/gobot/gobottest"
|
"github.com/hybridgroup/gobot/gobottest"
|
||||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.Adaptor = (*DigisparkAdaptor)(nil)
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
||||||
|
|
||||||
var _ gpio.DigitalWriter = (*DigisparkAdaptor)(nil)
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
||||||
var _ gpio.PwmWriter = (*DigisparkAdaptor)(nil)
|
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
||||||
var _ gpio.ServoWriter = (*DigisparkAdaptor)(nil)
|
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
||||||
|
|
||||||
type mock struct {
|
type mock struct {
|
||||||
locationA uint8
|
locationA uint8
|
||||||
@ -61,35 +61,30 @@ var errorFunc = func() error { return nil }
|
|||||||
|
|
||||||
func (l *mock) error() error { return errorFunc() }
|
func (l *mock) error() error { return errorFunc() }
|
||||||
|
|
||||||
func initTestDigisparkAdaptor() *DigisparkAdaptor {
|
func initTestAdaptor() *Adaptor {
|
||||||
a := NewDigisparkAdaptor("bot")
|
a := NewAdaptor()
|
||||||
a.connect = func(a *DigisparkAdaptor) (err error) { return nil }
|
a.connect = func(a *Adaptor) (err error) { return nil }
|
||||||
a.littleWire = new(mock)
|
a.littleWire = new(mock)
|
||||||
errorFunc = func() error { return nil }
|
errorFunc = func() error { return nil }
|
||||||
pwmInitErrorFunc = func() error { return nil }
|
pwmInitErrorFunc = func() error { return nil }
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDigisparkAdaptor(t *testing.T) {
|
func TestAdaptorConnect(t *testing.T) {
|
||||||
a := NewDigisparkAdaptor("bot")
|
a := NewAdaptor()
|
||||||
gobottest.Assert(t, a.Name(), "bot")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDigisparkAdaptorConnect(t *testing.T) {
|
|
||||||
a := NewDigisparkAdaptor("bot")
|
|
||||||
gobottest.Assert(t, a.Connect()[0], ErrConnection)
|
gobottest.Assert(t, a.Connect()[0], ErrConnection)
|
||||||
|
|
||||||
a = initTestDigisparkAdaptor()
|
a = initTestAdaptor()
|
||||||
gobottest.Assert(t, len(a.Connect()), 0)
|
gobottest.Assert(t, len(a.Connect()), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDigisparkAdaptorFinalize(t *testing.T) {
|
func TestAdaptorFinalize(t *testing.T) {
|
||||||
a := initTestDigisparkAdaptor()
|
a := initTestAdaptor()
|
||||||
gobottest.Assert(t, len(a.Finalize()), 0)
|
gobottest.Assert(t, len(a.Finalize()), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
|
func TestAdaptorDigitalWrite(t *testing.T) {
|
||||||
a := initTestDigisparkAdaptor()
|
a := initTestAdaptor()
|
||||||
err := a.DigitalWrite("0", uint8(1))
|
err := a.DigitalWrite("0", uint8(1))
|
||||||
gobottest.Assert(t, err, nil)
|
gobottest.Assert(t, err, nil)
|
||||||
gobottest.Assert(t, a.littleWire.(*mock).pin, uint8(0))
|
gobottest.Assert(t, a.littleWire.(*mock).pin, uint8(0))
|
||||||
@ -103,32 +98,32 @@ func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
|
|||||||
gobottest.Assert(t, err, errors.New("pin mode error"))
|
gobottest.Assert(t, err, errors.New("pin mode error"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDigisparkAdaptorServoWrite(t *testing.T) {
|
func TestAdaptorServoWrite(t *testing.T) {
|
||||||
a := initTestDigisparkAdaptor()
|
a := initTestAdaptor()
|
||||||
err := a.ServoWrite("2", uint8(80))
|
err := a.ServoWrite("2", uint8(80))
|
||||||
gobottest.Assert(t, err, nil)
|
gobottest.Assert(t, err, nil)
|
||||||
gobottest.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
|
gobottest.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
|
||||||
gobottest.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
|
gobottest.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
|
||||||
|
|
||||||
a = initTestDigisparkAdaptor()
|
a = initTestAdaptor()
|
||||||
errorFunc = func() error { return errors.New("servo error") }
|
errorFunc = func() error { return errors.New("servo error") }
|
||||||
err = a.ServoWrite("2", uint8(80))
|
err = a.ServoWrite("2", uint8(80))
|
||||||
gobottest.Assert(t, err, errors.New("servo error"))
|
gobottest.Assert(t, err, errors.New("servo error"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDigisparkAdaptorPwmWrite(t *testing.T) {
|
func TestAdaptorPwmWrite(t *testing.T) {
|
||||||
a := initTestDigisparkAdaptor()
|
a := initTestAdaptor()
|
||||||
err := a.PwmWrite("1", uint8(100))
|
err := a.PwmWrite("1", uint8(100))
|
||||||
gobottest.Assert(t, err, nil)
|
gobottest.Assert(t, err, nil)
|
||||||
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
|
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
|
||||||
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
|
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
|
||||||
|
|
||||||
a = initTestDigisparkAdaptor()
|
a = initTestAdaptor()
|
||||||
pwmInitErrorFunc = func() error { return errors.New("pwminit error") }
|
pwmInitErrorFunc = func() error { return errors.New("pwminit error") }
|
||||||
err = a.PwmWrite("1", uint8(100))
|
err = a.PwmWrite("1", uint8(100))
|
||||||
gobottest.Assert(t, err, errors.New("pwminit error"))
|
gobottest.Assert(t, err, errors.New("pwminit error"))
|
||||||
|
|
||||||
a = initTestDigisparkAdaptor()
|
a = initTestAdaptor()
|
||||||
errorFunc = func() error { return errors.New("pwm error") }
|
errorFunc = func() error { return errors.New("pwm error") }
|
||||||
err = a.PwmWrite("1", uint8(100))
|
err = a.PwmWrite("1", uint8(100))
|
||||||
gobottest.Assert(t, err, errors.New("pwm error"))
|
gobottest.Assert(t, err, errors.New("pwm error"))
|
||||||
|
@ -17,14 +17,14 @@ Example:
|
|||||||
|
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot/platforms/digispark"
|
"github.com/hybridgroup/gobot/platforms/digispark"
|
||||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
gbot := gobot.NewGobot()
|
gbot := gobot.NewGobot()
|
||||||
|
|
||||||
digisparkAdaptor := digispark.NewDigisparkAdaptor("Digispark")
|
digisparkAdaptor := digispark.NewAdaptor()
|
||||||
led := gpio.NewLedDriver(digisparkAdaptor, "led", "0")
|
led := gpio.NewLedDriver(digisparkAdaptor, "0")
|
||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
gobot.Every(1*time.Second, func() {
|
gobot.Every(1*time.Second, func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user