-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
index.js
445 lines (415 loc) · 14.5 KB
/
index.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
var { ipcRenderer, shell, clipboard } = require("electron")
var { exec } = require('child_process')
var sp = require('serialport')
var fs = require('fs')
var path = require('path')
var appVersion = window.require('electron').remote.app.getVersion()
var arduino_basepath = process.platform == 'win32' ? arduino_basepath = './compilation/arduino' : path.join(__dirname, '../../compilation/arduino')
var arduino_ide_cmd = process.platform == 'win32' ? 'arduino-cli.exe' : arduino_ide_cmd = path.join(__dirname, '../../compilation/arduino/arduino-cli')
window.addEventListener('load', function load(event) {
var quitDiv = '<button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>'
var checkBox = document.getElementById('verifyUpdate')
var portserie = document.getElementById('portserie')
var messageDiv = document.getElementById('messageDIV')
localStorage.setItem("verif",false)
document.getElementById('versionapp').textContent = " Otto Blockly V" + appVersion
function uploadOK(){
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.upload + ': ✅ OK code uploaded' + quitDiv
$('#message').modal('show');
setTimeout(function() {
$('#message').modal('hide');
}, 3000);
}
$('#btn_forum').on('click', function(){
shell.openExternal('https://discord.gg/CZZytnw')
})
$('#btn_site').on('click', function(){
shell.openExternal('https://www.ottodiy.com/')
})
$('#btn_contact').on('click', function(){
shell.openExternal('https://github.com/OttoDIY/blockly/issues')
})
$('#portserie').mouseover(function(){
sp.list(function(err,ports) {
var nb_com = localStorage.getItem("nb_com"), menu_opt = portserie.getElementsByTagName('option')
if(ports.length > nb_com){
ports.forEach(function(port){
if (port.vendorId){
var opt = document.createElement('option')
opt.value = port.comName
opt.text = port.comName
portserie.appendChild(opt)
localStorage.setItem("com",port.comName)
}
})
localStorage.setItem("nb_com",ports.length)
localStorage.setItem("com",portserie.options[1].value)
}
if(ports.length < nb_com){
while(menu_opt[1]) {
portserie.removeChild(menu_opt[1])
}
localStorage.setItem("com","com")
localStorage.setItem("nb_com",ports.length)
}
})
})
$('#btn_copy').on('click', function(){
clipboard.writeText($('#pre_previewArduino').text())
})
$('#btn_bin').on('click', function(){
if (localStorage.getItem('verif') == "false"){
$("#message").modal("show")
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.verif + quitDiv
return
}
localStorage.setItem("verif",false)
ipcRenderer.send('save-bin')
})
$.ajax({
cache: false,
url: "../config.json",
dataType: "json",
success : function(data) {
$.each(data, function(i, update){
if (update=="true") {
$('#verifyUpdate').prop('checked', true)
checkBox.dispatchEvent(new Event('change'))
ipcRenderer.send("version", "")
} else {
$('#verifyUpdate').prop('checked', false)
checkBox.dispatchEvent(new Event('change'))
}
})
}
})
checkBox.addEventListener('change', function(event){
if (event.target.checked) {
fs.writeFile('config.json', '{ "update": "true" }', function(err){
if (err) return console.log(err)
})
} else {
fs.writeFile('config.json', '{ "update": "false" }', function(err){
if (err) return console.log(err)
})
}
})
sp.list(function(err,ports){
var opt = document.createElement('option')
opt.value = "com"
opt.text = Blockly.Msg.com1
portserie.appendChild(opt)
ports.forEach(function(port) {
if (port.vendorId){
var opt = document.createElement('option')
opt.value = port.comName
opt.text = port.comName
portserie.appendChild(opt)
}
})
localStorage.setItem("nb_com",ports.length)
if (portserie.options.length > 1) {
portserie.selectedIndex = 1
localStorage.setItem("com",portserie.options[1].value)
} else {
localStorage.setItem("com","com")
}
})
$('#btn_version').on('click', function(){
$('#aboutModal').modal('hide')
ipcRenderer.send("version", "")
})
$('#btn_term').on('click', function(){
if (portserie.value=="com"){
$("#message").modal("show")
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.com2 + quitDiv
return
}
if (localStorage.getItem("prog") == "python") { ipcRenderer.send("repl", "") } else { ipcRenderer.send("prompt", "") }
})
$('#btn_factory').on('click', function(){
ipcRenderer.send("factory", "")
})
$('#btn_verify').on('click', function(){
if (localStorage.getItem('content') == "off") {
var data = editor.getValue()
} else {
var data = $('#pre_previewArduino').text()
}
var carte = localStorage.getItem('card')
var prog = localStorage.getItem('prog')
var com = portserie.value
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.check + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
if (prog == "python") {
fs.writeFile('./compilation/python/py/sketch.py', data, function(err){
if (err) return console.log(err)
})
exec('python -m pyflakes ./py/sketch.py', {cwd:"./compilation/python"}, function(err, stdout, stderr){
if (stderr) {
rech=RegExp('token')
if (rech.test(stderr)){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.error + quitDiv
} else {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ':✅ OK' + quitDiv
})
} else {
//fs.writeFile('./compilation/arduino/ino/sketch.ino', data, function(err){
fs.writeFile(`${arduino_basepath}/sketch/sketch.ino`, data, function(err){
if (err) return console.log(err)
})
var upload_arg = window.profile[carte].upload_arg
var cmd = `${arduino_ide_cmd} compile --fqbn ` + upload_arg +' sketch/sketch.ino'
/*
exec( cmd, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
//exec('verify.bat ' + carte, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
if (stderr) {
rech=RegExp('token')
if (rech.test(stderr)){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.error + quitDiv
} else {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': OK' + quitDiv
}) */
exec(cmd , {cwd: arduino_basepath} , (error, stdout, stderr) => {
if (error) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = error.toString() + quitDiv
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': ✅ Code is ready to upload' + quitDiv
$('#message').modal('show');
setTimeout(function() {
$('#message').modal('hide');
}, 3000);
})
}
localStorage.setItem("verif",true)
})
$('#btn_flash').on('click', function(){
var data = $('#pre_previewArduino').text()
var carte = localStorage.getItem('card')
var prog = profile[carte].prog
var speed = profile[carte].speed
var cpu = profile[carte].cpu
var com = portserie.value
var upload_arg = window.profile[carte].upload_arg
if ( com == "com" ){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.com2 + quitDiv
return
}
if ( localStorage.getItem('verif') == "false" ){
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.check + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
//fs.writeFile('./compilation/arduino/ino/sketch.ino', data, function(err){
fs.writeFile(`${arduino_basepath}/sketch/sketch.ino`, data, function(err){
if (err) return console.log(err)
})
var cmd = `${arduino_ide_cmd} compile --fqbn ` + upload_arg +' sketch/sketch.ino'
exec(cmd , {cwd: `${arduino_basepath}`} , (error, stdout, stderr) => {
if (error) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = error.toString() + quitDiv
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': ✅ OK' + quitDiv
/*
exec( cmd, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
//exec('verify.bat ' + carte, {cwd:'./compilation/arduino'}, function(err, stdout, stderr){
if (stderr) {
rech=RegExp('token')
if (rech.test(stderr)){
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = Blockly.Msg.error + quitDiv
} else {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
return
}
messageDiv.style.color = '#009000'
messageDiv.innerHTML = Blockly.Msg.check + ': OK' + quitDiv */
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.upload + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
cmd = `${arduino_ide_cmd} upload --port `+portserie.value +' --fqbn ' + upload_arg +' sketch/sketch.ino'
exec( cmd, {cwd:`${arduino_basepath}`}, function(err, stdout, stderr){
//exec('flash.bat ' + cpu + ' ' + prog + ' '+ com + ' ' + speed, {cwd: './compilation/arduino'} , function(err, stdout, stderr){
if (err) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
return
}
uploadOK()
}) })
localStorage.setItem("verif",false)
return
}
messageDiv.style.color = '#000000'
messageDiv.innerHTML = Blockly.Msg.upload + '<i class="fa fa-spinner fa-pulse fa-1_5x fa-fw"></i>'
if ( prog == "python" ) {
if ( cpu == "cortexM0" ) {
var cheminFirmware = "./compilation/python/firmware.hex"
var fullHexStr = ""
exec('wmic logicaldisk get volumename', function(err, stdout){
if (err) return console.log(err)
localStorage.setItem("volumename", stdout.split('\r\r\n').map(value => value.trim()))
})
exec('wmic logicaldisk get name', function(err, stdout){
if (err) return console.log(err)
localStorage.setItem("name", stdout.split('\r\r\n').map(value => value.trim()))
})
var volume = localStorage.getItem("volumename")
var drive = localStorage.getItem("name")
var volumeN = volume.split(',')
var driveN = drive.split(',')
var count = volumeN.length
var disk = ""
for (var i = 0 ; i < count ; i++) {
if (volumeN[i]=="MICROBIT") disk = driveN[i]
}
if (disk!="") {
fs.readFile(cheminFirmware, function(err, firmware){
firmware = String(firmware)
fullHexStr = upyhex.injectPyStrIntoIntelHex(firmware, data)
fs.writeFile(disk + '\sketch.hex', fullHexStr, function(err){
if (err) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
}
})
})
setTimeout(uploadOK, 7000)
} else {
messageDiv.style.color = '#000000'
messageDiv.innerHTML = 'Connect micro:bit!' + quitDiv
}
} else {
exec( 'python -m ampy -p ' + com + ' -b 115200 -d 1 run --no-output ./py/sketch.py', {cwd: "./compilation/python"} , function(err, stdout, stderr){
if (err) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
return
}
uploadOK()
})
}
} else {
cmd = `${arduino_ide_cmd} upload --port `+portserie.value +' --fqbn ' + upload_arg +' sketch/sketch.ino'
exec( cmd, {cwd:`${arduino_basepath}`}, function(err, stdout, stderr){
//exec('flash.bat ' + cpu + ' ' + prog + ' '+ com + ' ' + speed, {cwd: './compilation/arduino'} , function(err, stdout, stderr){
if (err) {
messageDiv.style.color = '#ff0000'
messageDiv.innerHTML = err.toString() + quitDiv
return
}
uploadOK()
})
}
localStorage.setItem("verif",false)
})
$('#btn_saveino').on('click', function(){
if (localStorage.getItem("prog") == "python") { ipcRenderer.send('save-py') } else { ipcRenderer.send('save-ino') }
})
$('#btn_saveXML').on('click', function(){
if (localStorage.getItem("content") == "on") {
ipcRenderer.send('save-bloc')
} else {
if (localStorage.getItem("prog") == "python") { ipcRenderer.send('save-py') } else { ipcRenderer.send('save-ino') }
}
})
ipcRenderer.on('saved-ino', function(event, path){
var code = $('#pre_previewArduino').text()
if (path === null) {
return
} else {
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-py', function(event, path){
var code = $('#pre_previewArduino').text()
if (path === null) {
return
} else {
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-bloc', function(event, path){
if (path === null) {
return
} else {
var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace)
var toolbox = localStorage.getItem("toolbox")
if (!toolbox) {
toolbox = $("#toolboxes").val()
}
if (toolbox) {
var newel = document.createElement("toolbox")
newel.appendChild(document.createTextNode(toolbox))
xml.insertBefore(newel, xml.childNodes[0])
}
var toolboxids = localStorage.getItem("toolboxids")
if (toolboxids === undefined || toolboxids === "") {
if ($('#defaultCategories').length) {
toolboxids = $('#defaultCategories').html()
}
}
var code = Blockly.Xml.domToPrettyText(xml)
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-bin', function(event, path){
if (path === null) {
return
} else {
var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace)
var toolbox = localStorage.getItem("toolbox")
if (!toolbox) {
toolbox = $("#toolboxes").val()
}
if (toolbox) {
var newel = document.createElement("toolbox")
newel.appendChild(document.createTextNode(toolbox))
xml.insertBefore(newel, xml.childNodes[0])
}
var toolboxids = localStorage.getItem("toolboxids")
if (toolboxids === undefined || toolboxids === "") {
if ($('#defaultCategories').length) {
toolboxids = $('#defaultCategories').html()
}
}
var code = Blockly.Xml.domToPrettyText(xml)
var res = path.split(".")
fs.writeFile(res[0]+'.bloc', code, function(err){
if (err) return console.log(err)
})
fs.copyFile(`${arduino_basepath}/build/sketch.ino.with_bootloader.hex`, res[0]+'_with_bootloader.hex', (err) => {if (err) throw err})
fs.copyFile(`${arduino_basepath}/build/sketch.ino.hex`, res[0]+'.hex', (err) => {if (err) throw err})
fs.copyFile(`${arduino_basepath}/ino/sketch.ino`, res[0]+'.ino', (err) => {if (err) throw err})
}
})
})