Skip to content

Commit ab1200d

Browse files
committed
handshaking, recvBitmaps, fast drawing
1 parent a4590a7 commit ab1200d

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

init.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ shieldhelpers.setPalette(hex`000000ffffffff2121ff93c4ff8135fff609249ca378dc52003
1212

1313
const theScreen: Bitmap = __screen_internal.createScreen();
1414
theScreen.fill(15)
15+
// control.inBackground(() => {
16+
radioControlRxLoop();
17+
// })
1518
// __screen_internal.loop()
1619

1720
namespace __screen_internal {
@@ -23,13 +26,11 @@ namespace __screen_internal {
2326
)
2427

2528
control.__screen.setupUpdate(() => shieldhelpers.updateScreen(img))
26-
// control.inBackground(() => {
27-
radioControlRxLoop();
28-
// })
2929

3030
return img as Bitmap;
3131
}
3232

33+
3334
export function loop() {
3435

3536
let i = 0;

radioControlRx.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const SCREEN_FN_ID_HANDSHAKE: number = 0;
12
const SCREEN_FN_ID_RESET_SCREEN_IMAGE: number = 5;
23
const SCREEN_FN_ID_SET_IMAGE_SIZE: number = 6;
34
const SCREEN_FN_ID_DRAW_TRANSPARENT_IMAGE: number = 7;
@@ -23,6 +24,7 @@ namespace controller {
2324
controllerButtonEvents[i],
2425
btn.id,
2526
() => {
27+
// screen().fill(btn.id)
2628
radio.sendBuffer(Buffer.fromArray([BUTTON_PRESS_RADIO_ID, controllerKeys[i], btn.id]))
2729
}
2830
)
@@ -48,7 +50,9 @@ function recvBitmaps(): Bitmap[] {
4850
while (numberOfBitmaps == 0) {
4951
basic.pause(3);
5052
}
53+
5154
radio.sendString("ACK");
55+
// screen().fill(6);
5256

5357

5458
//----------------------------
@@ -80,6 +84,7 @@ function recvBitmaps(): Bitmap[] {
8084
while (maxPacketBufferSize == 0) {
8185
basic.pause(3);
8286
}
87+
// screen().fill(3);
8388

8489
//-----------------------------------------
8590
// Calculate the number of incoming chunks:
@@ -140,11 +145,12 @@ function recvBitmaps(): Bitmap[] {
140145

141146
// Rebinding for safety - since we're going back to only responding to .onReceivedString() at the top of this loop:
142147
// radio.onReceivedBuffer(_ => { })
148+
// screen().fill(3);
143149
}
144150

145151
radio.onReceivedString((_: string) => { })
146152
radio.onReceivedBuffer((_: Buffer) => { })
147-
basic.showString("F")
153+
// basic.showString("F")
148154
return bitmaps;
149155
}
150156

@@ -169,22 +175,25 @@ function rebuildBitmap(buf: Buffer, bitmapWidth: number, bitmapHeight: number):
169175
}
170176

171177

172-
function handshake() {
173-
let receivedHandshake = false;
174-
radio.onReceivedString((receivedString: string) => {
175-
if (receivedString == "HANDSHAKE") {
176-
receivedHandshake = true;
177-
radio.sendString("ACK")
178-
}
179-
})
178+
function handshake(): number {
179+
let device_id: number = null;
180180

181-
while (!receivedHandshake) { basic.pause(3) }
182-
radio.onReceivedString((_: string) => { })
181+
radio.onReceivedBuffer((buf: Buffer) => {
182+
if (buf[0] == SCREEN_FN_ID_HANDSHAKE) {
183+
device_id = buf[1];
184+
radio.sendString("ACK")
185+
}
186+
});
187+
188+
while (device_id == null) { basic.pause(10) }
189+
radio.onReceivedBuffer((_: Buffer) => { })
190+
return device_id;
183191
}
184192

185193
function radioControlRxLoop() {
186194
radio.setGroup(5)
187-
handshake();
195+
196+
const device_id = handshake();
188197
const bitmaps: Bitmap[] = recvBitmaps();
189198
controller.bindButtonsForWDS();
190199

@@ -195,10 +204,12 @@ function radioControlRxLoop() {
195204
})
196205

197206
radio.onReceivedBuffer((buf: Buffer) => {
198-
radio.sendString("ACK");
207+
208+
// radio.sendString("ACK");
199209
const request: number = buf[0];
200210
const params: Buffer = buf.slice(1);
201211

212+
// screen().printCenter(""+request, 0);
202213
// basic.showNumber(fn_id)
203214

204215
// control.inBackground(() => {
@@ -233,6 +244,8 @@ function radioControlRxLoop() {
233244
// basic.showNumber(fn_id)
234245
screen().fill(params[0]);
235246

247+
// screen().fill(3);
248+
236249
// let endTime = input.runningTime();
237250
// basic.showNumber(endTime - startTime)
238251
break;
@@ -253,12 +266,13 @@ function radioControlRxLoop() {
253266
}
254267

255268
default: {
256-
basic.showString("D")
257269
break;
258270
}
259271
}
260-
// basic.pause(20);
261-
// })
262-
})
272+
}) // end of radio.onReceivedBuffer()
273+
274+
while (true) {
275+
basic.pause(3)
276+
}
263277
}
264278

0 commit comments

Comments
 (0)