Skip to content

Commit 971882e

Browse files
committed
screen.ts acks
1 parent b20ae70 commit 971882e

File tree

1 file changed

+72
-11
lines changed

1 file changed

+72
-11
lines changed

screen.ts

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,68 @@ namespace user_interface_base {
6969
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_SET_IMAGE_SIZE, width, height]));
7070
}
7171

72-
public static sendBitmap(name: string, bitmap: Bitmap) {
73-
function waitForAck() {
74-
let received = false;
75-
radio.onReceivedString((_: String) => {
76-
received = true;
77-
})
78-
79-
while (!received) {
80-
basic.pause(3)
81-
}
72+
public static waitForAck() {
73+
let received = false;
74+
radio.onReceivedString((_: String) => {
75+
received = true;
76+
})
77+
78+
while (!received) {
79+
basic.pause(3)
80+
}
81+
}
82+
83+
84+
public static getBuffer(bitmap: Bitmap, chunkIndex: number, chunkSize: number): Buffer {
85+
const width = bitmap.width
86+
const startIndex = chunkIndex * chunkSize;
87+
const startingRow = (startIndex / width | 0);
88+
89+
const endIndex = startIndex + chunkSize;
90+
const endingRow = (endIndex / width | 0);
91+
92+
// Buffer crosses multiple rows:
93+
if (startingRow != endingRow) {
94+
const rowBuf1 = Buffer.create(bitmap.width);
95+
const rowBuf2 = Buffer.create(bitmap.width);
96+
97+
bitmap.getRows(startingRow, rowBuf1);
98+
bitmap.getRows(startingRow + 1, rowBuf2);
99+
100+
const overhead = width - (startIndex % width);
101+
const chunkBuf1 = rowBuf1.slice(startIndex % width, overhead);
102+
const chunkBuf2 = rowBuf2.slice(0, chunkSize - overhead);
103+
104+
const res = chunkBuf1.concat(chunkBuf2);
105+
// basic.showNumber(res.length)
106+
return res
107+
}
108+
109+
// Simply get the row, slice off the required bytes:
110+
else {
111+
const rowBuf = Buffer.create(bitmap.width);
112+
bitmap.getRows(startingRow, rowBuf);
113+
const res = rowBuf.slice(startIndex % width, chunkSize);
114+
// basic.showNumber(res.length)
115+
return res
116+
}
117+
}
118+
119+
public static sendBitmap(bitmap: Bitmap) {
120+
const maxPacketBufferSize = 16;
121+
const numberOfChunks: number =
122+
(bitmap.height * bitmap.width) / maxPacketBufferSize;
123+
124+
// Send bitmap meta-data:
125+
radio.sendString("" + maxPacketBufferSize + "," + bitmap.width + "," + bitmap.height);
126+
this.waitForAck();
127+
128+
// Send a chunk of the bitmap and wait for ACK, RX will rebuild the bitmap:
129+
for (let j = 0; j < numberOfChunks; j++) {
130+
const rowBuffer = this.getBuffer(bitmap, j, maxPacketBufferSize);
131+
radio.sendBuffer(rowBuffer);
132+
133+
this.waitForAck();
82134
}
83135
}
84136

@@ -92,6 +144,7 @@ namespace user_interface_base {
92144
// }
93145
// Just separate the sendBitmap function into this file, make it have a static array that it can update, hash 'from' into that to get an index that you send.
94146
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_DRAW_TRANSPARENT_IMAGE, x, y]));
147+
this.waitForAck();
95148
}
96149

97150

@@ -124,6 +177,7 @@ namespace user_interface_base {
124177
c
125178
])
126179
);
180+
this.waitForAck();
127181
}
128182

129183
public static drawLineXfrm(
@@ -168,6 +222,7 @@ namespace user_interface_base {
168222
c: number
169223
) {
170224
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_DRAW_RECT, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, width, height, c]));
225+
this.waitForAck();
171226
}
172227

173228
public static drawRectXfrm(
@@ -179,7 +234,8 @@ namespace user_interface_base {
179234
c: number
180235
) {
181236
const w = xfrm.worldPos
182-
Screen.drawRect(x + w.x, y + w.y, width, height, c)
237+
Screen.drawRect(x + w.x, y + w.y, width, height, c);
238+
this.waitForAck();
183239
}
184240

185241

@@ -188,6 +244,7 @@ namespace user_interface_base {
188244
) {
189245
// basic.showNumber(c)
190246
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_FILL, c]));
247+
this.waitForAck();
191248
}
192249

193250
public static fillRect(
@@ -199,6 +256,7 @@ namespace user_interface_base {
199256
) {
200257
let startTime = input.runningTime();
201258
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_FILL_RECT, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, width, height, c]))
259+
this.waitForAck();
202260
let endTime = input.runningTime();
203261
basic.showNumber(endTime - startTime)
204262
}
@@ -366,6 +424,7 @@ namespace user_interface_base {
366424
public static setPixel(x: number, y: number, c: number) {
367425
if (c) {
368426
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_SET_PIXEL, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, c]));
427+
this.waitForAck();
369428
}
370429
}
371430

@@ -388,9 +447,11 @@ namespace user_interface_base {
388447
offsets?: texteffects.TextEffectState[]
389448
) {
390449
radio.sendString(text);
450+
this.waitForAck();
391451

392452
const c: number = (color == null) ? 0 : color;
393453
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_PRINT, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, c]));
454+
this.waitForAck();
394455
}
395456
}
396457
}

0 commit comments

Comments
 (0)