mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +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/platforms/digispark"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
digisparkAdaptor := digispark.NewDigisparkAdaptor("Digispark")
|
||||
led := gpio.NewLedDriver(digisparkAdaptor, "led", "0")
|
||||
digisparkAdaptor := digispark.NewAdaptor()
|
||||
led := gpio.NewLedDriver(digisparkAdaptor, "0")
|
||||
|
||||
work := func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
|
@ -8,20 +8,19 @@ import (
|
||||
// ErrConnection is the error resulting of a connection error with the digispark
|
||||
var ErrConnection = errors.New("connection error")
|
||||
|
||||
// DigisparkAdaptor is the Gobot Adaptor for the Digispark
|
||||
type DigisparkAdaptor struct {
|
||||
// Adaptor is the Gobot Adaptor for the Digispark
|
||||
type Adaptor struct {
|
||||
name string
|
||||
littleWire lw
|
||||
servo bool
|
||||
pwm bool
|
||||
connect func(*DigisparkAdaptor) (err error)
|
||||
connect func(*Adaptor) (err error)
|
||||
}
|
||||
|
||||
// NewDigisparkAdaptor returns a new DigisparkAdaptor with specified name
|
||||
func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
||||
return &DigisparkAdaptor{
|
||||
name: name,
|
||||
connect: func(d *DigisparkAdaptor) (err error) {
|
||||
// NewAdaptor returns a new Digispark Adaptor
|
||||
func NewAdaptor() *Adaptor {
|
||||
return &Adaptor{
|
||||
connect: func(d *Adaptor) (err error) {
|
||||
d.littleWire = littleWireConnect()
|
||||
if d.littleWire.(*littleWire).lwHandle == nil {
|
||||
return ErrConnection
|
||||
@ -31,11 +30,14 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
||||
}
|
||||
}
|
||||
|
||||
// Name returns the DigisparkAdaptors name
|
||||
func (d *DigisparkAdaptor) Name() string { return d.name }
|
||||
// Name returns the Digispark Adaptors 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
|
||||
func (d *DigisparkAdaptor) Connect() (errs []error) {
|
||||
func (d *Adaptor) Connect() (errs []error) {
|
||||
if err := d.connect(d); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
@ -43,10 +45,10 @@ func (d *DigisparkAdaptor) Connect() (errs []error) {
|
||||
}
|
||||
|
||||
// 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.
|
||||
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)
|
||||
|
||||
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
|
||||
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
|
||||
func (d *Adaptor) PwmWrite(pin string, value byte) (err error) {
|
||||
if !d.pwm {
|
||||
if err = d.littleWire.pwmInit(); err != nil {
|
||||
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.
|
||||
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) (err error) {
|
||||
func (d *Adaptor) ServoWrite(pin string, angle uint8) (err error) {
|
||||
if !d.servo {
|
||||
if err = d.littleWire.servoInit(); err != nil {
|
||||
return
|
||||
|
@ -5,15 +5,15 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||
"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.PwmWriter = (*DigisparkAdaptor)(nil)
|
||||
var _ gpio.ServoWriter = (*DigisparkAdaptor)(nil)
|
||||
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
||||
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
||||
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
||||
|
||||
type mock struct {
|
||||
locationA uint8
|
||||
@ -61,35 +61,30 @@ var errorFunc = func() error { return nil }
|
||||
|
||||
func (l *mock) error() error { return errorFunc() }
|
||||
|
||||
func initTestDigisparkAdaptor() *DigisparkAdaptor {
|
||||
a := NewDigisparkAdaptor("bot")
|
||||
a.connect = func(a *DigisparkAdaptor) (err error) { return nil }
|
||||
func initTestAdaptor() *Adaptor {
|
||||
a := NewAdaptor()
|
||||
a.connect = func(a *Adaptor) (err error) { return nil }
|
||||
a.littleWire = new(mock)
|
||||
errorFunc = func() error { return nil }
|
||||
pwmInitErrorFunc = func() error { return nil }
|
||||
return a
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptor(t *testing.T) {
|
||||
a := NewDigisparkAdaptor("bot")
|
||||
gobottest.Assert(t, a.Name(), "bot")
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorConnect(t *testing.T) {
|
||||
a := NewDigisparkAdaptor("bot")
|
||||
func TestAdaptorConnect(t *testing.T) {
|
||||
a := NewAdaptor()
|
||||
gobottest.Assert(t, a.Connect()[0], ErrConnection)
|
||||
|
||||
a = initTestDigisparkAdaptor()
|
||||
a = initTestAdaptor()
|
||||
gobottest.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorFinalize(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
func TestAdaptorFinalize(t *testing.T) {
|
||||
a := initTestAdaptor()
|
||||
gobottest.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
func TestAdaptorDigitalWrite(t *testing.T) {
|
||||
a := initTestAdaptor()
|
||||
err := a.DigitalWrite("0", uint8(1))
|
||||
gobottest.Assert(t, err, nil)
|
||||
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"))
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorServoWrite(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
func TestAdaptorServoWrite(t *testing.T) {
|
||||
a := initTestAdaptor()
|
||||
err := a.ServoWrite("2", uint8(80))
|
||||
gobottest.Assert(t, err, nil)
|
||||
gobottest.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
|
||||
gobottest.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
|
||||
|
||||
a = initTestDigisparkAdaptor()
|
||||
a = initTestAdaptor()
|
||||
errorFunc = func() error { return errors.New("servo error") }
|
||||
err = a.ServoWrite("2", uint8(80))
|
||||
gobottest.Assert(t, err, errors.New("servo error"))
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorPwmWrite(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
func TestAdaptorPwmWrite(t *testing.T) {
|
||||
a := initTestAdaptor()
|
||||
err := a.PwmWrite("1", uint8(100))
|
||||
gobottest.Assert(t, err, nil)
|
||||
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
|
||||
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
|
||||
|
||||
a = initTestDigisparkAdaptor()
|
||||
a = initTestAdaptor()
|
||||
pwmInitErrorFunc = func() error { return errors.New("pwminit error") }
|
||||
err = a.PwmWrite("1", uint8(100))
|
||||
gobottest.Assert(t, err, errors.New("pwminit error"))
|
||||
|
||||
a = initTestDigisparkAdaptor()
|
||||
a = initTestAdaptor()
|
||||
errorFunc = func() error { return errors.New("pwm error") }
|
||||
err = a.PwmWrite("1", uint8(100))
|
||||
gobottest.Assert(t, err, errors.New("pwm error"))
|
||||
|
@ -17,14 +17,14 @@ Example:
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/digispark"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
digisparkAdaptor := digispark.NewDigisparkAdaptor("Digispark")
|
||||
led := gpio.NewLedDriver(digisparkAdaptor, "led", "0")
|
||||
digisparkAdaptor := digispark.NewAdaptor()
|
||||
led := gpio.NewLedDriver(digisparkAdaptor, "0")
|
||||
|
||||
work := func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user