microbit: refactoring and increase test coverage for IOPinDriver

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-15 17:37:33 +02:00
parent c457be7bb6
commit 89daf5c324
2 changed files with 61 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package microbit
import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"gobot.io/x/gobot"
@ -154,7 +155,7 @@ func (b *IOPinDriver) Finalize() (err error) {
// DigitalRead reads from a pin
func (b *IOPinDriver) DigitalRead(pin string) (val int, err error) {
p, err := strconv.Atoi(pin)
p, err := validatedPin(pin)
if err != nil {
return
}
@ -168,7 +169,7 @@ func (b *IOPinDriver) DigitalRead(pin string) (val int, err error) {
// DigitalWrite writes to a pin
func (b *IOPinDriver) DigitalWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin)
p, err := validatedPin(pin)
if err != nil {
return
}
@ -181,7 +182,7 @@ func (b *IOPinDriver) DigitalWrite(pin string, level byte) (err error) {
// AnalogRead reads from a pin
func (b *IOPinDriver) AnalogRead(pin string) (val int, err error) {
p, err := strconv.Atoi(pin)
p, err := validatedPin(pin)
if err != nil {
return
}
@ -217,6 +218,19 @@ func (b *IOPinDriver) ensureOutput(pin int) {
}
}
func validatedPin(pin string) (int, error) {
i, err := strconv.Atoi(pin)
if err != nil {
return 0, err
}
if i < 0 || i > 2 {
return 0, errors.New("Invalid pin.")
}
return i, nil
}
// via http://stackoverflow.com/questions/23192262/how-would-you-set-and-clear-a-single-bit-in-go
// Sets the bit at pos in the integer n.

View File

@ -1,6 +1,7 @@
package microbit
import (
"errors"
"strings"
"testing"
@ -53,6 +54,17 @@ func TestIOPinDriverDigitalRead(t *testing.T) {
gobottest.Assert(t, val, 0)
}
func TestIOPinDriverDigitalReadInvalidPin(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
_, err := d.DigitalRead("A3")
gobottest.Refute(t, err, nil)
_, err = d.DigitalRead("6")
gobottest.Assert(t, err, errors.New("Invalid pin."))
}
func TestIOPinDriverDigitalWrite(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
@ -61,6 +73,14 @@ func TestIOPinDriverDigitalWrite(t *testing.T) {
gobottest.Assert(t, d.DigitalWrite("0", 1), nil)
}
func TestIOPinDriverDigitalWriteInvalidPin(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
gobottest.Refute(t, d.DigitalWrite("A3", 1), nil)
gobottest.Assert(t, d.DigitalWrite("6", 1), errors.New("Invalid pin."))
}
func TestIOPinDriverAnalogRead(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
@ -75,6 +95,17 @@ func TestIOPinDriverAnalogRead(t *testing.T) {
gobottest.Assert(t, val, 128)
}
func TestIOPinDriverAnalogReadInvalidPin(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
_, err := d.AnalogRead("A3")
gobottest.Refute(t, err, nil)
_, err = d.AnalogRead("6")
gobottest.Assert(t, err, errors.New("Invalid pin."))
}
func TestIOPinDriverDigitalAnalogRead(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
@ -101,3 +132,16 @@ func TestIOPinDriverDigitalWriteAnalogRead(t *testing.T) {
val, _ := d.AnalogRead("1")
gobottest.Assert(t, val, 128)
}
func TestIOPinDriverAnalogReadDigitalWrite(t *testing.T) {
a := NewBleTestAdaptor()
d := NewIOPinDriver(a)
a.TestReadCharacteristic(func(cUUID string) ([]byte, error) {
return []byte{0, 0, 1, 128, 2, 1}, nil
})
val, _ := d.AnalogRead("1")
gobottest.Assert(t, val, 128)
gobottest.Assert(t, d.DigitalWrite("1", 0), nil)
}