From 75e5eccc1517c695f0de8276222e3ea5e80cdfbe Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 19 Nov 2024 23:24:09 +0900 Subject: [PATCH 1/2] Change the operation of ws2812 to use piolib --- targets/macropad-rp2040/main.go | 38 ++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/targets/macropad-rp2040/main.go b/targets/macropad-rp2040/main.go index 59d34c1..300cb10 100644 --- a/targets/macropad-rp2040/main.go +++ b/targets/macropad-rp2040/main.go @@ -11,8 +11,9 @@ import ( keyboard "github.com/sago35/tinygo-keyboard" "github.com/sago35/tinygo-keyboard/keycodes/jp" + pio "github.com/tinygo-org/pio/rp2-pio" + "github.com/tinygo-org/pio/rp2-pio/piolib" "tinygo.org/x/drivers/sh1106" - "tinygo.org/x/drivers/ws2812" "tinygo.org/x/tinyfont" "tinygo.org/x/tinyfont/freemono" ) @@ -31,6 +32,18 @@ var ( black = color.RGBA{0x00, 0x00, 0x00, 0xFF} ) +const ( + rawWhite = 0x3F3F3FFF + rawRed = 0x00FF00FF + rawGreen = 0xFF0000FF + rawBlue = 0x0000FFFF + rawBlack = 0x000000FF +) + +func writeColors(s pio.StateMachine, ws *piolib.WS2812B, colors []uint32) { + ws.WriteRaw(colors) +} + func run() error { machine.SPI1.Configure(machine.SPIConfig{ Frequency: 48000000, @@ -42,13 +55,18 @@ func run() error { }) display.ClearDisplay() - neo := machine.WS2812 - neo.Configure(machine.PinConfig{Mode: machine.PinOutput}) - ws := ws2812.New(neo) - wsLeds := [12]color.RGBA{} + wsPin := machine.WS2812 + s, _ := pio.PIO0.ClaimStateMachine() + ws, _ := piolib.NewWS2812B(s, wsPin) + err := ws.EnableDMA(true) + if err != nil { + return err + } + wsLeds := [12]uint32{} for i := range wsLeds { - wsLeds[i] = black + wsLeds[i] = rawBlack } + writeColors(s, ws, wsLeds[:]) d := keyboard.New() @@ -92,9 +110,9 @@ func run() error { col := index % 3 fmt.Printf("gk: %d %d %d %d %d\n", layer, index, row, col, state) c := white - wsLeds[index] = white + wsLeds[index] = rawWhite if state == keyboard.PressToRelease { - wsLeds[index] = black + wsLeds[index] = rawBlack c = black } display.ClearBuffer() @@ -114,7 +132,7 @@ func run() error { // for Vial loadKeyboardDef() - err := d.Init() + err = d.Init() if err != nil { return err } @@ -139,7 +157,7 @@ func run() error { if err != nil { return err } - ws.WriteColors(wsLeds[:]) + writeColors(s, ws, wsLeds[:]) time.Sleep(1 * time.Millisecond) } From e9cc4f800d448904546ec966af4e8e615111676e Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 19 Nov 2024 23:25:02 +0900 Subject: [PATCH 2/2] go mod --- go.mod | 5 ++++- go.sum | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 36ad461..e8b4133 100644 --- a/go.mod +++ b/go.mod @@ -10,4 +10,7 @@ require ( tinygo.org/x/tinyfont v0.3.0 ) -require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect +require ( + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect + github.com/tinygo-org/pio v0.0.0-20240901140349-27cbe9d986eb // indirect +) diff --git a/go.sum b/go.sum index dace614..24d3fc8 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/itchio/lzma v0.0.0-20190703113020-d3e24e3e3d49/go.mod h1:avNrevQMli1p github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/tinygo-org/pio v0.0.0-20240901140349-27cbe9d986eb h1:LRIJwOxeTwo8ELCXWzRe6jaO82Hl386Cd9KhOonoCHw= +github.com/tinygo-org/pio v0.0.0-20240901140349-27cbe9d986eb/go.mod h1:LU7Dw00NJ+N86QkeTGjMLNkYcEYMor6wTDpTCu0EaH8= github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4= golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=