Refactor digispark package
This commit is contained in:
parent
330e5a8bba
commit
d74c77ec6c
|
@ -1,13 +1,14 @@
|
|||
package digispark
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"strconv"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type DigisparkAdaptor struct {
|
||||
gobot.Adaptor
|
||||
littleWire *LittleWire
|
||||
littleWire lw
|
||||
servo bool
|
||||
pwm bool
|
||||
connect func(*DigisparkAdaptor)
|
||||
|
@ -21,7 +22,7 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
|||
"DigisparkAdaptor",
|
||||
),
|
||||
connect: func(d *DigisparkAdaptor) {
|
||||
d.littleWire = LittleWireConnect()
|
||||
d.littleWire = littleWireConnect()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -29,62 +30,43 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
|||
// Connect starts connection to digispark, returns true if successful
|
||||
func (d *DigisparkAdaptor) Connect() bool {
|
||||
d.connect(d)
|
||||
d.SetConnected(true)
|
||||
return true
|
||||
}
|
||||
|
||||
// Reconnect retries connection to digispark, returns true if successful
|
||||
func (d *DigisparkAdaptor) Reconnect() bool {
|
||||
return d.Connect()
|
||||
}
|
||||
|
||||
// Finalize returns true if finalization is successful
|
||||
func (d *DigisparkAdaptor) Finalize() bool { return true }
|
||||
|
||||
// Disconnect returns true if connection to digispark is ended successfully
|
||||
func (d *DigisparkAdaptor) Disconnect() bool { return true }
|
||||
|
||||
// DigitalWrite writes level to specified pin using littlewire
|
||||
func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) {
|
||||
p, _ := strconv.Atoi(pin)
|
||||
|
||||
d.littleWire.PinMode(uint8(p), 0)
|
||||
d.littleWire.DigitalWrite(uint8(p), level)
|
||||
d.littleWire.pinMode(uint8(p), 0)
|
||||
d.littleWire.digitalWrite(uint8(p), level)
|
||||
}
|
||||
|
||||
// DigitalRead (not yet implemented)
|
||||
func (d *DigisparkAdaptor) DigitalRead(pin string, level byte) {}
|
||||
func (d *DigisparkAdaptor) DigitalRead(pin string) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
// PwmWrite updates pwm pin with sent value
|
||||
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) {
|
||||
if d.pwm == false {
|
||||
d.littleWire.PwmInit()
|
||||
d.littleWire.PwmUpdatePrescaler(1)
|
||||
d.littleWire.pwmInit()
|
||||
d.littleWire.pwmUpdatePrescaler(1)
|
||||
d.pwm = true
|
||||
}
|
||||
d.littleWire.PwmUpdateCompare(value, value)
|
||||
d.littleWire.pwmUpdateCompare(value, value)
|
||||
}
|
||||
|
||||
// AnalogRead (not yet implemented)
|
||||
func (d *DigisparkAdaptor) AnalogRead(string) int { return -1 }
|
||||
|
||||
// InitServo (not yet implemented)
|
||||
func (d *DigisparkAdaptor) InitServo() {}
|
||||
|
||||
// ServoWrite updates servo location with specified angle
|
||||
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) {
|
||||
if d.servo == false {
|
||||
d.littleWire.ServoInit()
|
||||
d.littleWire.servoInit()
|
||||
d.servo = true
|
||||
}
|
||||
d.littleWire.ServoUpdateLocation(angle, angle)
|
||||
d.littleWire.servoUpdateLocation(angle, angle)
|
||||
}
|
||||
|
||||
// I2cStart (not yet implemented)
|
||||
func (d *DigisparkAdaptor) I2cStart(byte) {}
|
||||
|
||||
// I2cRead (not yet implemented)
|
||||
func (d *DigisparkAdaptor) I2cRead(uint16) []uint16 { return make([]uint16, 0) }
|
||||
|
||||
// I2cWrite (not yet implemented)
|
||||
func (d *DigisparkAdaptor) I2cWrite([]uint16) {}
|
||||
|
|
|
@ -1,13 +1,49 @@
|
|||
package digispark
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
type mock struct {
|
||||
locationA uint8
|
||||
locationB uint8
|
||||
pwmChannelA uint8
|
||||
pwmChannelB uint8
|
||||
pwmPrescalerValue uint
|
||||
pin uint8
|
||||
mode uint8
|
||||
state uint8
|
||||
}
|
||||
|
||||
func (l *mock) digitalWrite(pin uint8, state uint8) {
|
||||
l.pin = pin
|
||||
l.state = state
|
||||
}
|
||||
func (l *mock) pinMode(pin uint8, mode uint8) {
|
||||
l.pin = pin
|
||||
l.mode = mode
|
||||
}
|
||||
func (l *mock) pwmInit() {}
|
||||
func (l *mock) pwmStop() {}
|
||||
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) {
|
||||
l.pwmChannelA = channelA
|
||||
l.pwmChannelB = channelB
|
||||
}
|
||||
func (l *mock) pwmUpdatePrescaler(value uint) {
|
||||
l.pwmPrescalerValue = value
|
||||
}
|
||||
func (l *mock) servoInit() {}
|
||||
func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) {
|
||||
l.locationA = locationA
|
||||
l.locationB = locationB
|
||||
}
|
||||
|
||||
func initTestDigisparkAdaptor() *DigisparkAdaptor {
|
||||
a := NewDigisparkAdaptor("bot")
|
||||
a.connect = func(a *DigisparkAdaptor) {}
|
||||
a.littleWire = new(mock)
|
||||
return a
|
||||
}
|
||||
|
||||
|
@ -21,12 +57,17 @@ func TestDigisparkAdaptorConnect(t *testing.T) {
|
|||
gobot.Assert(t, a.Connect(), true)
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorDisconnect(t *testing.T) {
|
||||
func TestDigisparkAdaptorIO(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
gobot.Assert(t, a.Disconnect(), true)
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorReconnect(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
gobot.Assert(t, a.Reconnect(), true)
|
||||
a.InitServo()
|
||||
a.DigitalRead("1")
|
||||
a.DigitalWrite("0", uint8(1))
|
||||
gobot.Assert(t, a.littleWire.(*mock).pin, uint8(0))
|
||||
gobot.Assert(t, a.littleWire.(*mock).state, uint8(1))
|
||||
a.PwmWrite("1", uint8(100))
|
||||
gobot.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
|
||||
gobot.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
|
||||
a.ServoWrite("2", uint8(80))
|
||||
gobot.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
|
||||
gobot.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
|
||||
}
|
||||
|
|
|
@ -2,141 +2,59 @@ package digispark
|
|||
|
||||
//#cgo LDFLAGS: -lusb
|
||||
//#include "littleWire.h"
|
||||
//#include "littleWire_servo.h"
|
||||
//typedef usb_dev_handle littleWire;
|
||||
import "C"
|
||||
import "fmt"
|
||||
|
||||
type LittleWire struct {
|
||||
type lw interface {
|
||||
digitalWrite(uint8, uint8)
|
||||
pinMode(uint8, uint8)
|
||||
pwmInit()
|
||||
pwmStop()
|
||||
pwmUpdateCompare(uint8, uint8)
|
||||
pwmUpdatePrescaler(uint)
|
||||
servoInit()
|
||||
servoUpdateLocation(uint8, uint8)
|
||||
}
|
||||
|
||||
type littleWire struct {
|
||||
lwHandle *C.littleWire
|
||||
}
|
||||
|
||||
func LittleWireConnect() *LittleWire {
|
||||
return &LittleWire{
|
||||
func littleWireConnect() *littleWire {
|
||||
return &littleWire{
|
||||
lwHandle: C.littleWire_connect(),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LittleWire) ReadFirmwareVersion() string {
|
||||
version := uint8(C.readFirmwareVersion(l.lwHandle))
|
||||
return fmt.Sprintf("%v.%v", version&0xF0>>4, version&0x0F)
|
||||
}
|
||||
|
||||
func (l *LittleWire) ChangeSerialNumber(serialNumber int) {
|
||||
C.changeSerialNumber(l.lwHandle, C.int(serialNumber))
|
||||
}
|
||||
|
||||
func (l *LittleWire) LittleWireError() int {
|
||||
return int(C.littleWire_error())
|
||||
}
|
||||
|
||||
func (l *LittleWire) DigitalWrite(pin uint8, state uint8) {
|
||||
func (l *littleWire) digitalWrite(pin uint8, state uint8) {
|
||||
C.digitalWrite(l.lwHandle, C.uchar(pin), C.uchar(state))
|
||||
}
|
||||
|
||||
func (l *LittleWire) PinMode(pin uint8, mode uint8) {
|
||||
func (l *littleWire) pinMode(pin uint8, mode uint8) {
|
||||
C.pinMode(l.lwHandle, C.uchar(pin), C.uchar(mode))
|
||||
}
|
||||
|
||||
func (l *LittleWire) DigitalRead(pin uint8) uint8 {
|
||||
return uint8(C.digitalRead(l.lwHandle, C.uchar(pin)))
|
||||
}
|
||||
|
||||
func (l *LittleWire) InternalPullup(pin uint8, state uint8) {
|
||||
C.internalPullup(l.lwHandle, C.uchar(pin), C.uchar(state))
|
||||
}
|
||||
|
||||
func (l *LittleWire) AnalogInit(voltageRef uint8) {
|
||||
C.analog_init(l.lwHandle, C.uchar(voltageRef))
|
||||
}
|
||||
|
||||
func (l *LittleWire) AnalogRead(channel uint8) uint {
|
||||
return uint(C.analogRead(l.lwHandle, C.uchar(channel)))
|
||||
}
|
||||
|
||||
func (l *LittleWire) PwmInit() {
|
||||
func (l *littleWire) pwmInit() {
|
||||
C.pwm_init(l.lwHandle)
|
||||
}
|
||||
|
||||
func (l *LittleWire) PwmStop() {
|
||||
func (l *littleWire) pwmStop() {
|
||||
C.pwm_stop(l.lwHandle)
|
||||
}
|
||||
|
||||
func (l *LittleWire) PwmUpdateCompare(channelA uint8, channelB uint8) {
|
||||
func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) {
|
||||
C.pwm_updateCompare(l.lwHandle, C.uchar(channelA), C.uchar(channelB))
|
||||
}
|
||||
|
||||
func (l *LittleWire) PwmUpdatePrescaler(value uint) {
|
||||
func (l *littleWire) pwmUpdatePrescaler(value uint) {
|
||||
C.pwm_updatePrescaler(l.lwHandle, C.uint(value))
|
||||
}
|
||||
|
||||
func (l *LittleWire) SpiInit() {
|
||||
C.spi_init(l.lwHandle)
|
||||
func (l *littleWire) servoInit() {
|
||||
C.servo_init(l.lwHandle)
|
||||
}
|
||||
|
||||
func (l *LittleWire) DebugSpi(message uint8) uint8 {
|
||||
return uint8(C.debugSpi(l.lwHandle, C.uchar(message)))
|
||||
}
|
||||
|
||||
func (l *LittleWire) SpiUpdateDelay(duration uint) {
|
||||
C.spi_updateDelay(l.lwHandle, C.uint(duration))
|
||||
}
|
||||
|
||||
func (l *LittleWire) I2cInit() {
|
||||
C.i2c_init(l.lwHandle)
|
||||
}
|
||||
|
||||
func (l *LittleWire) I2cStart(address7bit uint8, direction uint8) uint8 {
|
||||
return uint8(C.i2c_start(l.lwHandle, C.uchar(address7bit), C.uchar(direction)))
|
||||
}
|
||||
|
||||
func (l *LittleWire) I2cUpdateDelay(duration uint) {
|
||||
C.i2c_updateDelay(l.lwHandle, C.uint(duration))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireSendBit(bitValue uint8) {
|
||||
C.onewire_sendBit(l.lwHandle, C.uchar(bitValue))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireWriteByte(messageToSend uint8) {
|
||||
C.onewire_writeByte(l.lwHandle, C.uchar(messageToSend))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireReadByte() uint8 {
|
||||
return uint8(C.onewire_readByte(l.lwHandle))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireReadBit() uint8 {
|
||||
return uint8(C.onewire_readBit(l.lwHandle))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireResetPulse() uint8 {
|
||||
return uint8(C.onewire_resetPulse(l.lwHandle))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireFirstAddress() int {
|
||||
return int(C.onewire_firstAddress(l.lwHandle))
|
||||
}
|
||||
|
||||
func (l *LittleWire) OneWireNextAddress() int {
|
||||
return int(C.onewire_nextAddress(l.lwHandle))
|
||||
}
|
||||
|
||||
func (l *LittleWire) SoftPWMState(state uint8) {
|
||||
C.softPWM_state(l.lwHandle, C.uchar(state))
|
||||
}
|
||||
|
||||
func (l *LittleWire) SoftPWMWrite(ch1 uint8, ch2 uint8, ch3 uint8) {
|
||||
C.softPWM_write(l.lwHandle, C.uchar(ch1), C.uchar(ch2), C.uchar(ch3))
|
||||
}
|
||||
|
||||
func (l *LittleWire) Ws2812Write(pin uint8, r uint8, g uint8, b uint8) {
|
||||
C.ws2812_write(l.lwHandle, C.uchar(pin), C.uchar(r), C.uchar(g), C.uchar(b))
|
||||
}
|
||||
|
||||
func (l *LittleWire) Ws2812Flush(pin uint8) {
|
||||
C.ws2812_flush(l.lwHandle, C.uchar(pin))
|
||||
}
|
||||
|
||||
func (l *LittleWire) Ws2812Preload(r uint8, g uint8, b uint8) {
|
||||
C.ws2812_preload(l.lwHandle, C.uchar(r), C.uchar(g), C.uchar(b))
|
||||
func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) {
|
||||
C.servo_updateLocation(l.lwHandle, C.uchar(locationA), C.uchar(locationB))
|
||||
}
|
||||
|
|
|
@ -1,12 +1 @@
|
|||
package digispark
|
||||
|
||||
//#include "littleWire_servo.h"
|
||||
import "C"
|
||||
|
||||
func (l *LittleWire) ServoInit() {
|
||||
C.servo_init(l.lwHandle)
|
||||
}
|
||||
|
||||
func (l *LittleWire) ServoUpdateLocation(locationChannelA uint8, locationChannelB uint8) {
|
||||
C.servo_updateLocation(l.lwHandle, C.uchar(locationChannelA), C.uchar(locationChannelB))
|
||||
}
|
||||
|
|
|
@ -1,8 +1 @@
|
|||
package digispark
|
||||
|
||||
//#include "littleWire_util.h"
|
||||
import "C"
|
||||
|
||||
func Delay(duration uint) {
|
||||
C.delay(C.uint(duration))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue