2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2018-10-09 13:13:54 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2018-10-09 13:13:54 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/raspi"
|
2018-10-09 13:13:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func CCS811BootData(a *i2c.CCS811Driver) {
|
|
|
|
v, err := a.GetHardwareVersion()
|
|
|
|
if err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
fmt.Println(err.Error())
|
2018-10-09 13:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Hardare Version %#x\n", v)
|
|
|
|
|
|
|
|
d, err := a.GetFirmwareBootVersion()
|
|
|
|
if err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
fmt.Println(err.Error())
|
2018-10-09 13:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Boot Version %#x\n", d)
|
|
|
|
|
|
|
|
d, err = a.GetFirmwareAppVersion()
|
|
|
|
if err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
fmt.Println(err.Error())
|
2018-10-09 13:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("App Version %#x\n\n", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := raspi.NewAdaptor()
|
|
|
|
ccs811Driver := i2c.NewCCS811Driver(r)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
CCS811BootData(ccs811Driver)
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
s, err := ccs811Driver.GetStatus()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error fetching data from the status register: %+v\n", err.Error())
|
|
|
|
}
|
|
|
|
fmt.Printf("Status %+v \n", s)
|
|
|
|
|
|
|
|
hd, err := ccs811Driver.HasData()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error fetching data from the status register: %+v\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if hd {
|
|
|
|
ec02, tv0C, _ := ccs811Driver.GetGasData()
|
|
|
|
fmt.Printf("Gas Data - eco2: %+v, tvoc: %+v \n", ec02, tv0C)
|
|
|
|
|
|
|
|
temp, _ := ccs811Driver.GetTemperature()
|
|
|
|
fmt.Printf("Temperature %+v \n\n", temp)
|
|
|
|
} else {
|
2022-10-11 14:32:14 +08:00
|
|
|
fmt.Println("New data is not available")
|
2018-10-09 13:13:54 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("adaFruitBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{ccs811Driver},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-10-09 13:13:54 +08:00
|
|
|
}
|