-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
542 lines (487 loc) · 13.8 KB
/
main.ts
File metadata and controls
542 lines (487 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
* Language references
* https://makecode.com/blog/github-packages
* https://makecode.com/language
* https://makecode.microbit.org/blocks/custom
*/
//% weight=100 color=#F1BC03 icon="\uf11b" block="GameControllerizer"
namespace gamecontrollerizer {
const EXTERNAL_BUTTON_EVENT_ID: number = 12345;
//////////////////////////////////////////////////////////////////////////////////////////
// Types
//////////////////////////////////////////////////////////////////////////////////////////
export enum InputMode {
//% block="Hold"
Hold = -1,
//% block="Push"
Push = 3
}
export enum ButtonInputMode {
//% block="Hold"
Hold,
//% block="Push"
Push,
//% block="Release"
Release,
}
export enum GamepadDpad {
//% block="⬋ LB"
LeftBottom = 1,
//% block="⬇ BT"
Bottom = 2,
//% block="⬊ RB"
RightButtom = 3,
//% block="⬅ LM"
LeftMiddle = 4,
//% block="● N"
Neutral = 5,
//% block="➡ RM"
RightMiddle = 6,
//% block="⬉ LT"
LeftTop = 7,
//% block="⬆ TP"
Top = 8,
//% block="⬈ RT"
RightTop = 9
}
export enum GamepadButton {
//% block="Button-0"
B0 = 0,
//% block="Button-1"
B1 = 1,
//% block="Button-2"
B2 = 2,
//% block="Button-3"
B3 = 3,
//% block="Button-4"
B4 = 4,
//% block="Button-5"
B5 = 5,
//% block="Button-6"
B6 = 6,
//% block="Button-7"
B7 = 7,
//% block="Button-8"
B8 = 8,
//% block="Button-9"
B9 = 9,
//% block="Button-10"
B10 = 10,
//% block="Button-11"
B11 = 11,
//% block="None"
None = -1
}
export enum GamepadStick {
//% block="Left stick"
Left = 0,
//% block="Right stick"
Right = 1
}
export enum GamepadStickAxis {
x = 0,
y = 1
}
export enum ExtConnector {
//% block="microbit ring"
MB,
//% block="Grove P0/P14"
GROVE0,
//% block="Grove P1/P15"
GROVE1,
//% block="Grove P2/P16"
GROVE2
}
export enum ExButton {
//% block="B0"
EB0 = 0x01,
//% block="B1"
EB1 = 0x02,
//% block="B2"
EB2 = 0x04,
//% block="B3"
EB3 = 0x08,
}
export enum InputConfigTarget {
//% block="DPAD"
DPAD = 0x01,
//% block="Left stick"
LeftStick = 0x02,
//% block="Right stick"
RightStick = 0x04
}
export enum InputConfigMap {
//% block="normal"
Normal = 0x00,
//% block="reverse"
Reverse = 0x01,
//% block="the other"
Theother = 0x02
}
//////////////////////////////////////////////////////////////////////////////////////////
// Commands
//////////////////////////////////////////////////////////////////////////////////////////
abstract class Cmd {
protected dur: number;
protected bytes: number[];
constructor() { }
toBytes(): number[] {
return this.bytes;
}
}
/**
* Cmd:0xC0
*/
export class DpadCmd extends Cmd {
/**
*
* @param dpad
* @param dur
*/
constructor(dpad: GamepadDpad, dur: number) {
super();
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = 0xC0;
tBuf[1] = dur;
tBuf[2] = dpad;
this.bytes = tBuf;
}
}
//
/**
* Cmd:0xC1
*/
export class ButtonCmd extends Cmd {
/**
*
* @param buttons
* @param dur
*/
constructor(buttons: GamepadButton[], dur: number) {
super();
let tBtn: number = 0x0000;
for (let i = 0; i < buttons.length; i++) {
if (buttons[i] == GamepadButton.None)
continue;
tBtn |= (0x001 << buttons[i]);
}
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = 0xC1;
tBuf[1] = dur;
tBuf[2] = tBtn;
tBuf[3] = tBtn >> 8;
this.bytes = tBuf;
}
}
/**
* Cmd:0x02
*/
export class ButtonSetCmd extends ButtonCmd {
/**
*
* @param buttons
* @param dur
*/
constructor(buttons: GamepadButton[], dur: number) {
super(buttons, dur);
this.bytes[0] = 0xC2;
}
}
/**
* Cmd:0xC3
*/
export class ButtonUnsetCmd extends ButtonCmd {
/**
*
* @param buttons
* @param dur
*/
constructor(buttons: GamepadButton[], dur: number) {
super(buttons, dur);
this.bytes[0] = 0xC3;
}
}
/**
* Cmd:0xC4-7
*/
export class StickAxisCmd extends Cmd {
/**
*
* @param id
* @param axis
* @param value
* @param dur
*/
constructor(id: GamepadStick, axis: GamepadStickAxis, value: number, dur: number) {
super();
let tCmd = 0xC4 | (id << 1) | axis;
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = tCmd;
tBuf[1] = dur;
tBuf[2] = value;
this.bytes = tBuf;
}
}
/**
* Cmd:0xC8-9
*/
export class StickCmd extends Cmd {
/**
*
* @param id
* @param value_x
* @param value_y
* @param dur
*/
constructor(id: GamepadStick, value_x: number, value_y: number, dur: number) {
super();
let tCmd = 0xC8 | id;
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = tCmd;
tBuf[1] = dur;
tBuf[2] = value_x;
tBuf[3] = value_y;
this.bytes = tBuf;
}
}
/**
* Stick position
*/
export class StickPosition {
x: number;
y: number;
/**
*
* @param x [-127:127]
* @param y [-127:127]
*/
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
/**
* Cmd:0xCD
*/
export class InterruptCmd extends Cmd {
/**
*/
constructor() {
super();
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = 0xCD;
this.bytes = tBuf;
}
}
/**
* Cmd:0xCE
*/
export class InputConfigCmd extends Cmd {
/**
* @param target
* @param map
*/
constructor(ctarget: InputConfigTarget, cmap: InputConfigMap) {
super();
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = 0xCE;
tBuf[1] = InputMode.Hold;
tBuf[2] = ctarget;
tBuf[3] = cmap;
this.bytes = tBuf;
}
}
/**
* Cmd:0xCF
*/
export class DurationCmd extends Cmd {
/**
* @param dur
*/
constructor(dur: number) {
super();
let tBuf: number[] = [0, 0, 0, 0];
tBuf[0] = 0xCF;
tBuf[1] = dur;
this.bytes = tBuf;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// Functions
//////////////////////////////////////////////////////////////////////////////////////////
/**
* Connect G.C. module
* @param port
*/
//% blockId="connectGc"
//% block="[G.C.] Connect G.C. to %cn"
export function connectGc(cn: ExtConnector): void {
switch (cn) {
case ExtConnector.GROVE0:
serial.redirect(SerialPin.P0, SerialPin.P14, BaudRate.BaudRate115200);
break;
case ExtConnector.GROVE1:
serial.redirect(SerialPin.P1, SerialPin.P15, BaudRate.BaudRate115200);
break;
case ExtConnector.GROVE2:
serial.redirect(SerialPin.P2, SerialPin.P16, BaudRate.BaudRate115200);
break;
case ExtConnector.MB:
serial.redirect(SerialPin.P0, SerialPin.P1, BaudRate.BaudRate115200);
break;
}
// event setting
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
let tMsgId: number = serial.readBuffer(2)[0];
control.raiseEvent(EXTERNAL_BUTTON_EVENT_ID, tMsgId);
})
}
/**
* Send one command to G.C.
* @param cmd
*/
export function sendToGc(cmd: Cmd): void {
const tBuf = pins.createBufferFromArray(cmd.toBytes());
serial.writeBuffer(tBuf);
}
//////////////////////////////////////////////////////////////////////////////////////////
/**
* Operate Gamepad(digital)
* @param dpad
* @param button
* @param mode
*/
//% blockId="operateGamepadDigitally"
//% block="[G.C.] Digital gamepad : Dpad %dpad| Button %button|, then %mode"
//% advanced=true
export function operateGamepadDigitally(
dpad: GamepadDpad = GamepadDpad.Neutral,
button: GamepadButton = GamepadButton.B0,
mode: InputMode = InputMode.Hold): void {
let dur = mode;
let tCmd0 = new DpadCmd(dpad, 0);
let tCmd1 = new ButtonCmd([button], dur);
sendToGc(tCmd0);
sendToGc(tCmd1);
return;
}
/**
* Operate Gamepad(analog)
* @param dpad
* @param button
* @param stick0
* @param stick1
* @param mode
*/
//% blockId="operateGamepadAnalogly"
//% block="[G.C.] Analog gamepad : Dpad %dpad| Button %button| Stick0 %stick0| Stick1 %stick1|, then %mode"
//% advanced=true
export function operateGamepadAnalogly(
dpad: GamepadDpad = GamepadDpad.Neutral,
button: GamepadButton = GamepadButton.B0,
spos0: StickPosition = newStickPosition(),
spos1: StickPosition = newStickPosition(),
mode: InputMode = InputMode.Hold): void {
let dur = mode;
let tCmd0 = new DpadCmd(dpad, 0);
let tCmd1 = new ButtonCmd([button], 0);
let tCmd2 = new StickCmd(GamepadStick.Left, spos0.x, spos0.y, 0);
let tCmd3 = new StickCmd(GamepadStick.Right, spos1.x, spos1.y, dur);
sendToGc(tCmd0);
sendToGc(tCmd1);
sendToGc(tCmd2);
sendToGc(tCmd3);
return;
}
//////////////////////////////////////////////////////////////////////////////////////////
//% blockId="operateDpad"
//% block="[G.C.] Change Dpad to %dpad|, then %mode"
export function operateDpad(
dpad: GamepadDpad = GamepadDpad.Neutral,
mode: InputMode = InputMode.Hold): void {
let dur = mode;
let tCmd = new DpadCmd(dpad, dur);
sendToGc(tCmd);
return;
}
//% blockId="operateButtonIndividually"
//% block="[G.C.] Pick %button |, then %mode"
export function operateButtonIndividually(
button: GamepadButton = GamepadButton.B0,
mode: ButtonInputMode = ButtonInputMode.Hold): void {
let tCmd: Cmd;
switch (mode) {
case ButtonInputMode.Hold:
tCmd = new ButtonSetCmd([button], InputMode.Hold);
break;
case ButtonInputMode.Push:
tCmd = new ButtonSetCmd([button], InputMode.Push);
break;
case ButtonInputMode.Release:
tCmd = new ButtonUnsetCmd([button], InputMode.Hold);
break;
}
sendToGc(tCmd);
return;
}
//% blockId="operateStick"
//% block="[G.C.] Change %stick to %spos |,then %mode"
export function operateStick(
stick: GamepadStick = GamepadStick.Left,
spos: StickPosition = newStickPosition(0, 0),
mode: InputMode = InputMode.Hold): void {
let dur = mode;
let tCmd = new StickCmd(stick, spos.x, spos.y, dur);
sendToGc(tCmd);
return;
}
/**
* Create new Axis state
* @param x [-127:127] value of X-axis
* @param y [-127:127] value of Y-axis
*/
//% blockId="newStickPosition"
//% block="[G.C.] x %x |y %y"
//% x.min=-127 x.max=127
//% y.min=-127 y.max=127
export function newStickPosition(x: number = 0, y: number = 0): StickPosition {
return new StickPosition(x, y);
}
//% blockId="operateInterrupt"
//% block="[G.C.] Interrupt command inputs"
//% advanced=true
export function operateInterrupt(): void {
let tCmd = new InterruptCmd();
sendToGc(tCmd);
return;
}
//% blockId="operateInputConfig"
//% block="[G.C.] Config %ctarget |L-R input to %cmap |mode"
//% advanced=true
export function operateInputConfig(
ctarget: InputConfigTarget = InputConfigTarget.DPAD,
cmap: InputConfigMap = InputConfigMap.Normal): void {
let tCmd = new InputConfigCmd(ctarget, cmap);
sendToGc(tCmd);
return;
}
//% blockId="operateDuration"
//% block="[G.C.] Wait %dur| frames"
//% dur.min=1 dur.max=127
export function operateDuration(dur: number = 3): void {
let tCmd = new DurationCmd(dur);
sendToGc(tCmd);
return;
}
//////////////////////////////////////////////////////////////////////////////////////////
/**
* Do something when a external button on G.C.module is pressed
* @param eb button id
* @param handler code to run
*/
//% blockId="onExternalButtonPressed"
//% block="[G.C.] on %eb |is pressed"
export function onExternalButtonPressed(eb: ExButton = ExButton.EB0, handler: () => void) {
control.onEvent(EXTERNAL_BUTTON_EVENT_ID, eb, handler);
}
}