forked from hybridgroup/gobot
/
digispark_i2c.go
160 lines (139 loc) · 3.59 KB
/
digispark_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package digispark
import (
"errors"
)
type digisparkI2cConnection struct {
address uint8
adaptor *Adaptor
}
// NewDigisparkI2cConnection creates an i2c connection to an i2c device at
// the specified address
func NewDigisparkI2cConnection(adaptor *Adaptor, address uint8) (connection *digisparkI2cConnection) {
return &digisparkI2cConnection{adaptor: adaptor, address: address}
}
// 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
}
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) {
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)
for stop == 0 {
if read+l >= len(b) {
l = len(b) - read
stop = 1
}
if err = c.adaptor.littleWire.i2cRead(b[read:read+l], l, stop); err != nil {
return
}
read += l
}
return
}
func (c *digisparkI2cConnection) Write(data []byte) (written int, err error) {
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)
for stop == 0 {
if written+l >= len(data) {
l = len(data) - written
stop = 1
}
if err = c.adaptor.littleWire.i2cWrite(data[written:written+l], l, stop); err != nil {
return
}
written += l
}
return
}
func (c *digisparkI2cConnection) Close() error {
return nil
}
func (c *digisparkI2cConnection) ReadByte() (val byte, err error) {
b := make([]byte, 1)
if err = c.adaptor.littleWire.i2cRead(b, 1, 1); err != nil {
return
}
val = b[0]
return
}
func (c *digisparkI2cConnection) ReadByteData(reg uint8) (val uint8, err error) {
if err = c.WriteByte(reg); err != nil {
return
}
return c.ReadByte()
}
func (c *digisparkI2cConnection) 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 *digisparkI2cConnection) WriteByte(val byte) (err error) {
b := []byte{val}
err = c.adaptor.littleWire.i2cWrite(b, 1, 1)
return
}
func (c *digisparkI2cConnection) WriteByteData(reg uint8, val byte) (err error) {
buf := []byte{reg, val}
_, err = c.Write(buf)
return
}
func (c *digisparkI2cConnection) 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 *digisparkI2cConnection) 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
}