Move lux sensor logic to own file while main ui is reworked
This commit is contained in:
parent
a616ac6c56
commit
721c5f9728
50
cmd/ui/lux.go
Normal file
50
cmd/ui/lux.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
bh1750 "github.com/d2r2/go-bh1750"
|
||||||
|
i2c "github.com/d2r2/go-i2c"
|
||||||
|
logger "github.com/d2r2/go-logger"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var lg = logger.NewPackageLogger("main",
|
||||||
|
logger.ErrorLevel,
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer logger.FinalizeLogger()
|
||||||
|
|
||||||
|
logger.ChangePackageLogLevel("i2c", logger.ErrorLevel)
|
||||||
|
logger.ChangePackageLogLevel("bh1750", logger.ErrorLevel)
|
||||||
|
|
||||||
|
// Create new connection to I2C to bh1750
|
||||||
|
i2c, err := i2c.NewI2C(0x23, 1)
|
||||||
|
if err != nil {
|
||||||
|
lg.Fatal(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
|
||||||
|
for {
|
||||||
|
resolution := bh1750.HighResolution
|
||||||
|
lux, err := sensor.MeasureAmbientLight(i2c, resolution)
|
||||||
|
if err != nil {
|
||||||
|
lg.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Illuminance:", lux, "lx")
|
||||||
|
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue