Skip to content

Commit fc5391b

Browse files
committed
WIP
1 parent 8bdee26 commit fc5391b

File tree

16 files changed

+1591
-0
lines changed

16 files changed

+1591
-0
lines changed

targets/sgkey-ble/ble.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build tinygo && nrf52840
2+
3+
package main
4+
5+
import (
6+
"tinygo.org/x/bluetooth"
7+
)
8+
9+
var adapter = bluetooth.DefaultAdapter
10+
var reportIn bluetooth.Characteristic
11+
12+
var reportMap = []byte{
13+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
14+
0x09, 0x06, // USAGE (Keyboard)
15+
0xa1, 0x01, // COLLECTION (Application)
16+
0x85, 0x01, // REPORT_ID (1)
17+
0x05, 0x07, // USAGE_PAGE (Keyboard)
18+
0x19, 0x01, // USAGE_MINIMUM
19+
0x29, 0x7f, // USAGE_MAXIMUM
20+
0x15, 0x00, // LOGICAL_MINIMUM (0)
21+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
22+
0x75, 0x01, // REPORT_SIZE (1)
23+
0x95, 0x08, // REPORT_COUNT (8)
24+
0x81, 0x02, // INPUT (Data,Var,Abs)
25+
0x95, 0x01, // REPORT_COUNT (1)
26+
0x75, 0x08, // REPORT_SIZE (8)
27+
0x81, 0x01, // INPUT (Cnst,Ary,Abs)
28+
0x95, 0x06, // REPORT_COUNT (6)
29+
0x75, 0x08, // REPORT_SIZE (8)
30+
0x15, 0x00, // LOGICAL_MINIMUM (0)
31+
0x25, 0x65, // LOGICAL_MAXIMUM (101)
32+
0x05, 0x07, // USAGE_PAGE (Keyboard)
33+
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
34+
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
35+
0x81, 0x00, // INPUT (Data,Ary,Abs)
36+
0xc0, // END_COLLECTION
37+
}

targets/sgkey-ble/def.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import keyboard "github.com/sago35/tinygo-keyboard"
4+
5+
func loadKeyboardDef() {
6+
keyboard.KeyboardDef = []byte{
7+
0x5D, 0x00, 0x00, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x3D, 0x82, 0x80, 0x19, 0x1B, 0x9A, 0xE8, 0xB8, 0xA7, 0x6E, 0x26, 0x77, 0x5D, 0x26, 0xFB, 0x93, 0xC2, 0xE2, 0xE7, 0x46, 0x70, 0xE9, 0xC5, 0x5C, 0x25, 0x18, 0xAC, 0xC0, 0x02, 0xFD, 0x58, 0x39, 0x21, 0x5D, 0xCD, 0x75, 0xE8, 0x8B, 0x63, 0x00, 0xDB, 0x30, 0x34, 0xCC, 0x0F, 0x1B, 0xD8, 0x91, 0xE9, 0x98, 0x70, 0x7A, 0x6A, 0x9C, 0x4F, 0x71, 0xC3, 0x6C, 0xBE, 0xF6, 0x64, 0xAC, 0x6A, 0x5D, 0xE6, 0xAE, 0xEC, 0x22, 0x30, 0x1D, 0x96, 0x95, 0x6B, 0xBA, 0xC6, 0xA4, 0x9C, 0xC1, 0xD4, 0xCA, 0x47, 0xCE, 0x7E, 0x3C, 0x18, 0x38, 0xAA, 0x93, 0x88, 0x30, 0x3C, 0xB6, 0x9F, 0x66, 0x6D, 0x54, 0xE4, 0x7C, 0x28, 0x18, 0x6A, 0x5C, 0xA6, 0xD0, 0x1B, 0xDD, 0x25, 0xB3, 0x1B, 0x4F, 0x5D, 0xEC, 0x32, 0xE4, 0x35, 0xB8, 0x8A, 0xC2, 0x8C, 0x5D, 0xF1, 0x7F, 0x09, 0x77, 0xDF, 0x51, 0x63, 0x77, 0x5F, 0xE4, 0x1E, 0xAE, 0x91, 0x4A, 0x91, 0x71, 0xF8, 0xE5, 0xAF, 0x25, 0xFB, 0x9E, 0xFF, 0xAA, 0x78, 0x5F, 0x80,
8+
}
9+
}

