2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2023-02-04 21:42:57 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2023-02-04 21:42:57 +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/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/nanopi"
|
2023-02-04 21:42:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Wiring
|
|
|
|
// PWR NanoPi: 1, 17 (+3.3V, VCC); 2, 4 (+5V, VDD); 6, 9, 14, 20 (GND)
|
|
|
|
// GPIO NanoPi: the fourth header pin at inner USB side, count from USB side, is the PWM output
|
2024-02-11 22:34:50 +08:00
|
|
|
// LED: the PWM output is NOT able to drive a 20mA LED with full brightness (custom driver or low current LED is needed)
|
2023-02-04 21:42:57 +08:00
|
|
|
// Expected behavior: the LED fades in and out
|
|
|
|
func main() {
|
|
|
|
r := nanopi.NewNeoAdaptor()
|
|
|
|
led := gpio.NewLedDriver(r, "PWM")
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
brightness := uint8(0)
|
|
|
|
fadeAmount := uint8(15)
|
|
|
|
|
|
|
|
gobot.Every(100*time.Millisecond, func() {
|
|
|
|
if err := led.Brightness(brightness); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2023-02-04 21:42:57 +08:00
|
|
|
}
|