Skip to content

Commit e4c68bd

Browse files
committed
Generalisation of tryToSend to accept Strings, all radio comms using tryToSend, waitForAck() removed, ACK checks that the text is 'ACK' exactly
1 parent 0d7a948 commit e4c68bd

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

button.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ namespace user_interface_base {
66
public bottom: number,
77
public left: number,
88
public right: number
9-
) {}
9+
) { }
1010
}
1111

1212
export class ButtonStyle {
1313
constructor(
1414
public fill: number,
1515
public borders: Borders,
1616
public shadow: boolean
17-
) {}
17+
) { }
1818
}
1919

2020
export namespace ButtonStyles {
@@ -148,8 +148,8 @@ namespace user_interface_base {
148148
return !this.icon.invisible
149149
}
150150

151-
public hover(hov: boolean) {}
152-
public update() {}
151+
public hover(hov: boolean) { }
152+
public update() { }
153153

154154
isOffScreenX(): boolean {
155155
return this.icon.isOffScreenX()
@@ -221,7 +221,7 @@ namespace user_interface_base {
221221
value: this.ariaId,
222222
force,
223223
}
224-
accessibility.setLiveContent(msg)
224+
accessibility.setLiveContent(msg)
225225
}
226226

227227
constructor(opts: {
@@ -283,10 +283,10 @@ namespace user_interface_base {
283283

284284
private image_() {
285285
return typeof this.iconId == "string"
286-
? getIcon(this.iconId,false)
286+
? getIcon(this.iconId, false)
287287
: this.iconId
288288
}
289-
289+
290290
public setIcon(iconId: string, img?: Bitmap) {
291291
this.iconId = iconId
292292
if (img) this.icon.setImage(img)
@@ -310,7 +310,7 @@ namespace user_interface_base {
310310
super.draw()
311311

312312
if (this.dynamicBoundaryColorsOn) {
313-
const boundaryColour = (this.selected && this.pressable) ? 7: this.boundaryColor
313+
const boundaryColour = (this.selected && this.pressable) ? 7 : this.boundaryColor
314314

315315
for (let dist = 1; dist <= 3; dist++) {
316316
Screen.outlineBoundsXfrm(

screen.ts

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,27 @@ namespace user_interface_base {
7070
radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_SET_IMAGE_SIZE, width, height]));
7171
}
7272

73-
public static waitForAck() {
73+
public static tryToSend(data: Buffer | String) {
7474
let received = false;
75-
radio.onReceivedString((_: String) => {
76-
received = true;
77-
})
78-
79-
while (!received) {
80-
basic.pause(3)
81-
}
82-
}
83-
84-
public static tryToSend(buf: Buffer) {
85-
let received = false;
86-
radio.onReceivedString((_: String) => {
87-
received = true;
75+
radio.onReceivedString((s: String) => {
76+
if (s == "ACK")
77+
received = true;
8878
})
8979

9080
let timePassed = 0;
9181
while (!received) {
92-
if (timePassed % 99 == 0)
93-
radio.sendBuffer(buf);
94-
timePassed += 3;
95-
basic.pause(3)
82+
if (timePassed % 99 == 0) {
83+
if (typeof data == "string") {
84+
radio.sendString(data);
85+
} else {
86+
radio.sendBuffer(data as Buffer);
87+
}
88+
timePassed += 3;
89+
basic.pause(3)
90+
}
9691
}
9792
}
9893

99-
10094
public static getBuffer(bitmap: Bitmap, chunkIndex: number, chunkSize: number): Buffer {
10195
const width = bitmap.width
10296
const startIndex = chunkIndex * chunkSize;
@@ -137,8 +131,7 @@ namespace user_interface_base {
137131
(bitmap.height * bitmap.width) / maxPacketBufferSize;
138132

139133
// Send bitmap size information:
140-
radio.sendString("" + maxPacketBufferSize + "," + bitmap.width + "," + bitmap.height);
141-
this.waitForAck();
134+
this.tryToSend("" + maxPacketBufferSize + "," + bitmap.width + "," + bitmap.height);
142135

143136
// Send a chunk of the bitmap and wait for ACK, RX will rebuild the bitmap:
144137
for (let j = 0; j < numberOfChunks; j++) {
@@ -155,9 +148,6 @@ namespace user_interface_base {
155148
public static drawTransparentImage(from: Bitmap, x: number, y: number) {
156149
for (let i = 0; i < this.bitmapCache.length; i++) {
157150
if (this.bitmapCache[i] == from) {
158-
// radio.sendBuffer(Buffer.fromArray([SCREEN_FN_ID_DRAW_TRANSPARENT_IMAGE, i, x, y]));
159-
// this.waitForAck();
160-
161151
this.tryToSend(Buffer.fromArray([SCREEN_FN_ID_DRAW_TRANSPARENT_IMAGE, i, x, y]));
162152
return;
163153
}
@@ -172,7 +162,6 @@ namespace user_interface_base {
172162
y: number
173163
) {
174164
const w = xfrm.worldPos
175-
// Screen.image.drawTransparentBitmap(
176165
Screen.drawTransparentImage(
177166
from,
178167
Screen.x(x + w.x),
@@ -195,7 +184,6 @@ namespace user_interface_base {
195184
c
196185
])
197186
)
198-
// basic.pause(15)
199187
}
200188

201189
public static drawLineXfrm(
@@ -227,7 +215,6 @@ namespace user_interface_base {
227215
const c = shader(tx, ty)
228216
if (c) {
229217
Screen.setPixel(x, y, c)
230-
// basic.pause(15)
231218
}
232219
}
233220
}
@@ -241,7 +228,6 @@ namespace user_interface_base {
241228
c: number
242229
) {
243230
this.tryToSend(Buffer.fromArray([SCREEN_FN_ID_DRAW_RECT, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, width, height, c]));
244-
// basic.pause(15)
245231
}
246232

247233
public static drawRectXfrm(
@@ -261,7 +247,6 @@ namespace user_interface_base {
261247
c: number
262248
) {
263249
this.tryToSend(Buffer.fromArray([SCREEN_FN_ID_FILL, c]))
264-
// basic.pause(15)
265250
}
266251

267252
public static fillRect(
@@ -272,7 +257,6 @@ namespace user_interface_base {
272257
c: number
273258
) {
274259
this.tryToSend(Buffer.fromArray([SCREEN_FN_ID_FILL_RECT, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, width, height, c]))
275-
// basic.pause(15)
276260
}
277261

278262
public static fillRectXfrm(
@@ -459,12 +443,10 @@ namespace user_interface_base {
459443
font?: bitmaps.Font,
460444
offsets?: texteffects.TextEffectState[]
461445
) {
462-
radio.sendString(text);
463-
this.waitForAck();
446+
this.tryToSend(text);
464447

465448
const c: number = (color == null) ? 0 : color;
466449
this.tryToSend(Buffer.fromArray([SCREEN_FN_ID_PRINT, x + Screen.HALF_WIDTH, y + Screen.HALF_HEIGHT, c]));
467-
// basic.pause(15)
468450
}
469451
}
470452
}

0 commit comments

Comments
 (0)