Skip to content

Commit 94d5c32

Browse files
committed
Pre-optimised handshake checkpoint
1 parent 4760027 commit 94d5c32

2 files changed

Lines changed: 184 additions & 107 deletions

File tree

init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ namespace __screen_internal {
2020
)
2121

2222
control.__screen.setupUpdate(() => shieldhelpers.updateScreen(img))
23+
// control.inBackground(() => {
2324
radioControlRxLoop();
25+
// })
2426

2527
return img as Bitmap;
2628
}

radioControlRx.ts

Lines changed: 182 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -9,161 +9,236 @@ const SCREEN_FN_ID_SET_PIXEL: number = 21;
99
const SCREEN_FN_ID_PRINT: number = 23;
1010

1111

12+
function setup(): Bitmap[] {
13+
let bitmaps: Bitmap[] = []
1214

13-
function setup() {
14-
let latestString: string = "";
15+
//--------------------------------------------------
16+
// Get the number of bitmaps the Tx will be sending:
17+
//--------------------------------------------------
1518

16-
// radio.sendString("ASSET_TX_START" + ", " + iconNames.length)
17-
// iconNames.forEach(name => {
18-
// screen().sendBitmap(name, icons.get(name))
19-
// })
19+
let numberOfBitmaps = 0;
20+
radio.onReceivedString((header: String) => {
21+
if (header.split(",")[0] == "BITMAPS") {
22+
numberOfBitmaps = +header.split(",")[1];
23+
}
24+
})
2025

21-
// radio.sendString("ASSET_TX_END")
22-
// basic.showString("Done")
26+
while (numberOfBitmaps == 0) {
27+
basic.pause(3);
28+
}
29+
radio.sendString("ACK");
2330

2431

25-
//-------------------------
26-
// Wait for ASSET_TX_START:
27-
//-------------------------
32+
//----------------------------
33+
// Get the bitmaps one by one:
34+
//----------------------------
2835

36+
for (let i = 0; i < numberOfBitmaps; i++) {
37+
// Send the number of lines:
38+
let maxPacketBufferSize = 0;
39+
let bitmapWidth = 0;
40+
let bitmapHeight = 0;
2941

30-
radio.setGroup(5)
31-
radio.setTransmitPower(7)
32-
radio.setFrequencyBand(14)
42+
//---------------------------
43+
// Get the bitmap dimensions:
44+
//---------------------------
3345

34-
// First message should be an ASSET_TX_START + "," + number of assets
35-
// Then asset_name + "," + bitmap.height (number of rows of this asset)
46+
radio.onReceivedString((str: String) => {
47+
maxPacketBufferSize = +str.split(",")[0]
48+
bitmapWidth = +str.split(",")[1]
49+
bitmapHeight = +str.split(",")[2]
3650

37-
let receivedStartAssetTransferRequest = false;
38-
let numberOfAssetsExpected: number = 0;
51+
// basic.showNumber(maxPacketBufferSize)
52+
// basic.showNumber(bitmapWidth)
53+
// basic.showNumber(bitmapHeight)
3954

40-
radio.onReceivedString((receivedString: string) => {
41-
numberOfAssetsExpected = +receivedString.split(",")[1];
42-
// basic.showString("A: " + numberOfAssetsExpected)
43-
// basic.showNumber(numberOfAssetsExpected % 10)
44-
receivedStartAssetTransferRequest = true
45-
radio.sendString("ACK")
46-
});
47-
48-
// basic.showString("S")
49-
while (!receivedStartAssetTransferRequest) { basic.pause(25) }
50-
// basic.showString("D: " + numberOfAssetsExpected % 10)
51-
// basic.showString("D")
52-
53-
// Now get the [bitmapName, bitmapHeight], followed by each row of the bitmap
54-
let bitmapName: string;
55-
let bitmapHeight: number;
56-
let received = false;
57-
58-
// Reconstruct each of these assets from the following:
59-
// Header of [bitmap name, bitmap height] -> row of bitmap data
60-
for (let i = 0; i < numberOfAssetsExpected; i++) {
61-
62-
//--------------------------
63-
// Get bitmap name & height:
64-
//--------------------------
65-
radio.onReceivedString((receivedString: string) => {
66-
basic.showString("R")
67-
68-
bitmapName = receivedString.split(",")[0]
69-
bitmapHeight = +receivedString.split(",")[1]
70-
71-
// basic.showString("A: " + bitmapHeight);
72-
// basic.showNumber(bitmapHeight % 10)
73-
radio.sendString("ACK")
74-
received = true;
55+
radio.sendString("ACK");
7556
})
7657

77-
// basic.showString("W")
78-
while (!received) {
79-
basic.pause(25)
58+
while (maxPacketBufferSize == 0) {
59+
basic.pause(3);
8060
}
81-
basic.showString("D")
82-
83-
// Finally: unbind for safety - since we're done with this part - but a spurious string reception would mess up control flow/variables
84-
radio.onReceivedString(_ => { })
85-
86-
//------------------
87-
// Proccessing rows:
88-
//------------------
89-
90-
let rowsReceived: number = 0;
91-
let assetBuffer: Buffer = null;
92-
93-
// Process each row of the bitmap into a single asset:
94-
radio.onReceivedBuffer((onReceivedBuffer: Buffer) => {
95-
// basic.showString("B")
96-
97-
if (assetBuffer == null)
98-
assetBuffer = onReceivedBuffer
99-
else
100-
assetBuffer = Buffer.concat([assetBuffer, onReceivedBuffer])
101-
rowsReceived++;
102-
// basic.showString("A")
103-
// radio.sendString("ACK")
104-
// basic.showNumber(rowsReceived % 10);
61+
62+
//-----------------------------------------
63+
// Calculate the number of incoming chunks:
64+
//-----------------------------------------
65+
66+
const numberOfChunks: number =
67+
(bitmapWidth * bitmapHeight) / maxPacketBufferSize;
68+
69+
//----------------------------------
70+
// Concatenate the incoming buffers:
71+
//----------------------------------
72+
73+
let bitmapBuf: Buffer = null;
74+
let bitmapBufIsSet = false;
75+
let bufferReceived = false;
76+
radio.onReceivedBuffer((buf: Buffer) => {
77+
if (!bitmapBufIsSet) {
78+
bitmapBuf = buf;
79+
bitmapBufIsSet = true;
80+
} else {
81+
bitmapBuf = bitmapBuf.concat(buf);
82+
}
83+
84+
radio.sendString("ACK");
85+
bufferReceived = true;
10586
})
10687

107-
// basic.showString("C")
108-
// basic.showNumber(rowsReceived % 10);
109-
while (rowsReceived < bitmapHeight)
110-
basic.pause(5)
88+
89+
// Wait to receive all these chunks:
90+
for (let j = 0; j < numberOfChunks; j++) {
91+
while (!bufferReceived) {
92+
basic.pause(3)
93+
}
94+
bufferReceived = false;
95+
}
96+
97+
//---------------------------------
98+
// Rebuild the bitmap from buffers:
99+
//---------------------------------
100+
101+
bitmaps.push(rebuildBitmap(bitmapBuf, bitmapWidth, bitmapHeight));
102+
103+
// // Now rebuild the original bitmap from these buffers:
104+
// const img = rebuildBitmap(bitmapBuf, bitmapWidth, bitmapHeight);
105+
// const x = -(screen().width >> 1) + ((screen().width - img.width) >> 1);
106+
107+
// screen().drawBitmap(
108+
// img,
109+
// 10,
110+
// (screen().height >> 1) - 10
111+
// )
112+
111113
// basic.showString("D")
112114

115+
// bitmaps.push(img);
116+
113117
// Rebinding for safety - since we're going back to only responding to .onReceivedString() at the top of this loop:
114-
radio.onReceivedBuffer(_ => { })
118+
// radio.onReceivedBuffer(_ => { })
115119
}
116120

117-
basic.showString("D")
121+
radio.onReceivedString((_: string) => { })
122+
radio.onReceivedBuffer((_: Buffer) => { })
123+
basic.showString("F")
124+
return bitmaps;
125+
}
126+
127+
128+
function rebuildBitmap(buf: Buffer, bitmapWidth: number, bitmapHeight: number): Bitmap {
129+
let img: Bitmap = bitmaps.create(bitmapWidth, bitmapHeight);
130+
131+
// basic.showString("B")
132+
// basic.showNumber(bitmapWidth)
133+
// basic.showNumber(img.width)
134+
135+
// basic.showNumber(bitmapHeight)
136+
// basic.showNumber(img.height)
137+
138+
// basic.showNumber(buf.length)
139+
140+
for (let j = 0; j < bitmapHeight; j++) {
141+
img.setRows(j, buf.slice(j * bitmapWidth, bitmapWidth));
142+
}
143+
144+
return img;
145+
}
146+
147+
148+
function handshake() {
149+
let receivedHandshake = false;
150+
radio.onReceivedString((receivedString: string) => {
151+
if (receivedString == "HANDSHAKE") {
152+
receivedHandshake = true;
153+
radio.sendString("ACK")
154+
}
155+
})
156+
157+
while (!receivedHandshake) { basic.pause(3) }
158+
radio.onReceivedString((_: string) => { })
118159
}
119160

120161
function radioControlRxLoop() {
121-
setup();
162+
radio.setGroup(5)
163+
handshake();
164+
const bitmaps: Bitmap[] = setup();
122165

123-
let latestString: string = ""; // setup via radio.onReceivedString from setup()
166+
// const x = -(screen().width >> 1) + ((screen().width - img.width) >> 1);
124167

168+
// basic.showNumber(img.width)
169+
// basic.showNumber(img.height)
125170

126-
// Main loop; listen for draw commands:
127-
radio.onReceivedBuffer((buffer: Buffer) => {
128-
const fn_id: number = buffer[0];
129-
const params: Buffer = buffer.slice(1);
171+
// basic.showString("R")
130172

131-
// basic.showString("R")
173+
// basic.showNumber(img.width)
174+
// basic.showNumber(img.height)
175+
176+
// for (let i = 0; i < img.height; i++) {
177+
// for (let j = 0; j < img.width; j++) {
178+
// basic.showString("L")
179+
// basic.showNumber(img.getPixel(j, i))
180+
// basic.showString(",")
181+
// basic.showNumber(green_tick.getPixel(j, i))
182+
// }
183+
// }
184+
185+
let latestString = ""
186+
radio.onReceivedString((str: string) => {
187+
latestString = str
188+
})
189+
190+
// Main loop; listen for draw commands:
191+
radio.onReceivedBuffer((buf: Buffer) => {
192+
const fn_id: number = buf[0];
193+
const params: Buffer = buf.slice(1);
132194

133-
// basic.showNumber(fn_id)
134195
switch (fn_id) {
135196
// case SCREEN_FN_ID_ASSET_SETUP: { break;}
136197
// case SCREEN_FN_ID_RESET_SCREEN_IMAGE: { screen().resetscreenImage(); break; }
137198
// case SCREEN_FN_ID_SET_IMAGE_SIZE: { screen().setImageSize(params[0], params[1]); break; }
138199

139-
// semi-cheat; the assets are pre-loaded.
140200
case SCREEN_FN_ID_DRAW_TRANSPARENT_IMAGE: {
141-
// const from: Bitmap = microdata.icons.get(latestString);
142-
// screen().drawTransparentImage(from, params[1] - half_width, params[2] - half_height);
201+
const img = bitmaps[params[0]];
202+
screen().drawBitmap(
203+
img,
204+
params[1],
205+
params[2]
206+
)
207+
radio.sendString("ACK");
143208
break;
144209
}
145210

146211
case SCREEN_FN_ID_DRAW_LINE: { screen().drawLine(params[0], params[1], params[2], params[3], params[4]); break; }
147212
case SCREEN_FN_ID_DRAW_RECT: { screen().drawRect(params[0], params[1], params[2], params[3], params[4]); break; }
148-
case SCREEN_FN_ID_FILL: { screen().fill(params[0]); break; }
213+
214+
case SCREEN_FN_ID_FILL: {
215+
// let startTime = input.runningTime();
216+
217+
// basic.showNumber(fn_id)
218+
screen().fill(params[0]);
219+
radio.sendString("ACK");
220+
221+
// screen().drawBitmap(
222+
// bitmaps[0],
223+
// params[1],
224+
// params[2]
225+
// )
226+
227+
// let endTime = input.runningTime();
228+
// basic.showNumber(endTime - startTime)
229+
break;
230+
}
231+
149232
case SCREEN_FN_ID_FILL_RECT: { screen().fillRect(params[0], params[1], params[2], params[3], params[4]); break; }
150233
case SCREEN_FN_ID_SET_PIXEL: { screen().setPixel(params[0], params[1], params[2]); break; }
151234

152235
case SCREEN_FN_ID_PRINT: {
153-
// basic.showNumber(params[0])
154-
// basic.showString("A")
155-
// basic.showNumber(screen().width)
156-
// basic.showString("B")
157-
// basic.showNumber(screen().height)
158-
// basic.showString("C")
159-
// basic.showNumber(params[1])
160-
// basic.showString("D")
236+
basic.showString("P")
161237
screen().print(latestString, params[0], params[1] - (screen().height >> 1), params[2]); break;
162238
}
163239

164240
default: { break; }
165241
}
166-
basic.clearScreen()
167242
})
168243
}
169244

0 commit comments

Comments
 (0)