targets/sgkey-ble/main.go

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
package main
2+
3+
import (
4+
"context"
5+
_ "embed"
6+
"log"
7+
"machine"
8+
"machine/usb"
9+
10+
keyboard "github.com/sago35/tinygo-keyboard"
11+
"github.com/sago35/tinygo-keyboard/keycodes/jp"
12+
"tinygo.org/x/bluetooth"
13+
)
14+
15+
func main() {
16+
usb.Product = "sgkey-0.1.0"
17+
18+
err := run()
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
}
23+
24+
type RCS struct {
25+
row, col int
26+
state keyboard.State
27+
}
28+
29+
type KeyEvent struct {
30+
layer, indx int
31+
state keyboard.State
32+
}
33+
34+
func run() error {
35+
d := keyboard.New()
36+
37+
colPins := []machine.Pin{
38+
machine.D8,
39+
machine.D9,
40+
machine.D10,
41+
}
42+
43+
rowPins := []machine.Pin{
44+
machine.D1,
45+
machine.D2,
46+
}
47+
48+
// --------------------------------------------------
49+
bluetooth.SetSecParamsBonding()
50+
bluetooth.SetSecCapabilities(bluetooth.NoneGapIOCapability)
51+
52+
err := adapter.Enable()
53+
if err != nil {
54+
return err
55+
}
56+
adv := adapter.DefaultAdvertisement()
57+
adv.Configure(bluetooth.AdvertisementOptions{
58+
LocalName: "tinygo-corne",
59+
ServiceUUIDs: []bluetooth.UUID{
60+
61+
bluetooth.ServiceUUIDDeviceInformation,
62+
bluetooth.ServiceUUIDBattery,
63+
bluetooth.ServiceUUIDHumanInterfaceDevice,
64+
},
65+
/*
66+
gatt
67+
gacc
68+
parameters ?
69+
battery service
70+
device information
71+
hid
72+
73+
*/
74+
})
75+
adv.Start()
76+
// device information
77+
// model number string r 0x2A24
78+
// manufacture name string r 2A29
79+
// pnp id r 2A50
80+
//
81+
adapter.AddService(&bluetooth.Service{
82+
UUID: bluetooth.ServiceUUIDDeviceInformation,
83+
Characteristics: []bluetooth.CharacteristicConfig{
84+
{
85+
UUID: bluetooth.CharacteristicUUIDManufacturerNameString,
86+
Flags: bluetooth.CharacteristicReadPermission,
87+
Value: []byte("Nice Keyboards"),
88+
},
89+
{
90+
UUID: bluetooth.CharacteristicUUIDModelNumberString,
91+
Flags: bluetooth.CharacteristicReadPermission,
92+
Value: []byte("nice!nano"),
93+
},
94+
{
95+
UUID: bluetooth.CharacteristicUUIDPnPID,
96+
Flags: bluetooth.CharacteristicReadPermission,
97+
Value: []byte{0x02, 0x8a, 0x24, 0x66, 0x82, 0x34, 0x36},
98+
//Value: []byte{0x02, uint8(0x10C4 >> 8), uint8(0x10C4 & 0xff), uint8(0x0001 >> 8), uint8(0x0001 & 0xff)},
99+
},
100+
},
101+
})
102+
adapter.AddService(&bluetooth.Service{
103+
UUID: bluetooth.ServiceUUIDBattery,
104+
Characteristics: []bluetooth.CharacteristicConfig{
105+
{
106+
UUID: bluetooth.CharacteristicUUIDBatteryLevel,
107+
Value: []byte{80},
108+
Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicNotifyPermission,
109+
},
110+
},
111+
})
112+
// gacc
113+
/*
114+
device name r
115+
apperance r
116+
peripheral prefreed connection
117+
118+
*/
119+
120+
adapter.AddService(&bluetooth.Service{
121+
UUID: bluetooth.ServiceUUIDGenericAccess,
122+
Characteristics: []bluetooth.CharacteristicConfig{
123+
{
124+
UUID: bluetooth.CharacteristicUUIDDeviceName,
125+
Flags: bluetooth.CharacteristicReadPermission,
126+
Value: []byte("tinygo-corne"),
127+
},
128+
{
129+
130+
UUID: bluetooth.New16BitUUID(0x2A01),
131+
Flags: bluetooth.CharacteristicReadPermission,
132+
Value: []byte{uint8(0x03c4 >> 8), uint8(0x03c4 & 0xff)}, /// []byte(strconv.Itoa(961)),
133+
},
134+
// {
135+
// UUID: bluetooth.CharacteristicUUIDPeripheralPreferredConnectionParameters,
136+
// Flags: bluetooth.CharacteristicReadPermission,
137+
// Value: []byte{0x02},
138+
// },
139+
140+
// // //
141+
},
142+
})
143+
144+
//v := []byte{0x85, 0x02} // 0x85, 0x02
145+
reportValue := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
146+
147+
//var reportmap bluetooth.Characteristic
148+
149+
// hid
150+
adapter.AddService(&bluetooth.Service{
151+
UUID: bluetooth.ServiceUUIDHumanInterfaceDevice,
152+
/*
153+
- hid information r
154+
- report map r
155+
- report nr
156+
- client charecteristic configuration
157+
- report reference
158+
- report nr
159+
- client charecteristic configuration
160+
- report reference
161+
- hid control point wnr
162+
*/
163+
Characteristics: []bluetooth.CharacteristicConfig{
164+
// {
165+
// UUID: bluetooth.CharacteristicUUIDHIDInformation,
166+
// Flags: bluetooth.CharacteristicReadPermission,
167+
// Value: []byte{uint8(0x0111 >> 8), uint8(0x0111 & 0xff), uint8(0x0002 >> 8), uint8(0x0002 & 0xff)},
168+
// },
169+
{
170+
//Handle: &reportmap,
171+
UUID: bluetooth.CharacteristicUUIDReportMap,
172+
Flags: bluetooth.CharacteristicReadPermission,
173+
Value: reportMap, //hidReporMap, // make([]byte, 0, len(HID_REPORT_MAP)),
174+
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
175+
print("report map")
176+
},
177+
},
178+
{
179+
180+
Handle: &reportIn,
181+
UUID: bluetooth.CharacteristicUUIDReport,
182+
Value: reportValue[:],
183+
Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicNotifyPermission,
184+
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
185+
print("report in")
186+
},
187+
},
188+
{
189+
// protocl mode
190+
UUID: bluetooth.New16BitUUID(0x2A4E),
191+
Flags: bluetooth.CharacteristicWriteWithoutResponsePermission | bluetooth.CharacteristicReadPermission,
192+
Value: []byte{uint8(1)},
193+
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
194+
print("protocol mode")
195+
},
196+
},
197+
{
198+
UUID: bluetooth.CharacteristicUUIDHIDControlPoint,
199+
Flags: bluetooth.CharacteristicWriteWithoutResponsePermission,
200+
// Value: []byte{0x02},
201+
},
202+
},
203+
})
204+
/*
205+
d.AddBleKeyboard(24, "Corne-Left", [][]keyboard.Keycode{
206+
{
207+
keyboard.Keycode(kb.KeyY), keyboard.Keycode(kb.KeyU), keyboard.Keycode(kb.KeyI), keyboard.Keycode(kb.KeyO), keyboard.Keycode(kb.KeyP), keyboard.Keycode(kb.KeyBackspace),
208+
keyboard.Keycode(kb.KeyH), keyboard.Keycode(kb.KeyJ), keyboard.Keycode(kb.KeyK), keyboard.Keycode(kb.KeyL), keyboard.Keycode(kb.KeySemicolon), jp.KeyColon,
209+
keyboard.Keycode(kb.KeyN), keyboard.Keycode(kb.KeyM), keyboard.Keycode(kb.KeyComma), keyboard.Keycode(kb.KeyPeriod), keyboard.Keycode(kb.KeyBackslash), keyboard.Keycode(kb.KeyEsc),
210+
keyboard.Keycode(kb.KeyEnter), kc.KeyMod2, keyboard.Keycode(kb.KeyModifierRightAlt), 0, 0, 0,
211+
},
212+
{
213+
keyboard.Keycode(kb.KeyY), keyboard.Keycode(kb.KeyU), keyboard.Keycode(kb.KeyI), keyboard.Keycode(kb.KeyO), keyboard.Keycode(kb.KeyP), keyboard.Keycode(kb.KeyBackspace),
214+
keyboard.Keycode(kb.KeyH), keyboard.Keycode(kb.KeyJ), keyboard.Keycode(kb.KeyK), keyboard.Keycode(kb.KeyL), keyboard.Keycode(kb.KeySemicolon), jp.KeyColon,
215+
keyboard.Keycode(kb.KeyN), keyboard.Keycode(kb.KeyM), keyboard.Keycode(kb.KeyComma), keyboard.Keycode(kb.KeyPeriod), keyboard.Keycode(kb.KeyBackslash), keyboard.Keycode(kb.KeyEsc),
216+
keyboard.Keycode(kb.KeyEnter), kc.KeyMod2, keyboard.Keycode(kb.KeyModifierRightAlt), 0, 0, 0,
217+
},
218+
{
219+
keyboard.Keycode(kb.Key6), keyboard.Keycode(kb.Key7), keyboard.Keycode(kb.Key8), keyboard.Keycode(kb.Key9), keyboard.Keycode(kb.Key0), keyboard.Keycode(kb.KeyBackspace),
220+
keyboard.Keycode(kb.KeyH), keyboard.Keycode(kb.KeyJ), keyboard.Keycode(kb.KeyK), keyboard.Keycode(kb.KeyL), keyboard.Keycode(kb.KeySemicolon), jp.KeyColon,
221+
keyboard.Keycode(kb.KeyN), keyboard.Keycode(kb.KeyM), keyboard.Keycode(kb.KeyComma), keyboard.Keycode(kb.KeyPeriod), keyboard.Keycode(kb.KeyBackslash), keyboard.Keycode(kb.KeyEsc),
222+
keyboard.Keycode(kb.KeyEnter), kc.KeyMod2, keyboard.Keycode(kb.KeyModifierRightAlt), 0, 0, 0,
223+
},
224+
})
225+
*/
226+
keyChan := make(chan KeyEvent, 5)
227+
// --------------------------------------------------
228+
229+
matrixKeyCodes := [][]keyboard.Keycode{
230+
{
231+
jp.KeyT, jp.KeyI, jp.KeyN,
232+
jp.KeyY, jp.KeyG, jp.KeyO,
233+
},
234+
}
235+
mk := d.AddMatrixKeyboard(colPins, rowPins, matrixKeyCodes)
236+
mk.SetCallback(func(layer, index int, state keyboard.State) {
237+
keyChan <- KeyEvent{layer: layer, indx: index, state: state}
238+
})
239+
240+
go func() {
241+
for {
242+
select {
243+
case x := <-keyChan:
244+
var report []byte
245+
if x.state == keyboard.PressToRelease {
246+
report = []byte{0x01,
247+
0x00,
248+
0x00,
249+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
250+
} else {
251+
// TODO: actually move special keys to mod bits in array
252+
key := matrixKeyCodes[x.layer][x.indx]
253+
report = []byte{0x01,
254+
0x00,
255+
0x00,
256+
uint8(key), 0x00, 0x00, 0x00, 0x00, 0x00}
257+
}
258+
259+
_, err := reportIn.Write(report)
260+
261+
if err != nil {
262+
println("failed to send key:", err.Error())
263+
}
264+
}
265+
}
266+
}()
267+
268+
// for Vial
269+
loadKeyboardDef()
270+
271+
d.Debug = true
272+
return d.Loop(context.Background())
273+
}

