forked from MiroChao/pxt-BitPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitplayer.ts
190 lines (174 loc) · 5.28 KB
/
bitplayer.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
/*****************************************************************************
* | Description : BitPlayer extension for micro:bit
* | Developer : CH Makered
* | More Info : http://chmakered.com/
******************************************************************************/
enum Joystick {
//% block=" UpLeft"
UpLeft,
//% block=" ↑ Up"
Up,
//% block=" UpRight"
UpRight,
//% block=" ← Left"
Left,
//% block=" Middle"
Middle,
//% block=" → Right"
Right,
//% block=" LowerLeft"
LowerLeft,
//% block=" ↓ Down"
Down,
//% block=" LowerRight"
LowerRight
}
enum BitPlayerKey {
//% block="A"
key_A = <number>DAL.MICROBIT_ID_IO_P5,
//% block="B"
key_B = <number>DAL.MICROBIT_ID_IO_P11,
//% block="C"
key_C = <number>DAL.MICROBIT_ID_IO_P13,
//% block="D"
key_D = <number>DAL.MICROBIT_ID_IO_P14,
//% block="L"
key_L = <number>DAL.MICROBIT_ID_IO_P15,
//% block="R"
key_R = <number>DAL.MICROBIT_ID_IO_P16,
}
/**
* Button Events of BitPlayer
*/
//%
enum BitPlayerKeyEvent {
//% block="click"
click = DAL.MICROBIT_BUTTON_EVT_CLICK,
//% block="pressed"
pressed = DAL.MICROBIT_BUTTON_EVT_DOWN,
//% block="released"
released = DAL.MICROBIT_BUTTON_EVT_UP,
}
/**
* Provides access to BitPlayer blocks for micro: bit functionality.
*/
//% color=190 icon="\uf126" block= "BitPlayer"
//% groups="['Analog', 'Digital', 'I2C', 'Grove Modules']"
namespace BitPlayer {
let posi_init = 0;
function InitialPosition(): void {
posi_init = 1;
return;
}
/**
*
*/
//% shim=bitplayer::init
function init(): void {
return;
}
/**
* Do something when a key is pressed, releassed or clicked
*/
//% blockId=OnKey
//% block="on key $key| is $keyEvent"
export function OnKey(key: BitPlayerKey, keyEvent: BitPlayerKeyEvent, handler: Action) {
if (!posi_init) {
InitialPosition();
}
init();
control.onEvent(<number>key, <number>keyEvent, handler); // register handler
}
/**
* Get the key state (pressed or not)
* @param key the pin that acts as a button
*/
//% blockId=KeyPressed
//% block="key $key| is pressed"
export function KeyPressed(key: BitPlayerKey): boolean {
if (!posi_init) {
InitialPosition();
}
const pin = <DigitalPin><number>key;
pins.setPull(pin, PinPullMode.PullUp);
return pins.digitalReadPin(<DigitalPin><number>key) == 0;
}
/**
* turn on of off the vibration motor
*/
//% blockId=SetMotor
//% block="vibration $on|"
//% on.shadow="toggleOnOff"
//% on.defl="true"
//% weight=45
export function SetMotor(on: boolean) {
if (on) {
pins.digitalWritePin(DigitalPin.P8, 1);
}else{
pins.digitalWritePin(DigitalPin.P8, 0);
}
}
const x0 = 500;
const y0 = 500;
const d0 = 250;
/**
* Get the joystick state
* @param position the current position of joystick
*/
//% blockId=OnJoystick
//% block="joystick $position|"
//% position.fieldEditor="gridpicker"
//% position.fieldOptions.columns=3
//% weight = 40
export function OnJoystick(position: Joystick): boolean {
let x = pins.analogReadPin(AnalogPin.P1) - x0;
let y = pins.analogReadPin(AnalogPin.P2) - y0;
let d = Math.round(Math.sqrt(Math.abs(x * x) + Math.abs(y * y)));
const value1 = Math.round(d * 0.38); //0.38 is the value of sin 22.5°
const value2 = Math.round(d * 0.92); //0.92 is the value of sin 67.5°
let getPosition = Joystick.Middle;
if (d > d0) {
if (x > 0 && y > 0) { // (x,y) is at top right area
if (y > value2) {
getPosition = Joystick.Up;
} else if (y < value1) {
getPosition = Joystick.Right;
} else {
getPosition = Joystick.UpRight;
}
} else if (x > 0 && y < 0) { // (x,y) is at bot right area
if (x > value2) {
getPosition = Joystick.Right;
} else if (x < value1) {
getPosition = Joystick.Down;
} else {
getPosition = Joystick.LowerRight;
}
} else if (x < 0 && y < 0) { // (x,y) is at bot left area
y = Math.abs(y);
if (y > value2) {
getPosition = Joystick.Down;
} else if (y < value1) {
getPosition = Joystick.Left;
} else {
getPosition = Joystick.LowerLeft;
}
} else if (x < 0 && y > 0) { // (x,y) is at top left area
if (y > value2) {
getPosition = Joystick.Up;
} else if (y < value1) {
getPosition = Joystick.Left;
} else {
getPosition = Joystick.UpLeft;
}
}
} else {
getPosition = Joystick.Middle;
}
if (getPosition == position) {
return true;
} else {
return false;
}
}
}