1
0
mirror of https://github.com/kidoman/embd synced 2024-05-27 22:31:26 +02:00
embd/i2cdriver.go
Karan Misra c35deeb17c host specific drivers can now be loaded separately
this ensures cleaner abstractions/code and will ensure that the produced
binary is as small as possible. a convenience package is provided to
easily load all hosts easily: "github.com/kidoman/embd/host/all"
2014-04-06 06:50:09 +05:30

45 lines
687 B
Go

// 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
}