This commit is contained in:
parent
19a79f0e59
commit
65f284a2e9
4 changed files with 37 additions and 13 deletions
|
@ -11,7 +11,6 @@ import (
|
|||
"git.joco.dk/sng/fermentord/internal/controllers"
|
||||
"git.joco.dk/sng/fermentord/internal/dwingest"
|
||||
"git.joco.dk/sng/fermentord/internal/hw"
|
||||
"git.joco.dk/sng/fermentord/internal/lcd"
|
||||
"git.joco.dk/sng/fermentord/pkg/temperature"
|
||||
"git.joco.dk/sng/fermentord/pkg/tilt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
|
@ -26,12 +25,13 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
defer wg.Done()
|
||||
|
||||
// Display
|
||||
display, err := lcd.NewLCD()
|
||||
/*display, err := lcd.NewLCD()
|
||||
if err != nil {
|
||||
hub.CaptureException(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer display.Close()
|
||||
*/
|
||||
|
||||
// Controller
|
||||
ctrl := controllers.NewChamberController(*config)
|
||||
|
@ -40,7 +40,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
loadConfiguration := func() {
|
||||
temperature.ConfigUpdate <- *config
|
||||
ctrl.ConfigUpdates <- *config
|
||||
display.SetSetpointTemp(config.FermentationTemperature)
|
||||
//display.SetSetpointTemp(config.FermentationTemperature)
|
||||
}
|
||||
|
||||
viper.OnConfigChange(func(in fsnotify.Event) {
|
||||
|
@ -60,8 +60,8 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
}
|
||||
defer gpio.Close()
|
||||
|
||||
wg.Add(5)
|
||||
go display.Run(ctx, wg)
|
||||
wg.Add(4)
|
||||
//go display.Run(ctx, wg)
|
||||
go ctrl.Run(ctx, wg)
|
||||
go ingest.Run(ctx, wg, js, config)
|
||||
go temperature.PollSensors(ctx, wg, 1*time.Second, config.Sensors.Weight)
|
||||
|
@ -72,7 +72,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
case reading := <-temperature.Reading:
|
||||
ctrl.SetTemperature(reading)
|
||||
ingest.AddReading(reading)
|
||||
display.SetTemperature(reading)
|
||||
//display.SetTemperature(reading)
|
||||
|
||||
case ev := <-gpio.C:
|
||||
var evs string
|
||||
|
@ -81,7 +81,9 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
case hw.DoorClosed:
|
||||
log.Print("Door closed")
|
||||
gpio.LightsOff()
|
||||
gpio.StartFan()
|
||||
if config.CoolerEnabled || config.HeaterEnabled {
|
||||
gpio.StartFan()
|
||||
}
|
||||
ctrl.Resume()
|
||||
evs = "DOOR_CLOSED"
|
||||
|
||||
|
@ -113,7 +115,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
case state := <-ctrl.C:
|
||||
gpioSetState(state, gpio, config)
|
||||
ingest.AddState(state)
|
||||
display.SetState(state)
|
||||
//display.SetState(state)
|
||||
|
||||
case <-ctx.Done():
|
||||
return
|
||||
|
|
|
@ -88,7 +88,7 @@ func (p *ChamberController) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||
ticker.Stop()
|
||||
p.isPaused = false
|
||||
// TODO Check if the line below blocks on shutdown
|
||||
p.setChamberState(ChamberStateIdle)
|
||||
//p.setChamberState(ChamberStateIdle)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -28,11 +28,19 @@ func NewDWIngest() *DWIngest {
|
|||
}
|
||||
|
||||
func (p *DWIngest) AddReading(reading temperature.TemperatureReading) {
|
||||
p.chTemperatureReading <- reading
|
||||
// Non-blocking send
|
||||
select {
|
||||
case p.chTemperatureReading <- reading:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (p *DWIngest) AddState(state controllers.ChamberState) {
|
||||
p.chState <- state
|
||||
// Non-blocking send
|
||||
select {
|
||||
case p.chState <- state:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.Configuration) {
|
||||
|
|
|
@ -3,7 +3,9 @@ package lcd
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.joco.dk/sng/fermentord/internal/controllers"
|
||||
"git.joco.dk/sng/fermentord/pkg/temperature"
|
||||
|
@ -21,6 +23,7 @@ type LCD struct {
|
|||
chTemp chan temperature.TemperatureReading
|
||||
chState chan controllers.ChamberState
|
||||
chSetpoint chan float64
|
||||
lastUpdate time.Time
|
||||
}
|
||||
|
||||
func NewLCD() (*LCD, error) {
|
||||
|
@ -30,6 +33,7 @@ func NewLCD() (*LCD, error) {
|
|||
chTemp: make(chan temperature.TemperatureReading, 10),
|
||||
chState: make(chan controllers.ChamberState, 10),
|
||||
chSetpoint: make(chan float64, 10),
|
||||
lastUpdate: time.Now(),
|
||||
}
|
||||
|
||||
p.bus, err = i2c.NewI2C(0x27, 0)
|
||||
|
@ -49,13 +53,13 @@ func NewLCD() (*LCD, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
err = p.lcd.ShowMessage("Fermentor", device.SHOW_LINE_1|device.SHOW_BLANK_PADDING)
|
||||
err = p.lcd.ShowMessage("Fermentor", device.SHOW_LINE_1)
|
||||
if err != nil {
|
||||
p.bus.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = p.lcd.ShowMessage("Initializing", device.SHOW_LINE_2|device.SHOW_BLANK_PADDING)
|
||||
err = p.lcd.ShowMessage("Initializing", device.SHOW_LINE_2 /*|device.SHOW_BLANK_PADDING*/)
|
||||
if err != nil {
|
||||
p.bus.Close()
|
||||
return nil, err
|
||||
|
@ -66,6 +70,10 @@ func NewLCD() (*LCD, error) {
|
|||
}
|
||||
|
||||
func (p *LCD) Close() error {
|
||||
if err := p.lcd.BacklightOff(); err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
return p.bus.Close()
|
||||
}
|
||||
|
||||
|
@ -123,6 +131,10 @@ func (p *LCD) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||
}
|
||||
|
||||
func (p *LCD) update() error {
|
||||
if time.Since(p.lastUpdate) < 3*time.Second {
|
||||
return nil
|
||||
}
|
||||
|
||||
l1 := fmt.Sprintf("W:%4.1f C:%4.1f %s", p.wort, p.chamber, p.state)
|
||||
l2 := fmt.Sprintf("S:%4.1f A:%4.1f", p.ambient, p.setpoint)
|
||||
|
||||
|
@ -142,5 +154,7 @@ func (p *LCD) update() error {
|
|||
p.l2 = l2
|
||||
}
|
||||
|
||||
p.lastUpdate = time.Now()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue