-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathemulator.js
More file actions
140 lines (129 loc) · 5.38 KB
/
Copy pathemulator.js
File metadata and controls
140 lines (129 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Code to handle loading the Bangle.js emulator and setting up a UART connection
to it as if it was a real device.
The emulator that's loaded depends on the currently chosen device in the UI
(top left), but you can force it to be a specific one by changing the code below
if you want to check if an app can work:
CHOSENDEVICE = DEVICEINFO.find(d=>d.id.startsWith("BANGLEJS3"));
*/
var emu, emuConnectedCallback;
window.addEventListener('message', function(e) {
// emu can be undefined when emu window closed OR refreshed.
var event = e.data;
//if (typeof event!="object" || event.from!="emu") return;
console.log("EMU HOST MESSAGE", event);
switch (event.type) {
case "init":
console.log("EMU frame initialised");
try {
emu.addEventListener('unload', function(e) {
console.log("EMU frame closed (unload handler called)");
if (UART.getConnection())
UART.getConnection().closeHandler();
});
} catch (e) {
// Fails if cross-origin - not sure we can detect closure?
console.log("Unable to emu.addEventListener('unload', ...)", e);
}
if (UART.getConnection())
UART.getConnection().openHandler();
if (emuConnectedCallback) { // actually the promise resolver for connections
emuConnectedCallback(UART.getConnection());
emuConnectedCallback = undefined;
}
break;
case "tx": {
var d = event.data;
var a = new Uint8Array(d.length);
for (var i=0;i<d.length;i++) a[i]=d.charCodeAt(i);
if (UART.getConnection())
UART.getConnection().rxDataHandler(a.buffer);
break;
}
case "pagehide":
case "unload": {
console.log("EMU frame closed (event received)");
if (UART.getConnection())
UART.getConnection().closeHandler();
break;
}
}
});
// Create an emulator window and connection, returns promise
function startEmulator() {
function post(msg) {
if (!emu) return;
msg.for="emu";
emu.postMessage(msg,"*");
}
UART.endpoints.push({
name : "Emulator",
description : "Bangle.js Emulator",
svg : `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 26 26" width="48px" height="48px"><path d="M 3 1 C 1.34375 1 0 2.34375 0 4 L 0 18 C 0 19.65625 1.34375 21 3 21 L 10 21 L 10 22 L 16 22 L 16 21 L 23 21 C 24.65625 21 26 19.65625 26 18 L 26 4 C 26 2.34375 24.65625 1 23 1 Z M 3 3 L 23 3 C 23.550781 3 24 3.449219 24 4 L 24 18 C 24 18.550781 23.550781 19 23 19 L 3 19 C 2.449219 19 2 18.550781 2 18 L 2 4 C 2 3.449219 2.449219 3 3 3 Z M 8 5 C 6.34375 5 5 6.34375 5 8 L 5 14 C 5 15.65625 6.34375 17 8 17 L 18 17 C 19.65625 17 21 15.65625 21 14 L 21 8 C 21 6.34375 19.65625 5 18 5 Z M 7 8 L 19 8 L 19 14 C 19 14.550781 18.550781 15 18 15 L 8 15 C 7.449219 15 7 14.550781 7 14 Z M 6 23 C 5.019531 23 5 24 5 24 L 5 25 L 21 25 L 21 24 C 21 23.449219 20.550781 23 20 23 Z"/></svg>`,
isSupported : function() { return true; },
connect : function(connection, options) {
options = options || {};
connection.closeLowLevel = function(callback) {
if (emu) emu.close();
emu = undefined;
window.emu = undefined;
};
connection.writeLowLevel = function(data, callback, alreadyRetried) {
console.log("EMU sending", data);
return new Promise((resolve, reject) => {
post({type:"rx",data: data});
if (callback) callback();
resolve();
});
};
connection.openHandler();
return new Promise( resolve => {
var emuDevice = CHOSENDEVICE;
if (!CHOSENDEVICE.id)
CHOSENDEVICE = DEVICEINFO.find(d=>d.id.startsWith("BANGLEJS2")); /* default */
var url;/* = window.location.pathname;
if (url.includes("/"))
url = url.substr(0,url.lastIndexOf("/"));*/
if (window.location.hostname=="localhost")
url = "http://localhost/ide" + emuDevice.emulatorURL; // development only
else // otherwise use online IDE
url = "https://www.espruino.com/ide" + emuDevice.emulatorURL;
console.log("Opening Emulator",url);
emuConnectedCallback = resolve;
emu = window.emu = window.open(url, "banglewindow", emuDevice.emulatorWin);
var inited = false;
// when the window is loaded, post 'init'
emu.addEventListener("load", function() {
if (!inited) post({type:"init"});
inited = true;
}, false);
// if load didn't fire (window was already loaded) then post init after a short delay
setTimeout(function() {
if (!inited) post({type:"init"});
inited = true;
}, 1000);
});
}
});
let oldPorts = UART.ports;
UART.ports = ["Emulator"];
let promise = Comms.getDeviceInfo();
UART.ports = oldPorts;
return promise;
}
// Create an emulator and factory reset it, returns promise
function startCleanEmulator() {
return startOperation({name:"Start Emulator"}, () => {
Progress.show({title:`Starting Emulator`, sticky:true});
let promise;
if (Comms.isConnected() && UART.getConnection().device!="Emulator")
promise = Comms.disconnectDevice();
else
promise = Promise.resolve();
if (!Comms.isConnected())
promise = promise.then(() => startEmulator());
promise = promise.then(() => Comms.write("Bangle.factoryReset()\n"));
promise = promise.then(new Promise(resolve => setTimeout(resolve, 500)));
return promise;
});
}