forked from elecfreaks/pxt-joystickbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystickbit.ts
109 lines (77 loc) · 2.56 KB
/
joystickbit.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
/**
* Use this file to define custom functions and blocks.
* Read more at https://makecode.microbit.org/blocks/custom
*/
//% weight=0 color=#0fbc11 icon="\uf11b" block="Joystickbit"
namespace joystickbit {
export enum JoystickBitPin {
//% block="C"
P12 = DAL.MICROBIT_ID_IO_P12,
//% block="D"
P13 = DAL.MICROBIT_ID_IO_P13,
//% block="E"
P14 = DAL.MICROBIT_ID_IO_P14,
//% block="F"
P15 = DAL.MICROBIT_ID_IO_P15
}
export enum rockerType {
//% block="X"
X,
//% block="Y"
Y
}
export enum ButtonType {
//% block="pressed"
down = PulseValue.High,
//% block="released"
up = PulseValue.Low
}
/**
* initialization joystick:bit
*/
//% blockId=initJoystickBit block="initialization joystick:bit"
export function initJoystickBit(): void {
pins.digitalWritePin(DigitalPin.P0, 0)
pins.setPull(DigitalPin.P12, PinPullMode.PullUp)
pins.setPull(DigitalPin.P13, PinPullMode.PullUp)
pins.setPull(DigitalPin.P14, PinPullMode.PullUp)
pins.setPull(DigitalPin.P15, PinPullMode.PullUp)
pins.digitalWritePin(DigitalPin.P16, 1)
}
/**
* get Button
*/
//% blockId=getButton block="button %button is pressed"
export function getButton(button: JoystickBitPin): boolean {
return (pins.digitalReadPin(<number>button) == 0 ? true : false)
}
/**
* Registers code to run when a joystick:bit event is detected.
*/
//% blockId=onButtonEvent block="on button %button|is %event" blockExternalInputs=false
export function onButtonEvent(button: JoystickBitPin, event: ButtonType, handler: Action): void {
pins.onPulsed(<number>button, <number>event, handler);
}
/**
* get rocker value
* @param rocker describe parameter here, eg: 1
*/
//% blockId=getRockerValue block="rocker value of %rocker"
export function getRockerValue(rocker: rockerType): number {
switch (rocker) {
case rockerType.X: return pins.analogReadPin(AnalogPin.P1);
case rockerType.Y: return pins.analogReadPin(AnalogPin.P2);
default: return 0;
}
}
/**
* vibration motor
* @param time describe parameter here, eg: 100
*/
//% blockId=Vibration_Motor block="motor vibrate for %time ms"
export function Vibration_Motor(time: number): void {
pins.digitalWritePin(DigitalPin.P16, 0)
basic.pause(time)
pins.digitalWritePin(DigitalPin.P16, 1)
}
}