1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00
embd/i2cdriver.go

45 lines
687 B
Go
Raw Normal View History

// Generic I²C driver.
2014-03-23 14:09:31 +05:30
2014-03-03 00:51:23 +05:30
package embd
import "sync"
type i2cBusFactory func(byte) I2CBus
2014-03-03 00:51:23 +05:30
type i2cDriver struct {
busMap map[byte]I2CBus
2014-03-03 00:51:23 +05:30
busMapLock sync.Mutex
ibf i2cBusFactory
2014-03-03 00:51:23 +05:30
}
// NewI2CDriver returns a I2CDriver interface which allows control
// over the I²C subsystem.
func NewI2CDriver(ibf i2cBusFactory) I2CDriver {
2014-03-03 00:51:23 +05:30
return &i2cDriver{
busMap: make(map[byte]I2CBus),
ibf: ibf,
2014-03-03 00:51:23 +05:30
}
}
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-03 00:51:23 +05:30
}
b := i.ibf(l)
i.busMap[l] = b
2014-03-03 00:51:23 +05:30
return b
}
func (i *i2cDriver) Close() error {
for _, b := range i.busMap {
b.Close()
}
2014-03-23 05:09:13 +05:30
return nil
}