Skip to content

Commit

Permalink
Digispark i2c fixes, added Test for checking available addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelybodi committed Aug 17, 2018
1 parent 667e214 commit 0ed6ab0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
27 changes: 26 additions & 1 deletion platforms/digispark/digispark_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 37 additions & 10 deletions platforms/digispark/digispark_i2c.go
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)

Expand Down
22 changes: 14 additions & 8 deletions platforms/digispark/littleWire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 0ed6ab0

Please sign in to comment.