-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.ts
344 lines (317 loc) · 10.2 KB
/
custom.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
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
enum controllerButtons
{
//% block="A"
A = 1,
//% block="B"
B = 516,
//% block="C"
C = 686,
//% block="D"
D = 771,
//% block="E"
E = 855,
//% block="F"
F = 821
}
enum joysticAxis
{
//% block="X"
X,
//% block="Y"
Y
}
enum joysticAnalogStates
{
//% block="Top"
Top,
//% block="Right"
Right,
//% block="Bottom"
Bottom,
//% block="Left"
Left,
//% block="Centre"
Centre
}
//% block
enum joysticDigitalStates
{
//% block="Top"
Top,
//% block="TopRight"
TopRight,
//% block="Right"
Right,
//% block="BottomRight"
BottomRight,
//% block="Bottom"
Bottom,
//% block="BottomLeft"
BottomLeft,
//% block="Left"
Left,
//% block="TopLeft"
TopLeft,
//% block="Centre"
Centre
}
/**
* Custom blocks
*/
//% weight=100 color=#8A2BE2 icon=""
namespace pxt_Valley_EFJSB_Helpers
{
let xCentre = 522;
let yCentre = 522;
let joystickThreshhold = 5
/**
* returns the current centre variable used for the Joystick
* on the specified Axis, offset from the centre
* Return of -1 (zero) indicates either an error
* @param axis x for left/right, y for top/bottom
*/
//% advanced=true
//% blockId=getCurrentCentreValue block="Get the the current centre variable of %axis axis"
export function getCurrentCentreValue(axis: joysticAxis): number
{
switch (axis) {
case joysticAxis.X: return xCentre;
case joysticAxis.Y: return yCentre;
default: return -1;
}
}
/**
* Returns the current 'threshhold' variable used for the Joystick
* This is how far from centre the joystick needs to report
* before reporting not centred.
* Handy for Joysticks that twitch or are loose
*/
//% advanced=true
//% blockId=getCurrentJoystickThreshhold block="Get the the current 'threshhold' variable"
export function getCurrentJoystickGive(): number
{
return joystickThreshhold;
}
/**
* Sets the current 'threshhold' variable used for the Joystick
* This is how far from centre the joystick needs to report
* before reporting not centred.
* Handy for Joysticks that twitch or are loose
*/
//% advanced=true
//% newThreshhold.min=0 newGive.max=1023
//% blockId=setCurrentJoystickThreshhold block="Set the the current 'threshhold' variable"
export function setCurrentJoystickGive(newThreshhold : number): void
{
if (newThreshhold >= 0 &&
newThreshhold <= 1023)
{
joystickThreshhold = newThreshhold;
}
}
/**
* Calibrates the Joystick
* using the current positions on x and y axis.
* Joystick should be free of input when this is called.
*/
//% advanced=true
//% blockId=calibrateJoystick block="Calibrate the Joystick with current position as centre"
export function calibrateJoystick(): void
{
xCentre = pins.analogReadPin(AnalogPin.P0);
yCentre = pins.analogReadPin(AnalogPin.P1);
}
/**
* Get the current position of the Joystick,
* on the specified Axis from 0 -> 1023
* Reads left to right, bottom to top.
* (ie; bottom left is 0,0)
* @param axis x for left/right, y for top/bottom
*/
//% advanced=true
//% blockId=getRawJoystickValue block="Get raw value of Joystick on %axis axis"
export function getRawJoystickValue(axis: joysticAxis): number
{
switch (axis)
{
case joysticAxis.X: return pins.analogReadPin(AnalogPin.P0);
case joysticAxis.Y: return pins.analogReadPin(AnalogPin.P1);
default: return 0;
}
}
/**
* Get the current position of the Joystick,
* on the specified Axis, offset from the centre
* Left and Bottom values are negative,
* Right and Top values are positive,
* @param axis x for left/right, y for top/bottom
*/
//% advanced=true
//% blockId=getOffsetJoystickValue block="Get the Delta value of Joystick on %axis axis"
export function getOffsetJoystickValue(axis: joysticAxis): number
{
switch (axis)
{
case joysticAxis.X: return pins.analogReadPin(AnalogPin.P0) - xCentre;
case joysticAxis.Y: return pins.analogReadPin(AnalogPin.P1) - yCentre;
default: return 0;
}
}
/**
* Is the joystick registering the requested direction
* This is tested against the Centre position, set in Calibration
* Note, these are not exclusive. If joystick is at 0,0 then
* both Left and Bottom will return true
* @param state requested direction to check
*/
//% blockId=isJoystickPointing block="Is the joystick pointing %state of centre"
export function isJoystickPointing(state : joysticAnalogStates): boolean
{
let p0 = pins.analogReadPin(AnalogPin.P0);
let p1 = pins.analogReadPin(AnalogPin.P1);
switch (state)
{
case joysticAnalogStates.Left: return p0 < xCentre - joystickThreshhold;
case joysticAnalogStates.Right: return p0 > xCentre + joystickThreshhold;
case joysticAnalogStates.Top: return p1 > yCentre + joystickThreshhold;
case joysticAnalogStates.Bottom: return p1 < yCentre - joystickThreshhold;
case joysticAnalogStates.Centre: return (p1 >= yCentre - joystickThreshhold &&
p1 <= yCentre + joystickThreshhold &&
p0 >= xCentre - joystickThreshhold &&
p0 <= xCentre + joystickThreshhold);
default: return false;
}
}
/**
* What is the DPad (Digital) direction of the joystick
*/
//% blockId=getDigitalJoystickPosition block="Get the current Digital DPad value of the Joystick"
export function getDigitalJoystickPosition(): joysticDigitalStates
{
let p0 = pins.analogReadPin(AnalogPin.P0);
let p1 = pins.analogReadPin(AnalogPin.P1);
let digitalThreshhold = 150;
let left, right, up, down = false;
left = p0 < xCentre - digitalThreshhold;
right = p0 > xCentre + digitalThreshhold;
up = p1 > yCentre + digitalThreshhold;
down = p1 < yCentre - digitalThreshhold;
if (left)
{
if (up)
{
return joysticDigitalStates.TopLeft;
}
else
{
if (down)
{
return joysticDigitalStates.BottomLeft;
}
else
{
return joysticDigitalStates.Left;
}
}
}
else
{
if (right)
{
if (up)
{
return joysticDigitalStates.TopRight;
}
else
{
if (down)
{
return joysticDigitalStates.BottomRight;
}
else
{
return joysticDigitalStates.Right;
}
}
}
else
{
if (up)
{
return joysticDigitalStates.Top;
}
else
{
if (down)
{
return joysticDigitalStates.Bottom;
}
else
{
return joysticDigitalStates.Centre;
}
}
}
}
return joysticDigitalStates.Centre;
}
/**
* Convert the numeric points of digital joystick position to arrows
* @joystickState (0-8 clockwise from top) to a standard Micro:Bit ArrowName.
* @joystickState
*/
//% joystickState.min=0 joystickState.max=8
//% blockId=joystickPositionAsArrow block="Convert digital joystick state %joystickState to ArrowName"
export function joystickPositionAsArrow(joystickState: number): ArrowNames
{
switch (joystickState)
{
case joysticDigitalStates.Top: return ArrowNames.North; break;
case joysticDigitalStates.TopRight: return ArrowNames.NorthEast; break;
case joysticDigitalStates.Right: return ArrowNames.East; break;
case joysticDigitalStates.BottomRight: return ArrowNames.SouthEast; break;
case joysticDigitalStates.Bottom: return ArrowNames.South; break;
case joysticDigitalStates.BottomLeft: return ArrowNames.SouthWest; break;
case joysticDigitalStates.Left: return ArrowNames.West; break;
case joysticDigitalStates.TopLeft: return ArrowNames.NorthWest; break;
default: return null; break;
}
}
/**
* Check if a specific button is currently pressed.
* This uses the current analog value on Pin 2 of the Microbit.
* This is how the ElecFreaks Joystick:Bit V2.0 (the white one with 6 buttons) works.
* For the 4 button version, there is already a package. That one maps to pins which is
* simpler and probably better in most situations
* @param button the button, a-f, which map to the analog values.
*/
//% blockId=IsButtonPressed block="is button %button pressed"
export function IsButtonPressed(button: controllerButtons): boolean
{
let p = pins.analogReadPin(AnalogPin.P2);
if (p >= button - 2 &&
p <= button + 2)
{
return true;
}
else
{
return false;
}
}
/*
pins.setEvents(DigitalPin.P2, PinEventType.Edge);
control.onEvent(
EventBusSource.MICROBIT_ID_IO_P2,
EventBusValue.MICROBIT_PIN_EVT_RISE,
() => doeventstuff(),
EventFlags.QueueIfBusy
);
//% blockId=CustomButtonPressed block="button pressed"
export function doeventstuff() :void
{
basic.showNumber(pins.analogReadPin(AnalogPin.P2));
pause(100);
basic.clearScreen();
}*/
}