-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
156 lines (143 loc) · 4.27 KB
/
main.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
/**
* User Buttons for DFRobot gamer:bit Players.
*/
//%
enum GamerBitPin {
//% block="X button"
P1 = <number>DAL.MICROBIT_ID_IO_P1,
//% block="Y button"
P2 = <number>DAL.MICROBIT_ID_IO_P2,
//% block="D-PAD up"
P8 = <number>DAL.MICROBIT_ID_IO_P8,
//% block="D-PAD down"
P13 = <number>DAL.MICROBIT_ID_IO_P13,
//% block="D-PAD left"
P14 = <number>DAL.MICROBIT_ID_IO_P14,
//% block="D-PAD right"
P15 = <number>DAL.MICROBIT_ID_IO_P15,
}
/**
* Trigger Events Proposed by DFRobot gamer:bit Players.
*/
//%
enum GamerBitEvent {
//% block="pressed"
Down = DAL.MICROBIT_BUTTON_EVT_DOWN,
//% block="released"
Up = DAL.MICROBIT_BUTTON_EVT_UP,
//% block="click"
Click = DAL.MICROBIT_BUTTON_EVT_CLICK,
}
/**
* Functions for DFRobot gamer:bit Players.
*/
//% weight=10 color=#DF6721 icon="\uf11b" block="gamePad"
namespace gamePad {
let PIN_INIT = 0;
export enum Vibrator {
//% blockId="V0" block="stop"
V0 = 0,
//% blockId="V1" block="Vibration"
V1 = 255,
}
export enum Intensity {
//% blockId="I0" block="stop"
I0 = 0,
//% blockId="I1" block="weak"
I1 = 100,
//% blockId="I2" block="medium"
I2 = 180,
//% blockId="I3" block="strong"
I3 = 225
}
export enum Led {
//% blockId="OFF" block="off"
OFF = 0,
//% blockId="ON" block="on"
ON = 1
}
//% shim=gamerpad::init
function init(): void {
return;
}
function PinInit(): void {
pins.setPull(DigitalPin.P1, PinPullMode.PullNone);
pins.setPull(DigitalPin.P2, PinPullMode.PullNone);
pins.setPull(DigitalPin.P8, PinPullMode.PullNone);
pins.setPull(DigitalPin.P13, PinPullMode.PullNone);
pins.setPull(DigitalPin.P14, PinPullMode.PullNone);
pins.setPull(DigitalPin.P15, PinPullMode.PullNone);
pins.setPull(DigitalPin.P0, PinPullMode.PullUp);
pins.setPull(DigitalPin.P16, PinPullMode.PullUp);
PIN_INIT = 1;
return;
}
/**
* To scan a button whether be triggered : return '1' if pressed; return'0' if not.
*/
//% weight=70
//% blockId=gamePad_keyState block="button|%button|is pressed"
//% button.fieldEditor="gridpicker" button.fieldOptions.columns=3
export function keyState(button: GamerBitPin): boolean {
if (!PIN_INIT) {
PinInit();
}
let num = false;
if (0 == pins.digitalReadPin(<number>button)) {
num = true;
}
return num;
}
/**
* Registers code to run when a DFRobot gamer:bit event is detected.
*/
//% weight=60
//% blockGap=50
//% blockId=gamePad_onEvent block="on button|%button|is %event"
//% button.fieldEditor="gridpicker" button.fieldOptions.columns=3
//% event.fieldEditor="gridpicker" event.fieldOptions.columns=3
export function onEvent(button: GamerBitPin, event: GamerBitEvent, handler: Action) {
init();
if (!PIN_INIT) {
PinInit();
}
control.onEvent(<number>button, <number>event, handler); // register handler
}
/**
* Vibrating motor switch.
*/
//% weight=50
//% blockId=gamePad_vibratorMotor block="Vibrator motor switch|%index|"
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
export function vibratorMotor(index: Vibrator): void {
vibratorMotorSpeed(<number>index);
return;
}
/**
* Vibration motor speed setting, adjustable range 0~255.
*/
//% weight=30
//% blockGap=50
//% blockId=gamePad_vibratorMotorSpeed block="Vibrator motor intensity|%degree"
//% degree.min=0 degree.max=255
export function vibratorMotorSpeed(degree: number): void {
if (!PIN_INIT) {
PinInit();
}
let num = degree * 4;
pins.analogWritePin(AnalogPin.P12, <number>num);
return;
}
/**
* LED indicator light switch.
*/
//% weight=20
//% blockId=gamePad_led block="LED|%index|"
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
export function led(index: Led): void {
if (!PIN_INIT) {
PinInit();
}
pins.digitalWritePin(DigitalPin.P16, <number>index);
}
}