Working code

This commit is contained in:
stevenhowes
2022-03-08 20:17:16 +00:00
parent 1ab19d26cc
commit 6df4535ed5
4 changed files with 74 additions and 1 deletions
+7 -1
View File
@@ -1,2 +1,8 @@
# TinyGo-Pico-PWM
A working example of PWM for the Pi Pico using TinyGo
A working example of PWM for the Pi Pico using TinyGo.
I was frustraded by various examples (includig the official docs) being wrong for the Pico so I decided to make my own. It flashes the onboard LED in a 'breathe' pattern.
Makefile included, so you can 'make install', alternatively:
tinygo flash -target=pico
+3
View File
@@ -0,0 +1,3 @@
module github.com/stevenhowes/TinyGo-Pico-PWM
go 1.17
+44
View File
@@ -0,0 +1,44 @@
package main
import (
"machine"
"time"
)
func main() {
// 500Hz
var period uint64 = 1e9 / 500
pin := machine.LED
pwm := machine.PWM4
pin.Configure(machine.PinConfig{Mode: machine.PinPWM})
err := pwm.Configure(machine.PWMConfig{Period: period})
if err != nil {
println(err.Error())
}
ch, err := pwm.Channel(pin)
if err != nil {
println(err.Error())
}
pwm.SetTop(uint32(255))
for {
// Fade in
for i := 0; i <= 255; i++ {
pwm.Set(ch, uint32(i))
time.Sleep(time.Millisecond * 10)
}
// Fade out
for i := 255; i >= 0; i-- {
pwm.Set(ch, uint32(i))
time.Sleep(time.Millisecond * 10)
}
// Wait one second
time.Sleep(time.Millisecond * 1000)
}
}
+20
View File
@@ -0,0 +1,20 @@
.DEFAULT_GOAL := install
fmt:
go mod tidy
go fmt ./...
.PHONY:fmt
lint: fmt
golint ./...
.PHONY:lint
vet: fmt
# Need to work this out for tinygo..
# go vet ./...
.PHONY:vet
install: vet
tinygo flash -target=pico
.PHONY:install