2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-05-04 18:32:55 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-05-04 18:32:55 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-11 22:34:50 +08:00
|
|
|
"fmt"
|
2017-05-04 18:32:55 +08:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/raspi"
|
2017-05-04 18:32:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := raspi.NewAdaptor()
|
2023-12-12 02:09:00 +08:00
|
|
|
led := gpio.NewLedDriver(r, "pwm0")
|
2017-05-04 18:32:55 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
brightness := uint8(0)
|
|
|
|
fadeAmount := uint8(15)
|
|
|
|
|
|
|
|
gobot.Every(100*time.Millisecond, func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Brightness(brightness); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-05-04 18:32:55 +08:00
|
|
|
brightness = brightness + fadeAmount
|
|
|
|
if brightness == 0 || brightness == 255 {
|
|
|
|
fadeAmount = -fadeAmount
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("pwmBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{led},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-05-04 18:32:55 +08:00
|
|
|
}
|