targets/sgkey-ble/vial.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "tinygo-sgkey",
3+
"vendorId": "0x2e8a",
4+
"productId": "0x000a",
5+
"matrix": {"rows": 1, "cols": 6},
6+
"layouts": {
7+
"keymap": [
8+
["0,0","0,1","0,2"],
9+
["0,3","0,4","0,5"]
10+
]
11+
}
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build tinygo && nrf52840
2+
3+
package main
4+
5+
import (
6+
"tinygo.org/x/bluetooth"
7+
)
8+
9+
var adapter = bluetooth.DefaultAdapter
10+
var reportIn bluetooth.Characteristic
11+
12+
var rx bluetooth.DeviceCharacteristic
13+
14+
var reportMap = []byte{
15+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
16+
0x09, 0x06, // USAGE (Keyboard)
17+
0xa1, 0x01, // COLLECTION (Application)
18+
0x85, 0x01, // REPORT_ID (1)
19+
0x05, 0x07, // USAGE_PAGE (Keyboard)
20+
0x19, 0x01, // USAGE_MINIMUM
21+
0x29, 0x7f, // USAGE_MAXIMUM
22+
0x15, 0x00, // LOGICAL_MINIMUM (0)
23+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
24+
0x75, 0x01, // REPORT_SIZE (1)
25+
0x95, 0x08, // REPORT_COUNT (8)
26+
0x81, 0x02, // INPUT (Data,Var,Abs)
27+
0x95, 0x01, // REPORT_COUNT (1)
28+
0x75, 0x08, // REPORT_SIZE (8)
29+
0x81, 0x01, // INPUT (Cnst,Ary,Abs)
30+
0x95, 0x06, // REPORT_COUNT (6)
31+
0x75, 0x08, // REPORT_SIZE (8)
32+
0x15, 0x00, // LOGICAL_MINIMUM (0)
33+
0x25, 0x65, // LOGICAL_MAXIMUM (101)
34+
0x05, 0x07, // USAGE_PAGE (Keyboard)
35+
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
36+
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
37+
0x81, 0x00, // INPUT (Data,Ary,Abs)
38+
0xc0, // END_COLLECTION
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import keyboard "github.com/sago35/tinygo-keyboard"
4+
5+
func loadKeyboardDef() {
6+
keyboard.KeyboardDef = []byte{
7+
0x5D, 0x00, 0x00, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x3D, 0x82, 0x80, 0x19, 0x1B, 0x9A, 0xE8, 0xB8, 0xA7, 0x6E, 0x26, 0x77, 0x5D, 0x26, 0xFB, 0x93, 0xC2, 0xE2, 0xE7, 0x46, 0x70, 0xEA, 0x6B, 0x6E, 0x80, 0x92, 0xDB, 0x54, 0x1E, 0xE3, 0x07, 0x9C, 0xFF, 0x41, 0xF2, 0x48, 0xC2, 0x87, 0x1D, 0x4E, 0x9A, 0xCC, 0xA9, 0xD1, 0x3F, 0x95, 0x70, 0xF9, 0x85, 0x73, 0x1E, 0x94, 0x50, 0xD6, 0x8A, 0xB6, 0x52, 0x79, 0x14, 0x9C, 0x03, 0xE1, 0xE2, 0x9F, 0x87, 0x76, 0xE7, 0x2B, 0x89, 0x79, 0xE2, 0x55, 0x4A, 0x7B, 0x3F, 0x96, 0x16, 0x8E, 0xDE, 0x91, 0x95, 0xD9, 0xC4, 0xDB, 0x41, 0x3D, 0x93, 0x98, 0x82, 0xAD, 0xD6, 0xB6, 0xF1, 0x43, 0x17, 0xEE, 0x6B, 0x91, 0x87, 0x20, 0x6A, 0xDE, 0xCA, 0x4F, 0xFE, 0x01, 0xC8, 0x5F, 0xA3, 0x8A, 0x1F, 0x02, 0x7B, 0xC2, 0x9A, 0x09, 0x86, 0x60, 0xAB, 0x50, 0xA9, 0x47, 0x4B, 0x05, 0xC3, 0x80, 0xB0, 0x03, 0x53, 0x9A, 0x60, 0x8F, 0x27, 0x0E, 0xD1, 0x9E, 0xC7, 0x62, 0x19, 0xAE, 0xF3, 0xDB, 0xDE, 0x42, 0x63, 0x5D, 0xCC, 0xDB, 0xB2, 0x75, 0x02, 0x60, 0xA6, 0xC9, 0x15, 0x39, 0x98, 0x7C, 0xB2, 0xB5, 0x3A, 0x2C, 0xD8, 0x42, 0x68, 0xCA, 0xEA, 0xCC, 0x99, 0x2A, 0x7E, 0x44, 0x11, 0xC8, 0xDF, 0x54, 0x5D, 0x2C, 0x7A, 0x36, 0x77, 0x89, 0xAA, 0x59, 0xF2, 0xED, 0xB9, 0x70, 0x99, 0xE3, 0x56, 0xBB, 0xB5, 0xC4, 0x39, 0x99, 0x79, 0xFD, 0xDD, 0x1A, 0xF7, 0x17, 0x04, 0x2C, 0x2F, 0x0C, 0xFF, 0xEF, 0x6D, 0x1D, 0xEA, 0xBB, 0xDE, 0x15, 0xD6, 0x76, 0xEB, 0xFD, 0xFA, 0x68, 0x3F,
8+
}
9+
}

0 commit comments

Comments
 (0)