From 6df4535ed515a9cfb6875676a9aed72e122c28a4 Mon Sep 17 00:00:00 2001 From: stevenhowes <38082088+stevenhowes@users.noreply.github.com> Date: Tue, 8 Mar 2022 20:17:16 +0000 Subject: [PATCH] Working code --- README.md | 8 +++++++- go.mod | 3 +++ main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ makefile | 20 ++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 main.go create mode 100644 makefile diff --git a/README.md b/README.md index 7f0dff9..ae0067d 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d8dcc81 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/stevenhowes/TinyGo-Pico-PWM + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..9c6c395 --- /dev/null +++ b/main.go @@ -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) + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..43f6a52 --- /dev/null +++ b/makefile @@ -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 +