-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
IDE_functions.js
188 lines (182 loc) · 9 KB
/
IDE_functions.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
/**
* @license
* Copyright 2020 Sébastien CANET
* SPDX-License-Identifier: GPL-3.0
*/
/**
* @fileoverview Utility functions for handling typed variables.
* fake IDE code Arduino
* boardSelector: boards list
* serialMenu: serial port list
* serialConnectButton: open modal with serial console
* verifyButton: verify and compile in hex file
* uploadButton: upload hex file in Arduino board
* @author [email protected] (Sébastien CANET)
*/
const {ipcRenderer} = require('electron');
const {exec} = require('child_process');
const fs = require('fs-extra');
const tableify = require('tableify')
const SerialPort = require('serialport');
//populate COM port modal with all com port detected on system
document.getElementById('serialButton').addEventListener("mouseover", function (event) {
SerialPort.list().then(ports => {
let portsList = ports.map(function(obj) {
return {
path: obj.path,
manufacturer: obj.manufacturer,
vendorId: obj.vendorId,
productId: obj.productId
}
});
if (portsList.length === 0) {
document.getElementById('portListModalBody').innerHTML = "Aucun port n'est disponible";
} else {
document.getElementById('portListModalBody').innerHTML = tableify(portsList);
}
})
});
//COM port list inside the modal
document.getElementById('serialMenu').addEventListener("mouseover", function (event) {
SerialPort.list().then(ports => {
document.getElementById('serialMenu').options.length = 0;
ports.forEach(function (port) {
var option = document.createElement('option');
option.value = port.path;
option.text = port.path;
document.getElementById('serialMenu').appendChild(option);
});
});
});
window.addEventListener('load', function load(event) {
document.getElementById('verifyButton').onclick = function (event) {
try {
fs.accessSync('.\\compiler\\tmp', fs.constants.W_OK);
} catch (err) {
fs.mkdirSync('.\\compiler\\tmp', {
recursive: false
}, (err) => {
if (err)
throw err;
});
}
var file_path = '.\\tmp';
var file = '.\\compiler\\tmp\\tmp.ino';
var data = document.getElementsByClassName("ace_content")[0].innerText;
var boardSelected = document.getElementById('boardMenu').value;
if ((boardSelected == "none") || (boardSelected == "...") || (boardSelected == "") || (boardSelected == "undefined")) {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = MSG['IDE_select_board'];
return;
} else {
document.getElementById('content_serial').style.color = '#FFFFFF';
document.getElementById('content_serial').innerHTML = MSG['IDE_upload1'] + profile.default['description'];
var upload_arg = profile.default['upload_arg'];
if (document.getElementById('detailedCompilation').checked === true)
var cmd = 'arduino-cli.exe compile -v -b ' + upload_arg + ' ' + file_path;
else
var cmd = 'arduino-cli.exe compile -b ' + upload_arg + ' ' + file_path;
fs.writeFile(file, data, (err) => {
if (err)
return console.log(err);
});
document.getElementById('content_serial').innerHTML += '<br>' + MSG['IDE_verif_progress'];
exec(cmd, {
cwd: './compiler'
}, (error, stdout, stderr) => {
if (error) {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = stderr;
return;
}
document.getElementById('content_serial').style.color = '#00FF00';
document.getElementById('content_serial').innerHTML = stdout + '<br>' + MSG['IDE_verif_ok'];
});
}
};
document.getElementById('uploadButton').onclick = function (event) {
var file_path = '.\\tmp';
var boardSelected = document.getElementById('boardMenu').value;
var comPortSelected = document.getElementById('serialMenu').value;
if ((boardSelected == "none") || (boardSelected == "...") || (boardSelected == "") || (boardSelected == "undefined")) {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = MSG['IDE_select_board'];
return;
} else {
if (comPortSelected === "none") {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = MSG['IDE_select_port'];
return;
} else {
document.getElementById('content_serial').style.color = '#FFFFFF';
document.getElementById('content_serial').innerHTML = MSG['IDE_upload1'] + profile.default['description'] + MSG['IDE_upload2'] + comPortSelected;
document.getElementById('content_serial').innerHTML += '<br>' + MSG['IDE_upload3'];
var upload_arg = profile.default['upload_arg'];
}
}
if (document.getElementById('detailedCompilation').checked === true)
var cmd = 'arduino-cli.exe upload -v -p ' + comPortSelected + ' -b ' + upload_arg + ' ' + file_path;
else
var cmd = 'arduino-cli.exe upload -p ' + comPortSelected + ' -b ' + upload_arg + ' ' + file_path;
exec(cmd, {
cwd: './compiler'
}, (error, stdout, stderr) => {
if (error) {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = stderr;
return;
}
document.getElementById('content_serial').style.color = '#00FF00';
document.getElementById('content_serial').innerHTML = stdout + '<br>' + MSG['IDE_upload_ok'];
const path = require('path');
fs.readdir('.\\compiler\\tmp', (err, files) => {
if (err)
throw err;
for (const file of files) {
fs.unlink(path.join('.\\compiler\\tmp', file), err => {
if (err)
throw err;
});
}
});
});
};
document.getElementById('serialConnectButton').addEventListener('click', function () {
var langChoice = document.getElementById('languageMenu').value;
var boardSelected = document.getElementById('boardMenu').value;
var comPortSelected = document.getElementById('serialMenu').value;
if ((boardSelected == "none") || (boardSelected == "...") || (boardSelected == "") || (boardSelected == "undefined")) {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = MSG['IDE_select_board'];
return;
} else if (comPortSelected === "none") {
document.getElementById('content_serial').style.color = '#FF0000';
document.getElementById('content_serial').innerHTML = MSG['IDE_select_port'];
return;
} else {
document.getElementById('content_hoverButton').style.color = '#FFFFFF';
document.getElementById('content_hoverButton').innerHTML = MSG['IDE_connect'] + comPortSelected;
localStorage.setItem("comPort", comPortSelected);
localStorage.setItem("availableSpeed", JSON.stringify(profile.default['serialList']));
ipcRenderer.send("serialConnect", langChoice);
}
});
document.getElementById('wiringButton').addEventListener('click', function () {
// var val = location.search.match(new RegExp('[?&]lang=([^&]+)'));
// var argLangChoice = val ? decodeURIComponent(val[1].replace(/\+/g, '%20')) : 'en';
var langChoice = document.getElementById('languageMenu').value;
ipcRenderer.send("hackCable", langChoice);
});
document.getElementById('factoryButton').addEventListener('click', function () {
// var val = location.search.match(new RegExp('[?&]lang=([^&]+)'));
// var argLangChoice = val ? decodeURIComponent(val[1].replace(/\+/g, '%20')) : 'en';
var langChoice = document.getElementById('languageMenu').value;
ipcRenderer.send("blockFactory", langChoice);
});
document.getElementById('htmlButton').addEventListener('click', function () {
// var val = location.search.match(new RegExp('[?&]lang=([^&]+)'));
// var argLangChoice = val ? decodeURIComponent(val[1].replace(/\+/g, '%20')) : 'en';
var langChoice = document.getElementById('languageMenu').value;
ipcRenderer.send("blocklyHTML", langChoice);
});
});