gobot.io/x/gobot@v1.16.0/platforms/firmata/firmata_i2c.go (about) 1 package firmata 2 3 import ( 4 // "gobot.io/x/gobot/drivers/i2c" 5 "gobot.io/x/gobot/platforms/firmata/client" 6 ) 7 8 type firmataI2cConnection struct { 9 address int 10 adaptor *Adaptor 11 } 12 13 // NewFirmataI2cConnection creates an I2C connection to an I2C device at 14 // the specified address 15 func NewFirmataI2cConnection(adaptor *Adaptor, address int) (connection *firmataI2cConnection) { 16 return &firmataI2cConnection{adaptor: adaptor, address: address} 17 } 18 19 // Read tries to read a full buffer from the i2c device. 20 // Returns an empty array if the response from the board has timed out. 21 func (c *firmataI2cConnection) Read(b []byte) (read int, err error) { 22 ret := make(chan []byte) 23 24 if err = c.adaptor.Board.I2cRead(c.address, len(b)); err != nil { 25 return 26 } 27 28 c.adaptor.Board.Once(c.adaptor.Board.Event("I2cReply"), func(data interface{}) { 29 ret <- data.(client.I2cReply).Data 30 }) 31 32 result := <-ret 33 copy(b, result) 34 35 read = len(result) 36 37 return 38 } 39 40 func (c *firmataI2cConnection) Write(data []byte) (written int, err error) { 41 var chunk []byte 42 for len(data) >= 16 { 43 chunk, data = data[:16], data[16:] 44 err = c.adaptor.Board.I2cWrite(c.address, chunk) 45 if err != nil { 46 return 47 } 48 written += len(chunk) 49 } 50 if len(data) > 0 { 51 err = c.adaptor.Board.I2cWrite(c.address, data[:]) 52 written += len(data) 53 } 54 return 55 } 56 57 func (c *firmataI2cConnection) Close() error { 58 return nil 59 } 60 61 func (c *firmataI2cConnection) ReadByte() (val byte, err error) { 62 buf := []byte{0} 63 if _, err = c.Read(buf); err != nil { 64 return 65 } 66 val = buf[0] 67 return 68 } 69 70 func (c *firmataI2cConnection) ReadByteData(reg uint8) (val uint8, err error) { 71 if err = c.WriteByte(reg); err != nil { 72 return 73 } 74 return c.ReadByte() 75 } 76 77 func (c *firmataI2cConnection) ReadWordData(reg uint8) (val uint16, err error) { 78 if err = c.WriteByte(reg); err != nil { 79 return 80 } 81 82 buf := []byte{0, 0} 83 if _, err = c.Read(buf); err != nil { 84 return 85 } 86 low, high := buf[0], buf[1] 87 88 val = (uint16(high) << 8) | uint16(low) 89 return 90 } 91 92 func (c *firmataI2cConnection) WriteByte(val byte) (err error) { 93 buf := []byte{val} 94 _, err = c.Write(buf) 95 return 96 } 97 98 func (c *firmataI2cConnection) WriteByteData(reg uint8, val byte) (err error) { 99 buf := []byte{reg, val} 100 _, err = c.Write(buf) 101 return 102 } 103 104 func (c *firmataI2cConnection) WriteWordData(reg uint8, val uint16) (err error) { 105 low := uint8(val & 0xff) 106 high := uint8((val >> 8) & 0xff) 107 buf := []byte{reg, low, high} 108 _, err = c.Write(buf) 109 return 110 } 111 112 func (c *firmataI2cConnection) WriteBlockData(reg uint8, data []byte) (err error) { 113 if len(data) > 32 { 114 data = data[:32] 115 } 116 117 buf := make([]byte, len(data)+1) 118 copy(buf[:1], data) 119 buf[0] = reg 120 _, err = c.Write(buf) 121 return 122 }