Repository navigation
Expand file tree
/
Copy pathbackend.js
More file actions
65 lines (59 loc) · 2.19 KB
/
Copy pathbackend.js
File metadata and controls
65 lines (59 loc) · 2.19 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
const { ipcRenderer } = require('electron');
window.addEventListener('DOMContentLoaded', () => {
const codeArea = document.getElementById('codeArea');
const uploadButton = document.getElementById('uploadButton');
const refreshButton = document.getElementById('refreshButton');
const status = document.getElementById('status');
const portDropdown = document.getElementById('portDropdown');
async function refreshPorts() {
try {
const ports = await ipcRenderer.invoke('list-ports');
portDropdown.innerHTML = '';
ports.forEach(port => {
const option = document.createElement('option');
option.value = port.path;
option.textContent = port.friendlyName || port.path;
portDropdown.appendChild(option);
});
if (ports.length === 0) {
status.textContent = 'Status: No ports available.';
}
} catch (error) {
status.textContent = `Status: Error - ${error.message}`;
}
}
refreshPorts();
refreshButton.addEventListener('click', () => {
codeArea.value = '';
status.textContent = 'Status: Idle';
refreshPorts();
});
let isExecuting = false;
uploadButton.addEventListener('click', () => {
const code = codeArea.value;
if (isExecuting) {
status.textContent = 'Status: Some other code is executing';
return;
}
if (!code.trim()) {
status.textContent = 'Status: No code found';
return;
}
const preparedCode = `
import pyfirmata
board = pyfirmata.Arduino('${portDropdown.value}')
`;
status.textContent = 'Status: Executing';
isExecuting = true;
ipcRenderer.invoke('execute-code', preparedCode)
.then((result) => {
status.textContent = `Status: ${result}`;
})
.catch((error) => {
status.textContent = `Status: Error - ${error.message}`;
})
.finally(() => {
isExecuting = false;
});
});
});