hybridgroup.gobot/examples/raspi_ina3221.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

//go:build example
2017-05-05 11:20:32 +08:00
// +build example
2017-05-05 11:20:32 +08:00
//
// Do not build by default.
2017-05-04 06:08:56 +08:00
package main
import (
"fmt"
2017-05-04 06:08:56 +08:00
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/raspi"
2017-05-04 06:08:56 +08:00
)
func main() {
r := raspi.NewAdaptor()
ina := i2c.NewINA3221Driver(r)
work := func() {
gobot.Every(5*time.Second, func() {
for _, ch := range []i2c.INA3221Channel{i2c.INA3221Channel1, i2c.INA3221Channel2, i2c.INA3221Channel3} {
val, err := ina.GetBusVoltage(ch)
if err != nil {
fmt.Printf("INA3221Channel %v Bus Voltage error: %v\n", ch, err)
}
fmt.Printf("INA3221Channel %v Bus Voltage: %fV\n", ch, val)
val, err = ina.GetShuntVoltage(ch)
if err != nil {
fmt.Printf("INA3221Channel %v Shunt Voltage error: %v\n", ch, err)
}
fmt.Printf("INA3221Channel %v Shunt Voltage: %fV\n", ch, val)
val, err = ina.GetCurrent(ch)
if err != nil {
fmt.Printf("INA3221Channel %v Current error: %v\n", ch, err)
}
fmt.Printf("INA3221Channel %v Current: %fmA\n", ch, val)
val, err = ina.GetLoadVoltage(ch)
if err != nil {
fmt.Printf("INA3221Channel %v Load Voltage error: %v\n", ch, err)
}
fmt.Printf("INA3221Channel %v Load Voltage: %fV\n", ch, val)
2017-05-04 06:08:56 +08:00
}
})
}
robot := gobot.NewRobot("INA3221 Robot",
2017-05-04 06:08:56 +08:00
[]gobot.Connection{r},
[]gobot.Device{ina},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
2017-05-04 06:08:56 +08:00
}