1
0
mirror of https://github.com/kidoman/embd synced 2024-10-31 23:38:44 +01:00
embd/i2cdriver.go

45 lines
687 B
Go
Raw Normal View History

// Generic I²C driver.
2014-03-23 09:39:31 +01:00
2014-03-02 20:21:23 +01:00
package embd
import "sync"
type i2cBusFactory func(byte) I2CBus
2014-03-02 20:21:23 +01:00
type i2cDriver struct {
busMap map[byte]I2CBus
2014-03-02 20:21:23 +01:00
busMapLock sync.Mutex
ibf i2cBusFactory
2014-03-02 20:21:23 +01:00
}
// NewI2CDriver returns a I2CDriver interface which allows control
// over the I²C subsystem.
func NewI2CDriver(ibf i2cBusFactory) I2CDriver {
2014-03-02 20:21:23 +01:00
return &i2cDriver{
busMap: make(map[byte]I2CBus),
ibf: ibf,
2014-03-02 20:21:23 +01:00
}
}
func (i *i2cDriver) Bus(l byte) I2CBus {
i.busMapLock.Lock()
defer i.busMapLock.Unlock()
if b, ok := i.busMap[l]; ok {
return b
2014-03-02 20:21:23 +01:00
}
b := i.ibf(l)
i.busMap[l] = b
2014-03-02 20:21:23 +01:00
return b
}
func (i *i2cDriver) Close() error {
for _, b := range i.busMap {
b.Close()
}
2014-03-23 00:39:13 +01:00
return nil
}