-
Notifications
You must be signed in to change notification settings - Fork 16
/
WSJoyStick.ts
executable file
·179 lines (162 loc) · 5 KB
/
WSJoyStick.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
/*****************************************************************************
* | File : WSJoyStick
* | Author : Waveshare team
* | Function : Contorl JoyStick
* | Info :
*----------------
* | This version: V1.0
* | Date : 2018-02-06
* | Info : Basic version
*
******************************************************************************/
enum DIR {
NONE = 0,
U = 1,
D = 2,
L = 3,
R = 4,
U_L = 5,
U_R = 6,
D_L = 7,
D_R = 8
}
enum KEY {
P = 0,
A = 1,
B = 2,
C = 3,
D = 4,
E = 5,
F = 6,
}
let JoyStick_P = DigitalPin.P8;
let JoyStick_X = AnalogPin.P1;
let JoyStick_Y = AnalogPin.P2;
let KEY_A = DigitalPin.P5;
let KEY_B = DigitalPin.P11;
let KEY_C = DigitalPin.P15;
let KEY_D = DigitalPin.P14;
let KEY_E = DigitalPin.P13;
let KEY_F = DigitalPin.P12;
/**
* Operational remote JoyStick function
*/
//% weight=20 color=#3333FF icon="\uf11b"
namespace WSJoyStick {
let Read_X = 0, Read_Y = 0;
//% blockId==JoyStickInit block="JoyStickInit"
//% weight=100
export function JoyStickInit(): void {
pins.setPull(JoyStick_P, PinPullMode.PullUp);
pins.setPull(KEY_A, PinPullMode.PullUp);
pins.setPull(KEY_B, PinPullMode.PullUp);
pins.setPull(KEY_C, PinPullMode.PullUp);
pins.setPull(KEY_D, PinPullMode.PullUp);
pins.setPull(KEY_E, PinPullMode.PullUp);
pins.setPull(KEY_F, PinPullMode.PullUp);
//10 bits of AD conversion chip,max = 1024
Read_X = pins.analogReadPin(JoyStick_X);
Read_Y = pins.analogReadPin(JoyStick_Y);
}
//% blockId==Listen_Key block="Key %pin |Press"
//% weight=90
export function Listen_Key(pin: KEY): boolean {
let Val = 2;
//Read pin
if (pin == KEY.P) {
Val = pins.digitalReadPin(JoyStick_P);
} else if (pin == KEY.A) {
Val = pins.digitalReadPin(KEY_A);
} else if (pin == KEY.B) {
Val = pins.digitalReadPin(KEY_B);
} else if (pin == KEY.C) {
Val = pins.digitalReadPin(KEY_C);
} else if (pin == KEY.D) {
Val = pins.digitalReadPin(KEY_D);
} else if (pin == KEY.E) {
Val = pins.digitalReadPin(KEY_E);
} else {
Val = pins.digitalReadPin(KEY_F);
}
//registerWithDal((int)pin, MICROBIT_KEY_EVT_CLICK, body);
//To determine the value
if (Val == 0) {
return true;
} else {
return false;
}
}
//% blockId==onKey block="Key %pin |Press"
//% weight=80
export function onKey(pin: KEY, body: Action): void {
let Pin = 0;
//Read pin
if (pin == KEY.P) {
Pin = JoyStick_P;
} else if (pin == KEY.A) {
Pin = KEY_A;
} else if (pin == KEY.B) {
Pin = KEY_B;
} else if (pin == KEY.C) {
Pin = KEY_C;
} else if (pin == KEY.D) {
Pin = KEY_D;
} else if (pin == KEY.E) {
Pin = KEY_E;
} else {
Pin = KEY_F;
}
pins.onPulsed(Pin, PulseValue.Low, body);
}
//% blockId==Listen_Dir block="DIR Dir %pin "
//% weight=70
export function Listen_Dir(Dir: DIR): boolean {
let Get_Dir = DIR.NONE;
let New_X = pins.analogReadPin(AnalogPin.P1);
let New_Y = pins.analogReadPin(AnalogPin.P2);
let Right = New_X - Read_X;
let Left = Read_X - New_X;
let Up = New_Y - Read_Y;
let Down = Read_Y - New_Y;
let Dx = Math.abs(Read_X - New_X);
let Dy = Math.abs(New_Y - Read_Y);
let Precision = 150; //0.5v
if (Right > Precision && Dy < Precision) {
Get_Dir = DIR.R;
} else if (Left > Precision && Dy < Precision) {
Get_Dir = DIR.L;
} else if (Up > Precision && Dx < Precision) {
Get_Dir = DIR.U;
} else if (Down > Precision && Dx < Precision) {
Get_Dir = DIR.D;
} else if (Right > Precision && Up > Precision) {
Get_Dir = DIR.U_R;
} else if (Right > Precision && Down > Precision) {
Get_Dir = DIR.D_R;
} else if (Left > Precision && Up > Precision) {
Get_Dir = DIR.U_L;
} else if (Left > Precision && Down > Precision) {
Get_Dir = DIR.D_L;
} else {
Get_Dir = DIR.NONE;
}
//To determine the value
if (Get_Dir == Dir) {
return true;
} else {
return false;
}
}
/**
* Plays a tone through pin ``P0`` for the given duration.
* @param frequency pitch of the tone to play in Hertz (Hz)
* @param ms tone duration in milliseconds (ms)
*/
//% help=music/play-tone weight=60
//% blockId=PlayMusic block="Play |Music %note=device_note|for %duration=device_beat" blockGap=8
//% parts="headphone"
//% useEnumVal=1
export function PlayMusic(frequency: number, ms: number): void {
pins.analogPitch(frequency, ms);
}
}