You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
package I2C
|
|
|
|
import (
|
|
"time"
|
|
_ "fmt"
|
|
"sync"
|
|
)
|
|
|
|
/*
|
|
i2c monitor implements a long running monitor responsible for sending active devices to the rlc
|
|
*/
|
|
|
|
type I2CMonitor struct {
|
|
*I2CBus
|
|
Devices *devs
|
|
DevChan chan int
|
|
}
|
|
|
|
type devs struct {
|
|
sync.Mutex
|
|
m map[int]*I2CDevice
|
|
}
|
|
|
|
func NewMonitor(bus int,ch chan int) *I2CMonitor {
|
|
m := &I2CMonitor{}
|
|
b := NewBus(bus)
|
|
m.I2CBus = b
|
|
d := make(map[int]*I2CDevice)
|
|
m.Devices = &devs{m:d}
|
|
m.DevChan = ch
|
|
return m
|
|
}
|
|
|
|
func (m *I2CMonitor) Update() {
|
|
/*
|
|
scans bus and adds new active devices
|
|
*/
|
|
devs := m.Scan()
|
|
chng := m.Devices.Parse(m.I2CBus,devs)
|
|
for _, d := range chng {
|
|
go m.ConnectDevice(d)
|
|
}
|
|
}
|
|
|
|
func (m *I2CMonitor) Monitor() {
|
|
// functon that updates the device list and notifies rlc of any changes to sensor composition
|
|
s := make(chan struct{})
|
|
t := 5 * time.Second
|
|
go func(signal chan struct{},to time.Duration) { // simple signal func to init scan
|
|
for {
|
|
signal <-struct{}{}
|
|
time.Sleep(to)
|
|
}
|
|
}(s,t)
|
|
|
|
for {
|
|
<-s
|
|
m.Update()
|
|
}
|
|
}
|
|
|
|
func (m *I2CMonitor) ConnectDevice(addr int) {
|
|
m.DevChan <-addr
|
|
}
|
|
|
|
func (m *I2CMonitor) GetDevice(addr int) interface{GetAddr() int; GetStatus() string; GetType() string} {
|
|
m.Devices.Lock()
|
|
defer m.Devices.Unlock()
|
|
return m.Devices.m[addr]
|
|
}
|
|
|
|
func (d *devs) Parse(bus *I2CBus,devices map[int]bool) []int {
|
|
d.Lock()
|
|
defer d.Unlock()
|
|
newdevs := []int{}
|
|
for addr, status := range devices {
|
|
if dev, exists := d.m[addr]; exists {
|
|
// device seen
|
|
if status != dev.bool { // if device state changed
|
|
dev.bool = status
|
|
if status {
|
|
newdevs = append(newdevs,dev.GetAddr())
|
|
}
|
|
}
|
|
} else {
|
|
// device not seen yet
|
|
if status {
|
|
// active
|
|
newd := NewDevice(addr,bus)
|
|
newd.bool = status
|
|
d.m[addr] = newd
|
|
newdevs = append(newdevs,newd.GetAddr())
|
|
}
|
|
}
|
|
}
|
|
return newdevs
|
|
}
|