mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
Digispark i2c fixes, added Test for checking available addresses
This commit is contained in:
parent
667e21443a
commit
0ed6ab0421
@ -99,10 +99,35 @@ func (d *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection
|
||||
if bus != 0 {
|
||||
return nil, fmt.Errorf("Invalid bus number %d, only 0 is supported", bus)
|
||||
}
|
||||
return NewDigisparkI2cConnection(d, uint8(address)), nil
|
||||
c := NewDigisparkI2cConnection(d, uint8(address))
|
||||
if err := c.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return i2c.Connection(c), nil
|
||||
}
|
||||
|
||||
// GetDefaultBus returns the default i2c bus for this platform
|
||||
func (d *Adaptor) GetDefaultBus() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// TestConnection returns found i2c connections to devices in a given range of addresses
|
||||
func (d *Adaptor) TestConnection(start, end int, success func(int)) error {
|
||||
conn, err := d.GetConnection(start, d.GetDefaultBus())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := conn.(*digisparkI2cConnection)
|
||||
if !d.i2c {
|
||||
return errors.New("Digispark i2c not initialized")
|
||||
}
|
||||
if start > end {
|
||||
start, end = end, start
|
||||
}
|
||||
for ; start <= end; start++ {
|
||||
if err = c.Test(uint8(start)); err == nil {
|
||||
success(start)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,36 +1,57 @@
|
||||
package digispark
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type digisparkI2cConnection struct {
|
||||
address uint8
|
||||
adaptor *Adaptor
|
||||
}
|
||||
|
||||
// NewDigisparkI2cConnection creates an I2C connection to an I2C device at
|
||||
// NewDigisparkI2cConnection creates an i2c connection to an i2c device at
|
||||
// the specified address
|
||||
func NewDigisparkI2cConnection(adaptor *Adaptor, address uint8) (connection *digisparkI2cConnection) {
|
||||
c := &digisparkI2cConnection{adaptor: adaptor, address: address}
|
||||
c.Init()
|
||||
return c
|
||||
return &digisparkI2cConnection{adaptor: adaptor, address: address}
|
||||
}
|
||||
|
||||
// Init makes sure that the i2c device is already initialized and started
|
||||
// Init makes sure that the i2c device is already initialized
|
||||
func (c *digisparkI2cConnection) Init() (err error) {
|
||||
if !c.adaptor.i2c {
|
||||
if err = c.adaptor.littleWire.i2cInit(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = c.adaptor.littleWire.i2cStart(c.address, 0); err != nil { // direction as a param?
|
||||
return
|
||||
}
|
||||
c.adaptor.i2c = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Test tests i2c connection with the given address
|
||||
func (c *digisparkI2cConnection) Test(address uint8) error {
|
||||
if !c.adaptor.i2c {
|
||||
return errors.New("Digispark i2c not initialized")
|
||||
}
|
||||
return c.adaptor.littleWire.i2cStart(address, 0)
|
||||
}
|
||||
|
||||
// UpdateDelay updates i2c signal delay amount; tune if neccessary to fit your requirements
|
||||
func (c *digisparkI2cConnection) UpdateDelay(duration uint) error {
|
||||
if !c.adaptor.i2c {
|
||||
return errors.New("Digispark i2c not initialized")
|
||||
}
|
||||
return c.adaptor.littleWire.i2cUpdateDelay(duration)
|
||||
}
|
||||
|
||||
// Read tries to read a full buffer from the i2c device.
|
||||
// Returns an empty array if the response from the board has timed out.
|
||||
func (c *digisparkI2cConnection) Read(b []byte) (read int, err error) {
|
||||
err = c.Init()
|
||||
if !c.adaptor.i2c {
|
||||
err = errors.New("Digispark i2c not initialized")
|
||||
return
|
||||
}
|
||||
if err = c.adaptor.littleWire.i2cStart(c.address, 1); err != nil {
|
||||
return
|
||||
}
|
||||
l := 8
|
||||
stop := uint8(0)
|
||||
|
||||
@ -48,7 +69,13 @@ func (c *digisparkI2cConnection) Read(b []byte) (read int, err error) {
|
||||
}
|
||||
|
||||
func (c *digisparkI2cConnection) Write(data []byte) (written int, err error) {
|
||||
err = c.Init()
|
||||
if !c.adaptor.i2c {
|
||||
err = errors.New("Digispark i2c not initialized")
|
||||
return
|
||||
}
|
||||
if err = c.adaptor.littleWire.i2cStart(c.address, 0); err != nil {
|
||||
return
|
||||
}
|
||||
l := 4
|
||||
stop := uint8(0)
|
||||
|
||||
|
@ -6,7 +6,10 @@ package digispark
|
||||
//typedef usb_dev_handle littleWire;
|
||||
import "C"
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type lw interface {
|
||||
digitalWrite(uint8, uint8) error
|
||||
@ -21,7 +24,7 @@ type lw interface {
|
||||
i2cStart(address7bit uint8, direction uint8) error
|
||||
i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error
|
||||
i2cRead(readBuffer []byte, length int, endWithStop uint8) error
|
||||
// i2cUpdateDelay(uint duration) error
|
||||
i2cUpdateDelay(duration uint) error
|
||||
error() error
|
||||
}
|
||||
|
||||
@ -85,7 +88,10 @@ func (l *littleWire) i2cStart(address7bit uint8, direction uint8) error {
|
||||
if C.i2c_start(l.lwHandle, C.uchar(address7bit), C.uchar(direction)) == 1 {
|
||||
return nil
|
||||
}
|
||||
return l.error()
|
||||
if err := l.error(); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("Littlewire i2cStart failed for %d in direction %d", address7bit, direction)
|
||||
}
|
||||
|
||||
// i2cWrite sends byte(s) over i2c with a given length <= 4
|
||||
@ -100,11 +106,11 @@ func (l *littleWire) i2cRead(readBuffer []byte, length int, endWithStop uint8) e
|
||||
return l.error()
|
||||
}
|
||||
|
||||
//// i2cUpdateDelay updates i2c signal delay amount. Tune if neccessary to fit your requirements
|
||||
//func (l *littleWire) i2cUpdateDelay(uint duration) error {
|
||||
// C.i2c_updateDelay(l.lwHandle, C.uint(duration))
|
||||
// return l.error()
|
||||
//}
|
||||
// i2cUpdateDelay updates i2c signal delay amount. Tune if neccessary to fit your requirements
|
||||
func (l *littleWire) i2cUpdateDelay(duration uint) error {
|
||||
C.i2c_updateDelay(l.lwHandle, C.uint(duration))
|
||||
return l.error()
|
||||
}
|
||||
|
||||
func (l *littleWire) error() error {
|
||||
str := C.GoString(C.littleWire_errorName())
|
||||
|
Loading…
x
Reference in New Issue
Block a user