piframe-go/cmd/ui/ui.go

51 lines
972 B
Go
Raw Normal View History

2020-08-25 22:55:05 +00:00
package main
import (
bh1750 "github.com/d2r2/go-bh1750"
2020-08-26 00:18:29 +00:00
i2c "github.com/d2r2/go-i2c"
2020-08-25 22:55:05 +00:00
logger "github.com/d2r2/go-logger"
2020-08-26 00:18:29 +00:00
"time"
2020-08-25 22:55:05 +00:00
)
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)
2020-08-26 00:18:29 +00:00
if err != nil {
lg.Fatal(err)
}
2020-08-25 22:55:05 +00:00
// Free I2C connection on exit
defer i2c.Close()
// Setup sensor
sensor := bh1750.NewBH1750()
// Reset sensor prior to use
err = sensor.Reset(i2c)
2020-08-26 00:18:29 +00:00
if err != nil {
lg.Fatal(err)
}
2020-08-25 22:55:05 +00:00
// Read sensor value over time
for {
resolution := bh1750.HighResolution
lux, err := sensor.MeasureAmbientLight(i2c, resolution)
2020-08-26 00:18:29 +00:00
if err != nil {
lg.Fatal(err)
}
2020-08-25 22:55:05 +00:00
println("Illuminance:", lux, "lx")
time.Sleep(500 * time.Millisecond)
}
}