forked from microbit-apps/pxt-arcadeshield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shieldhelpers.ts
266 lines (230 loc) · 7.31 KB
/
shieldhelpers.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
namespace shieldhelpers {
type ArcadeButtonId = "left" | "right" | "up" | "down" | "a" | "b" | "menu"
interface ArcadeShieldMessage {
type: "initialize" | "show-image" | "set-brightness" | "set-palette" | "button-down" | "button-up" | "display-on" | "display-off"
runId: any
}
interface InitializeMessage extends ArcadeShieldMessage {
type: "initialize"
}
interface ShowImageMessage extends ArcadeShieldMessage {
type: "show-image"
data: string
}
interface SetBrightnessMessage extends ArcadeShieldMessage {
type: "set-brightness"
value: number
}
interface SetPaletteMessage extends ArcadeShieldMessage {
type: "set-palette"
data: string
}
interface ButtonMessage extends ArcadeShieldMessage {
type: "button-down" | "button-up"
buttonId: ArcadeButtonId
}
interface DisplayMessage extends ArcadeShieldMessage {
type: "display-on" | "display-off"
}
class ScreenState {
runId: string;
brightness: number;
displayOn: boolean;
gotSimMessage: boolean;
constructor() {
this.runId = Math.random() + "";
this.displayOn = undefined
this.gotSimMessage = false
}
displayHeight(): number {
return 120;
}
displayWidth(): number {
return 160;
}
displayPresent(): boolean {
return this.displayOn;
}
private sendMessage(msg: string) {
control.simmessages.send("microbit-apps/pxt-arcadeshield", Buffer.fromUTF8(msg) , false)
}
initSim() {
const msg: ArcadeShieldMessage = {
type: "initialize",
runId: this.runId
}
this.sendMessage(JSON.stringify(msg))
}
setScreenBrightness(b: number) {
// NOTE: May need to cache locally for querying
const msg: SetBrightnessMessage = {
type: "set-brightness",
runId: this.runId,
value: b
}
this.sendMessage(JSON.stringify(msg))
}
setPalette(buf: Buffer) {
// NOTE: May need to cache locally for querying
const msg: SetPaletteMessage = {
type: "set-palette",
runId: this.runId,
data: buf.toBase64()
}
this.sendMessage(JSON.stringify(msg))
}
showImage(img: Bitmap) {
// NOTE: May need to cache locally for querying
const msg: ShowImageMessage = {
type: "show-image",
runId: this.runId,
data: img.__buffer.toBase64()
}
this.sendMessage(JSON.stringify(msg))
}
}
let _screenState: ScreenState = null;
//% shim=TD_NOOP
function startSim() {
control.simmessages.onReceived("microbit-apps/pxt-arcadeshield", handleShieldMessage)
_screenState.initSim()
while (!_screenState.gotSimMessage) {
basic.pause(0)
_screenState.initSim()
}
}
function getScreenState() {
if (!_screenState) {
_screenState = new ScreenState()
startSim()
}
}
//% shim=TD_NOOP
function simUpdateScreen(img: Bitmap) {
getScreenState();
if (_screenState)
_screenState.showImage(img);
}
export function updateScreen(img: Bitmap) {
__screenhelpers.updateScreen(img)
simUpdateScreen(img)
}
//% shim=TD_NOOP
function simSetPalette(b: Buffer) {
getScreenState();
if (_screenState)
_screenState.setPalette(b);
}
export function setPalette(b: Buffer) {
__screenhelpers.setPalette(b)
simSetPalette(b)
}
//% shim=TD_NOOP
function simSetScreenBrightness(n: number) {
getScreenState();
if (_screenState)
_screenState.setScreenBrightness(n);
}
export function setScreenBrightness(n: number) {
__screenhelpers.setScreenBrightness(n)
simSetScreenBrightness(n)
}
// getters
let __height = 0
//% shim=TD_NOOP
function simDisplayHeight() {
__height = 120
getScreenState();
if (_screenState)
__height = _screenState.displayHeight();
}
export function displayHeight(): number {
__height = __screenhelpers.displayHeight()
simDisplayHeight()
return __height
}
let __width = 0
//% shim=TD_NOOP
function simDisplayWidth() {
__width = 160
getScreenState();
if (_screenState)
__width = _screenState.displayWidth();
}
export function displayWidth(): number {
__width = __screenhelpers.displayWidth()
simDisplayWidth()
return __width
}
let __present = undefined
//% shim=TD_NOOP
function simDisplayPresent() {
__present = undefined
getScreenState();
if (_screenState)
__present = _screenState.displayPresent();
}
//% blockId=shieldPresent block="shield present?"
//% blockNamespace="Controller"
//% weight=0
//% help=github:pxt-arcadeshield/docs/shield-resent
export function shieldPresent(): boolean {
__present = undefined
while (__present === undefined) {
__present = __screenhelpers.displayPresent()
simDisplayPresent()
basic.pause(0)
}
return __present
}
function getButton(id: ArcadeButtonId): controller.Button {
switch (id) {
case "left": return controller.left
case "right": return controller.right
case "up": return controller.up
case "down": return controller.down
case "a": return controller.A
case "b": return controller.B
case "menu": return controller.menu
}
return null
}
function handleShieldMessage(b: Buffer) {
const s = b.toString()
const msg = <ArcadeShieldMessage>JSON.parse(s)
if (msg) {
console.log(msg.type)
getScreenState();
_screenState.gotSimMessage = true
basic.pause(0)
if (msg.type === "button-down" || msg.type === "button-up") {
const button = getButton((<ButtonMessage>msg).buttonId)
if (button) {
if (msg.type === "button-down") {
button.raiseButtonDown()
} else {
button.raiseButtonUp()
}
}
} else if (msg.type === "display-on") {
getScreenState()
_screenState.displayOn = true
basic.pause(0)
control.raiseEvent(ControllerShieldEvent.Present,0)
} else if (msg.type === "display-off") {
getScreenState()
_screenState.displayOn = false
basic.pause(0)
control.raiseEvent(ControllerShieldEvent.Absent,0)
}
}
}
/**
* Register simulator to get button presses
*/
//% shim=TD_NOOP
export function registerSim() {
getScreenState()
}
}
shieldhelpers.registerSim()