forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_driver.go
148 lines (116 loc) · 3.97 KB
/
generic_driver.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
package i2c
import (
"fmt"
"time"
)
// GenericDriver implements the interface gobot.Driver.
type GenericDriver struct {
*Driver
}
// NewGenericDriver creates a new generic i2c gobot driver, which just forwards all connection functions.
func NewGenericDriver(c Connector, name string, address int, options ...func(Config)) *GenericDriver {
return &GenericDriver{Driver: NewDriver(c, name, address, options...)}
}
// WriteByte writes one byte to the i2c device.
func (d *GenericDriver) WriteByte(val byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.WriteByte(val)
}
// WriteByteData writes the given byte value to the given register of an i2c device.
func (d *GenericDriver) WriteByteData(reg uint8, val byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.WriteByteData(reg, val)
}
// WriteWordData writes the given 16 bit value to the given register of an i2c device.
func (d *GenericDriver) WriteWordData(reg uint8, val uint16) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.WriteWordData(reg, val)
}
// WriteBlockData writes the given buffer to the given register of an i2c device.
func (d *GenericDriver) WriteBlockData(reg uint8, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.WriteBlockData(reg, data)
}
// WriteData writes the given buffer to the given register of an i2c device.
// It uses plain write to prevent WriteBlockData(), which is sometimes not supported by adaptor.
func (d *GenericDriver) WriteData(reg uint8, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
buf := make([]byte, len(data)+1)
copy(buf[1:], data)
buf[0] = reg
return d.writeAndCheckCount(buf)
}
// Write writes the given buffer to the i2c device.
func (d *GenericDriver) Write(data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.writeAndCheckCount(data)
}
// ReadByte reads a byte from the current register of an i2c device.
func (d *GenericDriver) ReadByte() (byte, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.ReadByte()
}
// ReadByteData reads a byte from the given register of an i2c device.
func (d *GenericDriver) ReadByteData(reg uint8) (byte, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.ReadByteData(reg)
}
// ReadWordData reads a 16 bit value starting from the given register of an i2c device.
func (d *GenericDriver) ReadWordData(reg uint8) (uint16, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.ReadWordData(reg)
}
// ReadBlockData fills the given buffer with reads starting from the given register of an i2c device.
func (d *GenericDriver) ReadBlockData(reg uint8, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.connection.ReadBlockData(reg, data)
}
// ReadData fills the given buffer with reads from the given register of an i2c device.
// It uses plain read to prevent ReadBlockData(), which is sometimes not supported by adaptor.
func (d *GenericDriver) ReadData(reg uint8, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.connection.WriteByte(reg); err != nil {
return err
}
// write process needs some time, so wait at least 5ms before read a value
// when decreasing to much, the check below will fail
time.Sleep(10 * time.Millisecond)
return d.readAndCheckCount(data)
}
// Read fills the given buffer with reads of an i2c device.
func (d *GenericDriver) Read(data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.readAndCheckCount(data)
}
func (d *GenericDriver) writeAndCheckCount(data []byte) error {
n, err := d.connection.Write(data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("written count (%d) differ from expected (%d)", n, len(data))
}
return nil
}
func (d *GenericDriver) readAndCheckCount(data []byte) error {
n, err := d.connection.Read(data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("read count (%d) differ from expected (%d)", n, len(data))
}
return nil
}