Skip to content

Commit b247644

Browse files
committed
demo use case
1 parent 62eac59 commit b247644

3 files changed

Lines changed: 156 additions & 44 deletions

File tree

app.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace microdata {
22
import AppInterface = user_interface_base.AppInterface
33
import Scene = user_interface_base.Scene
44
import SceneManager = user_interface_base.SceneManager
5+
import Screen = user_interface_base.Screen
56

67
// Auto-save slot
78
export const SAVESLOT_AUTO = "sa"
@@ -28,21 +29,94 @@ namespace microdata {
2829
reportEvent("app.start")
2930

3031
radio.setGroup(5)
31-
radio.setTransmitPower(7)
32-
radio.setFrequencyBand(14)
32+
// radio.setTransmitPower(7)
33+
// radio.setFrequencyBand(14)
3334

35+
this.handshake();
3436
sendAllAssetsOverRadio();
3537

3638
this.sceneManager = new SceneManager()
3739
datalogger.includeTimestamp(FlashLogTimeStampFormat.None)
3840

3941
// const arcadeShieldConnected = shieldhelpers.shieldPresent();
4042
// if (arcadeShieldConnected)
41-
this.pushScene(new microdata.Home(this));
43+
44+
// this.pushScene(new microdata.Home(this));
4245
// else
4346
// new DistributedLoggingProtocol(this, false);
4447
// else
4548
// new DistributedLoggingProtocol(this, false);
49+
50+
51+
const imgs = [
52+
"led_light_sensor",
53+
"thermometer",
54+
"accelerometer",
55+
"finger_press",
56+
"green_tick",
57+
"magnet",
58+
"pin_0",
59+
"pin_1",
60+
"pin_2",
61+
];
62+
63+
64+
let pauseLoop = false;
65+
66+
// input.onButtonPressed(Button.A, function() {
67+
// pauseLoop = true;
68+
// basic.showString("A")
69+
// })
70+
71+
// input.onButtonPressed(Button.B, function() {
72+
// pauseLoop = true;
73+
// basic.showString("B")
74+
// })
75+
76+
77+
let i = 0;
78+
while (true) {
79+
if (!pauseLoop) {
80+
basic.showNumber(i % 10)
81+
Screen.fill(i % 16)
82+
83+
const img = icons.get(imgs[i % imgs.length])
84+
Screen.drawTransparentImage(
85+
img,
86+
(screen().width >> 1) - (img.width >> 1),
87+
(screen().height >> 1) - (img.height >> 1)
88+
)
89+
90+
i++;
91+
} else {
92+
Screen.fill(1)
93+
basic.pause(3000)
94+
pauseLoop = false;
95+
}
96+
}
97+
}
98+
99+
handshake() {
100+
let handshakeRecieved = false;
101+
radio.onReceivedString((_: string) => {
102+
handshakeRecieved = true;
103+
radio.sendString("ACK")
104+
})
105+
106+
radio.sendString("HANDSHAKE");
107+
// let handshakeTimeout = 0;
108+
while (!handshakeRecieved) {
109+
// if (handshakeTimeout == 0) {
110+
// }
111+
112+
// handshakeTimeout += 3;
113+
basic.pause(3)
114+
115+
// if (handshakeTimeout >= 99) {
116+
// handshakeTimeout = 0;
117+
// }
118+
}
119+
radio.onReceivedString((_: string) => { })
46120
}
47121

48122
public pushScene(scene: Scene) {

assets.ts

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,34 @@ namespace microdata {
99
}
1010

1111
export function sendAllAssetsOverRadio() {
12+
// const iconNames: string[] = [
13+
// "wordLogo",
14+
// "microbitLogo",
15+
// "edit_program",
16+
// "MISSING",
17+
// "largeDisk",
18+
// "linear_graph_1",
19+
// "led_light_sensor",
20+
// "thermometer",
21+
// "accelerometer",
22+
// "finger_press",
23+
// "green_tick",
24+
// "magnet",
25+
// "pin_0",
26+
// "pin_1",
27+
// "pin_2",
28+
// "right_turn",
29+
// "right_spin",
30+
// "microphone",
31+
// "tile_button_a",
32+
// "tile_button_b",
33+
// "compass",
34+
// "radio_set_group",
35+
// "largeSettingsGear",
36+
// "microbitLogoWhiteBackground"
37+
// ];
38+
1239
const iconNames: string[] = [
13-
"wordLogo",
14-
"microbitLogo",
15-
"edit_program",
1640
"MISSING",
1741
"largeDisk",
1842
"linear_graph_1",
@@ -27,57 +51,71 @@ namespace microdata {
2751
"pin_2",
2852
"right_turn",
2953
"right_spin",
30-
"microphone",
31-
"tile_button_a",
32-
"tile_button_b",
33-
"compass",
34-
"radio_set_group",
35-
"largeSettingsGear",
36-
"microbitLogoWhiteBackground"
54+
"microphone"
3755
];
3856

57+
// const iconNames: string[] = [
58+
// "green_tick"
59+
// // "wordLogo"
60+
// ]
3961

40-
const waitForAck = () => {
41-
basic.showString("W")
62+
// Get the number of bitmaps:
63+
radio.sendString("BITMAPS," + iconNames.length);
64+
waitForAck();
4265

43-
let ackReceived = false;
44-
radio.onReceivedString(_ => {
45-
ackReceived = true;
46-
})
66+
for (let i = 0; i < iconNames.length; i++) {
67+
Screen.sendBitmap(icons.get(iconNames[i]));
68+
}
4769

48-
// timeout:
49-
for (let timeChunk = 0; timeChunk < 500; timeChunk += 25) {
50-
if (ackReceived)
51-
break
52-
basic.pause(25)
53-
}
70+
basic.showString("F")
71+
}
5472

55-
radio.onReceivedValue(_ => { }) // reset radio
56-
return ackReceived;
57-
};
73+
function waitForAck() {
74+
let received = false;
75+
radio.onReceivedString((_: String) => {
76+
received = true;
77+
})
5878

79+
while (!received) {
80+
basic.pause(3)
81+
}
82+
}
5983

60-
basic.showString("St")
61-
radio.sendString("ASSET_TX_START" + ", " + iconNames.length)
62-
basic.pause(10)
84+
function getBuffer(bitmap: Bitmap, chunkIndex: number, chunkSize: number): Buffer {
85+
const width = bitmap.width
86+
const startIndex = chunkIndex * chunkSize;
87+
const startingRow = (startIndex / width | 0);
6388

64-
while (!waitForAck()) {
65-
radio.sendString("ASSET_TX_START" + ", " + iconNames.length)
66-
basic.pause(10)
67-
}
89+
const endIndex = startIndex + chunkSize;
90+
const endingRow = (endIndex / width | 0);
6891

69-
// iconNames.forEach(name => {
70-
for (let i = 0; i < 1; i++) {
71-
Screen.sendBitmap(iconNames[i], icons.get(iconNames[i]))
72-
// Screen.sendBitmap(name, icons.get(name))
73-
// })
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
74107
}
75108

76-
// radio.sendString("ASSET_TX_END")
77-
basic.showString("D")
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+
}
78117
}
79118

80-
81119
// All unused assets have been cut since the flash storage limit has been approached
82120

83121
export class icons {

pxt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"radio": "*",
88
"microphone": "*",
99
"datalogger": "*",
10-
"user-interface-base": "github:microbit-apps/user-interface-base#276829b6de640cdafb63edbc2e8fc16c1ba81b44",
10+
"user-interface-base": "github:microbit-apps/user-interface-base#91e9a211f59c42ec947857a945aaa6f4aeea692b",
1111
"jacdac": "github:microsoft/pxt-jacdac#v1.9.28",
1212
"jacdac-light-level": "github:microsoft/pxt-jacdac/light-level#v1.9.28",
1313
"jacdac-soil-moisture": "github:microsoft/pxt-jacdac/soil-moisture#v1.9.28",

0 commit comments

Comments
 (0)