This commit is contained in:
parent
1f681060dd
commit
faf8ac0b5e
78
cmd/ui/ui.go
78
cmd/ui/ui.go
|
@ -1,50 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
bh1750 "github.com/d2r2/go-bh1750"
|
||||
i2c "github.com/d2r2/go-i2c"
|
||||
logger "github.com/d2r2/go-logger"
|
||||
"time"
|
||||
)
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
var lg = logger.NewPackageLogger("main",
|
||||
logger.ErrorLevel,
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
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)
|
||||
logger.ChangePackageLogLevel("bh1750", logger.ErrorLevel)
|
||||
// Setup signal listening
|
||||
sigs := make(chan os.Signal)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer func() { close(sigs) }()
|
||||
|
||||
// Create new connection to I2C to bh1750
|
||||
i2c, err := i2c.NewI2C(0x23, 1)
|
||||
if err != nil {
|
||||
lg.Fatal(err)
|
||||
}
|
||||
// Goroutine to handle os signals (nuke fim so we can get to config ui)
|
||||
go func() {
|
||||
for sig := range sigs {
|
||||
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
|
||||
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
|
||||
// Forever run slideshow / show ui as the main program executed on a PiFrame
|
||||
for {
|
||||
resolution := bh1750.HighResolution
|
||||
lux, err := sensor.MeasureAmbientLight(i2c, resolution)
|
||||
if err != nil {
|
||||
lg.Fatal(err)
|
||||
// Run slideshow
|
||||
fim = exec.Command("/usr/local/bin/fim-slideshow.sh")
|
||||
if err := fim.Run(); err != nil {
|
||||
// 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")
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
// Run config UI when slideshow stops
|
||||
app := tview.NewApplication()
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue