forked from kidoman/embd
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathi2cdriver.go
More file actions
44 lines (33 loc) · 687 Bytes
/
i2cdriver.go
File metadata and controls
44 lines (33 loc) · 687 Bytes
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
// Generic I²C driver.
package embd
import "sync"
type i2cBusFactory func(byte) I2CBus
type i2cDriver struct {
busMap map[byte]I2CBus
busMapLock sync.Mutex
ibf i2cBusFactory
}
// NewI2CDriver returns a I2CDriver interface which allows control
// over the I²C subsystem.
func NewI2CDriver(ibf i2cBusFactory) I2CDriver {
return &i2cDriver{
busMap: make(map[byte]I2CBus),
ibf: ibf,
}
}
func (i *i2cDriver) Bus(l byte) I2CBus {
i.busMapLock.Lock()
defer i.busMapLock.Unlock()
if b, ok := i.busMap[l]; ok {
return b
}
b := i.ibf(l)
i.busMap[l] = b
return b
}
func (i *i2cDriver) Close() error {
for _, b := range i.busMap {
b.Close()
}
return nil
}