go-rpio/examples/software pwm/pwm.go

48 lines
882 B
Go
Raw Normal View History

2018-06-25 07:52:24 +02:00
/*
A Software Based PWM example by @Ronin11, using the go-rpio library
Toggles a LED on physical pin 10
Connect a LED with resistor from pin 10 to ground.
2018-06-25 07:52:24 +02:00
*/
package main
import (
2018-06-25 07:55:02 +02:00
"fmt"
2018-06-25 07:52:24 +02:00
"os"
"time"
"github.com/Ronin11/go-rpio"
)
const pin = rpio.Pin(10)
func main() {
if err := rpio.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Unmap gpio memory when done
defer rpio.Close()
//Creates the PWM Signal running on the pin, at 2KHz, with a 0% PWM cycle.
2018-06-25 07:52:24 +02:00
pwm := rpio.CreateSofwarePWM(pin, 2000, 0, 32)
2018-06-25 07:52:24 +02:00
pwm.Start()
defer pwm.Stop()
2018-06-25 07:52:24 +02:00
// five times smoothly fade in and out
for i := 0; i < 5; i++ {
for i := uint32(0); i < 32; i++ { // increasing brightness
pwm.SetDutyCycle(i, 32)
time.Sleep(time.Second/32)
}
for i := uint32(32); i > 0; i-- { // decreasing brightness
pwm.SetDutyCycle(i, 32)
2018-06-25 07:52:24 +02:00
time.Sleep(time.Second/32)
}
}
}