Wait on slaves for w1 bus master bulk read
This commit is contained in:
parent
1d72205a2d
commit
840777b2fd
2 changed files with 41 additions and 5 deletions
|
@ -28,8 +28,6 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
defer hub.Flush(10 * time.Second)
|
||||
defer wg.Done()
|
||||
|
||||
temperature.Initialize()
|
||||
|
||||
// Controller
|
||||
chCtrlTemp := make(chan temperature.TemperatureReading, 10)
|
||||
defer close(chCtrlTemp)
|
||||
|
|
|
@ -18,8 +18,15 @@ import (
|
|||
"github.com/getsentry/sentry-go"
|
||||
)
|
||||
|
||||
type BulkReadBusState int
|
||||
|
||||
const (
|
||||
BulkReadIdle BulkReadBusState = 0
|
||||
BulkReadBusy BulkReadBusState = -1
|
||||
BulkReadReady BulkReadBusState = 1
|
||||
|
||||
sensorConversionTime = 750 * time.Millisecond
|
||||
busPollDelay = 10 * time.Millisecond
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -39,7 +46,7 @@ var (
|
|||
filters map[string]*EWMAFilter
|
||||
)
|
||||
|
||||
func Initialize() {
|
||||
func init() {
|
||||
sensors = make([]string, 0)
|
||||
filters = make(map[string]*EWMAFilter)
|
||||
C = make(chan TemperatureReading, 30)
|
||||
|
@ -60,6 +67,7 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
|
|||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
work_loop:
|
||||
select {
|
||||
case <-ticker.C:
|
||||
start := time.Now()
|
||||
|
@ -91,8 +99,24 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
|
|||
time.Sleep(deltaSleep)
|
||||
}
|
||||
|
||||
// Read all sensors.
|
||||
readSensors(hub)
|
||||
// Poll bus state
|
||||
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:
|
||||
sensors = c
|
||||
|
@ -153,6 +177,20 @@ func triggerBulkRead() error {
|
|||
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.
|
||||
func read(sensor string) (int64, error) {
|
||||
path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave")
|
||||
|
|
Loading…
Reference in a new issue