mirror of https://github.com/periph/devices
add i2c implementation
parent
12356d3840
commit
050bc7a20a
@ -1,33 +1,86 @@
|
|||||||
package firmata
|
package firmata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"periph.io/x/conn/v3/gpio"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"periph.io/x/conn/v3/physic"
|
"periph.io/x/conn/v3/physic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Err10BitAddressingNotSupported = errors.New("10-bit addressing not supported")
|
||||||
|
)
|
||||||
|
|
||||||
type I2CBus struct {
|
type I2CBus struct {
|
||||||
|
c ClientI
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *I2CBus) SCL() gpio.PinIO {
|
func newI2CBus(c ClientI) (*I2CBus, error) {
|
||||||
panic("implement me")
|
b := &I2CBus{
|
||||||
|
c: c,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *I2CBus) SDA() gpio.PinIO {
|
err := c.SendI2CConfig(0)
|
||||||
panic("implement me")
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *I2CBus) Close() error {
|
return b, nil
|
||||||
panic("implement me")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *I2CBus) String() string {
|
func (b *I2CBus) Close() error {
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *I2CBus) Tx(addr uint16, w, r []byte) error {
|
func (b *I2CBus) String() string {
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *I2CBus) SetSpeed(f physic.Frequency) error {
|
func (b *I2CBus) Tx(addr uint16, w, r []byte) error {
|
||||||
panic("implement me")
|
if addr >= 0b11111111 {
|
||||||
|
return fmt.Errorf("%w: 0x%04X", Err10BitAddressingNotSupported, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(r) > math.MaxUint16 {
|
||||||
|
return fmt.Errorf("%w: cannot reach more than %d bytes", ErrValueOutOfRange, math.MaxUint16)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
ch := make(chan I2CPacket)
|
||||||
|
defer close(ch)
|
||||||
|
|
||||||
|
address := uint8(addr)
|
||||||
|
|
||||||
|
release, err := b.c.SetI2CAddressListener(address, ch)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer release()
|
||||||
|
|
||||||
|
if len(w) > 0 {
|
||||||
|
if err := b.c.WriteI2CData(address, false, w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(r) > 0 {
|
||||||
|
if err := b.c.ReadI2CData(address, false, uint16(len(r))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pck := <-ch
|
||||||
|
|
||||||
|
copy(r, pck.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *I2CBus) SetSpeed(f physic.Frequency) error {
|
||||||
|
return fmt.Errorf("%w: firmata does not support setting bus frequency", ErrUnsupportedFeature)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue