forked from Vegz78/micro-bit-game-pad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
122 lines (98 loc) · 2.93 KB
/
test.ts
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
// tests go here; this will not be compiled when this package is used as a library
input.onButtonPressed(Button.A, () => {
bluetooth.pressJoystickButton(JoystickButton.JOYSTICK_BUTTON_1);
basic.showString("A");
})
// Two input work only with this pxt.json config
// "yotta": {
// "config": {
// "microbit-dal": {
// "bluetooth": {
// "dfu_service": 0,
// "event_service": 0,
// "device_info_service": 0,
// "security_level": "SECURITY_MODE_ENCRYPTION_NO_MITM"
// },
// "gatt_table_size": "0x700"
// }
// }
// }
input.onButtonPressed(Button.B, () => {
bluetooth.pressJoystickButton(JoystickButton.JOYSTICK_BUTTON_2);
basic.showString("B");
})
basic.showString("OK")
bluetooth.startJoystickService()
function normalize(v:number, limit:number) {
// Make range -800 to 800
const RANGE_MIN = -limit;
const RANGE_MAX = limit;
const DEADZONE = 25;
const SCALING_FACTOR = RANGE_MAX/127
if (Math.abs(v) < DEADZONE) {
v = 0;
} else {
if (v > 0) {
v -= DEADZONE;
} else {
v += DEADZONE;
}
}
if (v < RANGE_MIN) {
v = RANGE_MIN;
}
if (v > RANGE_MAX) {
v = RANGE_MAX;
}
v -= v%10;
v /= SCALING_FACTOR;
return Math.floor(v);
// if (v < -800) {
// v = -800;
// }
// if (v > 800) {
// v = 800;
// }
// v /= 6.29;
// return v;
}
// function movingAverage(average:number, sample:number) {
// return average - average / 8 + sample / 8;
// }
let xs = 0;
let ys = 0;
let zs = 0;
// let xIndex = 0;
// let yIndex = 0;
// let zIndex = 0;
basic.forever(function () {
xs = xs - (xs >> 3) + (input.acceleration(Dimension.X) >> 3);
ys = ys - (ys >> 3) + (input.acceleration(Dimension.Y) >> 3);
zs = zs - (zs >> 3) + (input.acceleration(Dimension.Z) >> 3);
xs -= xs%10;
ys -= ys%10;
zs -= zs%10;
let x = normalize(xs, 550);
let y = normalize(ys, 10);
let z = normalize(zs, 350);
// zs = movingAverage(zs, z);
// // ys = movingAverage(ys, y);
// // zs = movingAverage(zs, z);
// serial.writeLine(z.toString() + " " + zs.toString());
serial.writeLine(Math.floor(x).toString() + "," + Math.floor(z).toString() );
// xs[xIndex] = x;
// ys[yIndex] = y;
// zs[zIndex] = z;
// xIndex = (xIndex + 1);
// if (xIndex >= 8) {
// xIndex = 0;
// }
// yIndex = (yIndex + 1) % ys.length;
// zIndex = (zIndex + 1) % zs.length;
// let x_average = xs.reduce((a, b) => a + b, 0) / xs.length;
// let y_average = ys.reduce((a, b) => a + b, 0) / ys.length;
// let z_average = zs.reduce((a, b) => a + b, 0) / zs.length;
// bluetooth.sendMovement(x_average, y_average, z_average);
bluetooth.sendMovement(Math.floor(x), Math.floor(y), Math.floor(z));
control.waitMicros(4);
})