-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathemulator.js
238 lines (228 loc) · 7.18 KB
/
emulator.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Gordon Williams (gw@pur3.co.uk)
If we're running in an iframe, this gets enabled and allows the IDE
to work by passing messages using window.postMessage.
Use embed.js on the client side to link this in.
*/
var emu;
(function() {
var callbacks = {};
var EMULATORS = [
{
id : "BANGLEJS2",
name : "Bangle.js 1",
description : '240x240 16 bit, 3 buttons',
link : "https://www.espruino.com/Bangle.js",
emulatorURL : "/emu/emu_banglejs1.html",
emulatorWin : "innerWidth=290,innerHeight=268,location=0",
width : 240,
height : 240,
}, {
id : "BANGLEJS2",
name : "Bangle.js 2",
description : '176x176 3 bit, 1 button, full touchscreen',
link : "https://www.espruino.com/Bangle.js2",
emulatorURL : "/emu/emu_banglejs2.html",
emulatorWin : "innerWidth=290,innerHeight=268,location=0",
width : 176,
height : 176,
}
];
function post(msg) {
if (!emu) return;
msg.for="emu";
emu.postMessage(msg,"*");
}
window.addEventListener('message', function(e) {
if (!emu) return;
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");
emu.addEventListener('unload', function(e) {
console.log("EMU frame closed");
callbacks.disconnected();
callbacks = {};
});
callbacks.open("success");
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);
callbacks.receive(a.buffer);
break;
}
}
});
function chooseDevice(callback) {
var selected = false;
var popup = Espruino.Core.App.openPopup({
id: "sendmethod",
title: "Upload Destination",
padding: true,
contents: Espruino.Core.HTML.domList(EMULATORS.map(e=>({
title: e.name,
description : e.description,//+`<a href="${e.link}" target="_blank">more info</a>`,
callback : function() {
selected=true;
popup.close();
callback(e);
}
}))),
position: "auto",
onClose: () => {
if (!selected) callback(undefined);
}
});
}
function openEmulator(device) {
var url = window.location.pathname;
if (url.includes("/"))
url = url.substr(0, url.lastIndexOf("/"));
url = window.location.origin + url + device.emulatorURL;
if (Espruino.Config.EMULATOR_BANGLEJS_WINDOW === 'inside') {
try {
$('.emulator-window').remove();
var emulatorWindow = $('<div class="emulator-window"><div>EMULATOR</div><iframe src=""></iframe></div>').appendTo('body');
if (emulatorWindow) {
var title = $('.emulator-window > div');
var emulatorIFrame = $('.emulator-window > iframe');
if (title && emulatorIFrame) {
var w = device.width + 40;
var h = device.height + 14 + 30 + 3;
title.text(device.name);
emulatorWindow.draggable({
containment: 'window',
opacity: 0.35,
handle: 'div',
});
// emulatorWindow.resizable({
// handles: 'e, s',
// minWidth: w,
// minHeight: h,
// aspectRatio: true
// });
title.css({
height: '30px',
padding: '0 10px',
lineHeight: '30px',
fontSize: '18px',
cursor: 'default',
});
emulatorWindow.css({
position: 'absolute',
top: '10px',
left: '316px',
width: w + 'px',
height: h + 'px',
backgroundColor: '#fff',
zIndex: '5',
});
emulatorIFrame.css({
width: '100%',
height: 'calc(100% - 30px)',
border: '0 none',
});
var close = () => {
$('.emulator-window').remove();
}
emulatorIFrame.load(() => {
emulatorIFrame[0].contentWindow.close = close;
post({ type: "init" });
});
emulatorIFrame.attr('src', url);
return emulatorIFrame[0].contentWindow;
}
}
} catch (err) {
console.log('ERROR', err);
}
}
var emu = window.open(url, "banglewindow", device.emulatorWin);
if (emu.addEventListener) {
emu.addEventListener("load", function () {
post({ type: "init" });
}, false);
} else
if (emu.attachEvent) {
emu.attachEvent("onload", function () {
post({ type: "init" });
}, false);
}
return emu;
}
var device = {
"name" : "Emulator",
"init" : function() {
Espruino.Core.Config.addSection("Emulator", { sortOrder:350, description: "Settings for Bangle.js emulator" });
Espruino.Core.Config.add("EMULATOR_BANGLEJS", {
section : "Emulator",
name : "Enable Bangle.js Emulator",
description : "Enable or disable Bangle.js emulator",
type : "boolean",
defaultValue : true
});
Espruino.Core.Config.add("EMULATOR_BANGLEJS_WINDOW", {
section : "Emulator",
name : "Bangle.js Emulator Windows",
description : "Use a popup window or a internal window to display Bangle.js emulator",
type : { "outside": "Popup window", "inside": "Internal window" },
defaultValue : "outside",
onChange: function () {
if (emu) {
emu.close();
emu = undefined;
}
}
});
},
"getStatus": function(ignoreSettings) {
if (!Espruino.Config.EMULATOR_BANGLEJS && !ignoreSettings)
return {warning:"Disabled in Communications Settings"};
return true;
},
"getPorts": function(callback) {
var emulatorRequested = window.location.search.substr(1).split("&").includes("emulator");
if (emulatorRequested)
Espruino.Config.set("EMULATOR_BANGLEJS", true);
if (!Espruino.Config.EMULATOR_BANGLEJS)
return callback([]);
var port = {
path:'Emulator',
description:'Bangle.js Emulator',
type:"emulator"};
if (emulatorRequested)
port.autoconnect = true;
callback([port], true/*instantPorts*/);
},
"open": function(path, openCallback, receiveCallback, disconnectCallback) {
chooseDevice(function(device) {
if (!device) {
openCallback(null); // flag error
return;
}
callbacks.open = openCallback;
callbacks.receive = receiveCallback;
callbacks.disconnected = function() {
emu = undefined;
callbacks = {};
disconnectCallback();
}
emu = openEmulator(device);
});
},
"write": function(d, callback) {
post({type:"rx",data:d});
setTimeout(callback,10);
},
"close": function() {
if (emu) emu.close();
//callbacks.disconnected(); // called by emu.onunload
emu = undefined;
},
};
Espruino.Core.Serial.devices.push(device);
})();