Skip to content

Commit 7cf35d5

Browse files
committed
Add ./targets/xiao-kb012/
1 parent eeecbe6 commit 7cf35d5

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

targets/xiao-kb012/main.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"image/color"
7+
"log"
8+
"machine"
9+
"machine/usb"
10+
"math/rand"
11+
"runtime/volatile"
12+
"time"
13+
14+
keyboard "github.com/sago35/tinygo-keyboard"
15+
"github.com/sago35/tinygo-keyboard/ble"
16+
"tinygo.org/x/drivers/ws2812"
17+
)
18+
19+
func main() {
20+
usb.Product = "xiao-kb01-0.1.0"
21+
22+
err := run()
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
}
27+
28+
var (
29+
white = color.RGBA{0x3F, 0x3F, 0x3F, 0xFF}
30+
black = color.RGBA{0x00, 0x00, 0x00, 0xFF}
31+
)
32+
33+
func run() error {
34+
var changed volatile.Register8
35+
changed.Set(0)
36+
37+
neo := machine.D4
38+
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
39+
ws := ws2812.New(neo)
40+
wsLeds := [12]color.RGBA{}
41+
for i := range wsLeds {
42+
wsLeds[i] = black
43+
}
44+
45+
d := keyboard.New()
46+
47+
pins := []machine.Pin{
48+
machine.D0,
49+
machine.D1,
50+
machine.D2,
51+
machine.D3,
52+
}
53+
54+
sm := d.AddSquaredMatrixKeyboard(pins, [][]keyboard.Keycode{
55+
{
56+
0x0000, 0x0001, 0x0002, 0x0003,
57+
0x0004, 0x0005, 0x0006, 0x0007,
58+
0x0008, 0x0009, 0x000A, 0x000B,
59+
},
60+
})
61+
sm.SetCallback(func(layer, index int, state keyboard.State) {
62+
row := index / 4
63+
col := index % 4
64+
fmt.Printf("sm: %d %d %d %d\n", layer, row, col, state)
65+
rowx := row
66+
if col%2 == 1 {
67+
rowx = 3 - row - 1
68+
}
69+
c := rand.Int()
70+
wsLeds[rowx+3*col] = color.RGBA{
71+
byte(c>>16) & 0x3F,
72+
byte(c>>8) & 0x3F,
73+
byte(c>>0) & 0x3F,
74+
0xFF,
75+
}
76+
if state == keyboard.PressToRelease {
77+
wsLeds[rowx+3*col] = black
78+
}
79+
fmt.Printf("%#v\n", wsLeds)
80+
changed.Set(1)
81+
})
82+
83+
d.AddRotaryKeyboard(machine.D5, machine.D10, [][]keyboard.Keycode{
84+
{
85+
0x000C, 0x000D,
86+
},
87+
})
88+
89+
d.AddRotaryKeyboard(machine.D9, machine.D8, [][]keyboard.Keycode{
90+
{
91+
0x000E, 0x000F,
92+
},
93+
})
94+
95+
gpioPins := []machine.Pin{machine.D7, machine.D6}
96+
for c := range gpioPins {
97+
gpioPins[c].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
98+
}
99+
d.AddGpioKeyboard(gpioPins, [][]keyboard.Keycode{
100+
{
101+
0x0010, 0x0011,
102+
},
103+
})
104+
105+
time.Sleep(2 * time.Second)
106+
bk := ble.NewSplitKeyboard(usb.Product)
107+
err := bk.Connect()
108+
if err != nil {
109+
return err
110+
}
111+
112+
d.Keyboard = bk
113+
114+
// for Vial
115+
//loadKeyboardDef()
116+
117+
err = d.Init()
118+
if err != nil {
119+
return err
120+
}
121+
122+
cont := true
123+
ticker := time.Tick(4 * time.Millisecond)
124+
for cont {
125+
<-ticker
126+
err := d.Tick()
127+
if err != nil {
128+
return err
129+
}
130+
if changed.Get() != 0 {
131+
ws.WriteColors(wsLeds[:])
132+
changed.Set(0)
133+
}
134+
}
135+
136+
return nil
137+
}

0 commit comments

Comments
 (0)