forked from kerber/codecard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
178 lines (146 loc) · 4.3 KB
/
renderer.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
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
window.ELECTRON_DISABLE_SECURITY_WARNINGS = true;
const connectBtn = document.getElementById('connectBtn');
const status = document.getElementById('status');
const terminalContainer = document.getElementById('terminal-container')
const terminal = document.getElementById('term')
const SerialPort = require('serialport')
var dataBuffer = ''
var serialport = ''
var term = new Terminal({
fontFamily: 'Fira Code, Iosevka, monospace',
fontSize: 12,
experimentalCharAtlas: 'dynamic',
cursorBlink: true ,
bellStyle: 'sound'
});
term.open(terminal);
Terminal.applyAddon(fit);
fit.fit(term);
var shellprompt = '\x1B[1;3;31mcodecard\x1B[0m$ ';
term.prompt = function () {
// term.write('\r\n' + shellprompt);
term.write(shellprompt);
};
var port;
detectBadge()
connectBtn.onclick = function () {
if (connectBtn.innerHTML == 'Connect') {
terminalContainer.style.display === 'block'
port.open()
} else if (connectBtn.innerHTML == 'Disconnect') {
terminalContainer.style.display === 'none'
port.close()
} else if (connectBtn.innerHTML == 'Refresh') {
detectBadge()
}
}
function detectBadge(){
disableTerminal()
SerialPort.list((err, ports) => {
if (err) {
status.textContent = err.message
return
}
for (var i = 0; i < ports.length; i++) {
if (ports[i].comName.includes('COM') || ports[i].comName.includes('ttyUSB') || ports[i].comName.includes('wchusbserial')) {
serialport = ports[i].comName;
status.textContent = 'Card detected at: \'' + serialport + '\''
connectBtn.innerHTML = 'Connect'
connect();
return;
} else {
status.textContent = 'No card detected'
connectBtn.innerHTML = 'Refresh'
}
}
})
}
function disableTerminal() {
document.getElementsByClassName('terminal')[0].style.display = 'none'
}
function enableTerminal() {
document.getElementsByClassName('terminal')[0].style.display = 'block'
}
function connect() {
port = new SerialPort(serialport, {
autoOpen: false,
baudRate: 115200
});
const parsers = SerialPort.parsers;
const parser = new parsers.Readline({
delimiter: '\r\n'
});
port.pipe(parser);
port.on('open', () => {
console.log('Port open');
term.prompt()
term.focus()
status.textContent = 'Connected to port ' + serialport
connectBtn.innerHTML = 'Disconnect'
enableTerminal()
// port.write('help\r\n', (err) => {
// if (err) { return console.log('Error: ', err.message) }
// //console.log('message written');
// });
});
port.on('close', () => {
status.textContent = 'Disconnected from port \'' + serialport + '\''
term.clear()
term.write('\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b');
console.log('Port closed');
connectBtn.innerHTML = 'Connect'
disableTerminal()
})
parser.on('data', (data) => {
if (data.includes('>>>')) {
term.prompt()
term.focus()
return
}
term.writeln(data);
});
term.on('data', function(data) {
if (data.indexOf('\r\n') == -1 && encodeURI(data) != '%7F'){
dataBuffer+=data;
}else if (encodeURI(data) === '%7F') {
dataBuffer = dataBuffer.substring(0, dataBuffer.length - 1)
}
});
term.on('paste', function (data, ev) {
term.write(data)
});
term.on('key', function (key, ev) {
var printable = (
!ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey && !ev.spaceKey
);
if (dataBuffer.indexOf('clear') > -1 ) {
term.clear()
term.write('\b \b\b \b\b \b\b \b\b \b');
dataBuffer=''
return
}
if (ev.keyCode == 13 && dataBuffer.trim() != '') {
port.write(dataBuffer)
term.write('\r\n')
dataBuffer = ''
} else if (ev.keyCode == 8) {
if (term._core.buffer.x > 10) {
term.write('\b \b');
}
} else if (key == '[D') {
if (term._core.buffer > 10) {
term._core.buffer = term._core.buffer - 1
}
} else if (printable && key.trim() != '') {
//console.log('key', key)
term.write(key);
}
// else if (key.trim() == '') {
// term.write('\r\n')
// term.prompt()
// }
});
}