Wait on slaves for w1 bus master bulk read

This commit is contained in:
Søren Rasmussen 2022-03-15 17:09:53 +01:00
parent 1d72205a2d
commit 840777b2fd
2 changed files with 41 additions and 5 deletions

View file

@ -28,8 +28,6 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
defer hub.Flush(10 * time.Second) defer hub.Flush(10 * time.Second)
defer wg.Done() defer wg.Done()
temperature.Initialize()
// Controller // Controller
chCtrlTemp := make(chan temperature.TemperatureReading, 10) chCtrlTemp := make(chan temperature.TemperatureReading, 10)
defer close(chCtrlTemp) defer close(chCtrlTemp)

View file

@ -18,8 +18,15 @@ import (
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
) )
type BulkReadBusState int
const ( const (
BulkReadIdle BulkReadBusState = 0
BulkReadBusy BulkReadBusState = -1
BulkReadReady BulkReadBusState = 1
sensorConversionTime = 750 * time.Millisecond sensorConversionTime = 750 * time.Millisecond
busPollDelay = 10 * time.Millisecond
) )
var ( var (
@ -39,7 +46,7 @@ var (
filters map[string]*EWMAFilter filters map[string]*EWMAFilter
) )
func Initialize() { func init() {
sensors = make([]string, 0) sensors = make([]string, 0)
filters = make(map[string]*EWMAFilter) filters = make(map[string]*EWMAFilter)
C = make(chan TemperatureReading, 30) C = make(chan TemperatureReading, 30)
@ -60,6 +67,7 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
defer ticker.Stop() defer ticker.Stop()
for { for {
work_loop:
select { select {
case <-ticker.C: case <-ticker.C:
start := time.Now() start := time.Now()
@ -91,8 +99,24 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
time.Sleep(deltaSleep) time.Sleep(deltaSleep)
} }
// Read all sensors. // Poll bus state
readSensors(hub) poll_bus:
state, err := pollBusState()
if err != nil {
accErrTrigger++
hub.CaptureException(err)
log.Print(err)
break work_loop
}
switch state {
case BulkReadBusy:
time.Sleep(busPollDelay)
goto poll_bus
case BulkReadIdle, BulkReadReady:
readSensors(hub)
}
case c := <-ConfigUpdates: case c := <-ConfigUpdates:
sensors = c sensors = c
@ -153,6 +177,20 @@ func triggerBulkRead() error {
return err return err
} }
func pollBusState() (BulkReadBusState, error) {
b, err := ioutil.ReadFile("/sys/bus/w1/devices/w1_bus_master1/therm_bulk_read")
if err != nil {
return BulkReadIdle, err
}
i, err := strconv.Atoi(string(b))
if err != nil {
return BulkReadIdle, err
}
return BulkReadBusState(i), nil
}
// read returns the temperature of the specified sensor in millidegrees celcius. // read returns the temperature of the specified sensor in millidegrees celcius.
func read(sensor string) (int64, error) { func read(sensor string) (int64, error) {
path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave") path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave")