Initial rough cut UI
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
KemoNine 2020-08-28 18:33:03 -04:00
parent 1f681060dd
commit faf8ac0b5e
1 changed files with 43 additions and 35 deletions

View File

@ -1,50 +1,58 @@
package main package main
import ( import (
bh1750 "github.com/d2r2/go-bh1750" "fmt"
i2c "github.com/d2r2/go-i2c" "log"
logger "github.com/d2r2/go-logger" "os"
"time" "os/exec"
) "os/signal"
"syscall"
var lg = logger.NewPackageLogger("main", "github.com/gdamore/tcell"
logger.ErrorLevel, "github.com/rivo/tview"
) )
func main() { func main() {
defer logger.FinalizeLogger() // fim placeholder so we can operate on it when a signal is received
var fim *exec.Cmd = nil
logger.ChangePackageLogLevel("i2c", logger.ErrorLevel) // Setup signal listening
logger.ChangePackageLogLevel("bh1750", logger.ErrorLevel) sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
defer func() { close(sigs) }()
// Create new connection to I2C to bh1750 // Goroutine to handle os signals (nuke fim so we can get to config ui)
i2c, err := i2c.NewI2C(0x23, 1) go func() {
if err != nil { for sig := range sigs {
lg.Fatal(err) fmt.Println(sig)
} if fim != nil { // Just in case someone lays on ctrl-c or similar during startup
if err := fim.Process.Kill(); err != nil {
log.Fatal("failed to kill process: ", err)
}
}
}
}()
// Free I2C connection on exit // Forever run slideshow / show ui as the main program executed on a PiFrame
defer i2c.Close()
// Setup sensor
sensor := bh1750.NewBH1750()
// Reset sensor prior to use
err = sensor.Reset(i2c)
if err != nil {
lg.Fatal(err)
}
// Read sensor value over time
for { for {
resolution := bh1750.HighResolution // Run slideshow
lux, err := sensor.MeasureAmbientLight(i2c, resolution) fim = exec.Command("/usr/local/bin/fim-slideshow.sh")
if err != nil { if err := fim.Run(); err != nil {
lg.Fatal(err) // Unwrap the error a bit so we can find out if a signal killed fim or something else
// An exit code of -1 means the program didn't exit in time or was terminated by a signal (per the docs)
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() != -1 {
log.Fatalf("Error : ", err)
}
} }
println("Illuminance:", lux, "lx") // Run config UI when slideshow stops
app := tview.NewApplication()
time.Sleep(500 * time.Millisecond) frame := tview.NewFrame(tview.NewBox().SetBackgroundColor(tcell.ColorBlack)).
SetBorders(2, 2, 2, 2, 4, 4).
AddText("PiFrame", true, tview.AlignCenter, tcell.ColorWhite).
AddText("Configuration Utility", true, tview.AlignCenter, tcell.ColorRed)
if err := app.SetRoot(frame, true).EnableMouse(true).Run(); err != nil {
log.Fatal(err)
}
} }
} }