Skip to content

Instantly share code, notes, and snippets.

@maruel
Created December 10, 2017 03:18
Show Gist options
  • Save maruel/f0230fce67aa713154d84c9e2e175e1c to your computer and use it in GitHub Desktop.
Save maruel/f0230fce67aa713154d84c9e2e175e1c to your computer and use it in GitHub Desktop.
Creates two PWMs with different duty cycless using periph.io
package main
import (
"log"
"os"
"os/signal"
"time"
"periph.io/x/periph"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/host/bcm283x"
)
func main() {
if _, err := periph.Init(); err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer bcm283x.GPIO12.Halt()
defer bcm283x.GPIO13.Halt()
const steps = 20
t := time.NewTicker(50 * time.Millisecond)
for i := 0; ; i++ {
select {
case <-c:
return
case <-t.C:
j := i % (steps + 1)
if (i/(steps+1))&1 == 1 {
j = steps - j
}
v := (gpio.Duty(j) * gpio.DutyMax) / steps
if err := bcm283x.GPIO12.PWM(v, 100*time.Microsecond); err != nil {
log.Fatal(err)
}
if err := bcm283x.GPIO13.PWM(gpio.DutyMax-v, 100*time.Microsecond); err !==
nil {
log.Fatal(err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment