-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmata_i2c.go
136 lines (112 loc) · 2.95 KB
/
firmata_i2c.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package firmata
import (
// "gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/firmata/client"
)
type firmataI2cConnection struct {
address int
adaptor *Adaptor
}
// NewFirmataI2cConnection creates an I2C connection to an I2C device at
// the specified address
func NewFirmataI2cConnection(adaptor *Adaptor, address int) (connection *firmataI2cConnection) {
return &firmataI2cConnection{adaptor: adaptor, address: address}
}
// 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 *firmataI2cConnection) Read(b []byte) (read int, err error) {
ret := make(chan []byte)
if err = c.adaptor.board.I2cRead(c.address, len(b)); err != nil {
return
}
c.adaptor.board.Once(c.adaptor.board.Event("I2cReply"), func(data interface{}) {
ret <- data.(client.I2cReply).Data
})
result := <-ret
copy(b, result)
read = len(result)
return
}
func (c *firmataI2cConnection) Write(data []byte) (written int, err error) {
var chunk []byte
for len(data) >= 16 {
chunk, data = data[:16], data[16:]
err = c.adaptor.board.I2cWrite(c.address, chunk)
if err != nil {
return
}
written += len(chunk)
}
if len(data) > 0 {
err = c.adaptor.board.I2cWrite(c.address, data[:])
written += len(data)
}
return
}
func (c *firmataI2cConnection) Close() error {
return nil
}
func (c *firmataI2cConnection) ReadByte() (val byte, err error) {
buf := []byte{0}
if _, err = c.Read(buf); err != nil {
return
}
val = buf[0]
return
}
func (c *firmataI2cConnection) ReadByteData(reg uint8) (val uint8, err error) {
if err = c.WriteByte(reg); err != nil {
return
}
return c.ReadByte()
}
func (c *firmataI2cConnection) ReadWordData(reg uint8) (val uint16, err error) {
if err = c.WriteByte(reg); err != nil {
return
}
buf := []byte{0, 0}
if _, err = c.Read(buf); err != nil {
return
}
low, high := buf[0], buf[1]
val = (uint16(high) << 8) | uint16(low)
return
}
func (c *firmataI2cConnection) ReadBlockData(reg uint8, b []byte) (n int, err error) {
// Assume target device handles this well, will not behave exactly as on SMBus
if err = c.WriteByte(reg); err != nil {
return
}
// Try to read full 32 bytes
buf := make([]byte, 32)
bytesRead, err := c.Read(buf)
copy(b, buf)
return bytesRead, err
}
func (c *firmataI2cConnection) WriteByte(val byte) (err error) {
buf := []byte{val}
_, err = c.Write(buf)
return
}
func (c *firmataI2cConnection) WriteByteData(reg uint8, val byte) (err error) {
buf := []byte{reg, val}
_, err = c.Write(buf)
return
}
func (c *firmataI2cConnection) WriteWordData(reg uint8, val uint16) (err error) {
low := uint8(val & 0xff)
high := uint8((val >> 8) & 0xff)
buf := []byte{reg, low, high}
_, err = c.Write(buf)
return
}
func (c *firmataI2cConnection) WriteBlockData(reg uint8, data []byte) (err error) {
if len(data) > 32 {
data = data[:32]
}
buf := make([]byte, len(data)+1)
copy(buf[:1], data)
buf[0] = reg
_, err = c.Write(buf)
return
}