1
0
Fork 0
mirror of https://github.com/kidoman/embd synced 2025-07-04 04:17:41 +02:00

OneWire bus impl

This commit is contained in:
Max Matveev 2016-02-05 22:16:51 -08:00
parent bfcd1345fe
commit 82f119fadb
72 changed files with 568 additions and 174 deletions

44
onewiredriver.go Normal file
View file

@ -0,0 +1,44 @@
// Generic OneWire driver.
package embd
import "sync"
type w1BusFactory func(byte) W1Bus
type w1Driver struct {
busMap map[byte]W1Bus
busMapLock sync.Mutex
ibf w1BusFactory
}
// NewW1Driver returns a W1Driver interface which allows control
// over the OneWire subsystem.
func NewW1Driver(ibf w1BusFactory) W1Driver {
return &w1Driver{
busMap: make(map[byte]W1Bus),
ibf: ibf,
}
}
func (i *w1Driver) Bus(l byte) W1Bus {
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 *w1Driver) Close() error {
for _, b := range i.busMap {
b.Close()
}
return nil
}