-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex_builder.html
More file actions
238 lines (218 loc) · 7.46 KB
/
Copy pathindex_builder.html
File metadata and controls
238 lines (218 loc) · 7.46 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
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
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="Demo">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
label {
display: block;
padding: 10px 0 10px 0;
}
div {
background-color: lightgrey;
margin: 15px;
}
textarea {
margin: 5px;
}
p, a, label, button, input {
margin: 5px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
</head>
<body onload="checkExtension(), populatePorts()">
<div>
<p id="lblExtension">Message: ...</p>
<button id="checkExtensionBtn" onclick="checkExtension()">Check if extension is installed/functioning</button>
</div>
<div>
<label>Arduino board:
<select id="boardType">
<option value='{"builder":"arduino:avr:uno","ext":"uno"}'>Uno</option>
</select>
</label>
</div>
<div>
<p>Choose a file(.hex) to upload to an arduino</p>
<form id="uploadFileForm">
<label>File:
<input id="fileInput" type="file"/>
</label>
<input type="submit" id="uploadFileBtn" value="Upload file"/>
</form>
</div>
<div>
<p>Fill in your code to upload to an arduino</p>
<textarea id="codeTextarea" rows="20" cols="80" id="editor">
int led = 13;
void setup(){
pinMode(led, OUTPUT);
}
void loop(){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}</textarea><br/>
<select id="compilerType">
<option value="http://localhost:7000/compile">Local arduino builder</option>
</select>
<select id="portType">
</select>
<button id="verifyTxtBtn" onclick="verifytxt()">Verify Code</button>
<button id="uploadTxtBtn" onclick="uploadtxt()">Upload Code</button>
<button id="copyToClipboardBtn" onclick="copyToClipboard()">Copy to Clipboard</button>
<p id="lblStatus">Status: ...</p>
</div>
<script type="text/javascript">
var uploadFileForm = document.getElementById('uploadFileForm');
var fileInput = document.getElementById('fileInput');
var boardType = document.getElementById('boardType');
var compilerType = document.getElementById('compilerType');
var portType = document.getElementById('portType');
//var verifyBtn = document.getElementById('verifyTxtBtn');
//var uploadFileBtn = document.getElementById('uploadFileBtn');
var lblStatus = document.getElementById('lblStatus');
var lblExtension = document.getElementById('lblExtension');
var extensionActive = false;
uploadFileForm.addEventListener('submit', handleSubmit, false);
/**
* Function checkExtension will poll if the extension is installed or not
*/
function checkExtension() {
chrome.runtime.sendMessage(extensionid, 'check', response => {
if(!response) {
lblExtension.innerHTML = 'No extension installed';
extensionActive = false;
} else {
lblExtension.innerHTML = JSON.stringify(response);
extensionActive = true;
}
});
}
/**
* Function populatePorts will add connected devices to the select box
*/
function populatePorts() {
chrome.runtime.sendMessage(extensionid, 'ports', response => {
if(!response) {
lblExtension.innerHTML = 'No extension installed';
} else {
// empty select
portType.options.length = 0;
// populate list with response
var data = response['message'];
let option;
for (let i = 0; i < data.length; i++) {
option = document.createElement('option');
option.text = data[i].comName;
option.value = data[i].comName;
portType.add(option);
}
}
});
}
/**
* Function handleSubmit() will load a hex-file from the local PC
* and post it to the extension to flash the device
*/
function handleSubmit(e) {
e.preventDefault();
// get the chosen file from the form
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function(event) {
// get the string text of the file
var filecontents = event.target.result;
// get the message payload ready to send to the chrome app
var message = {
board: JSON.parse(boardType.value)['ext'],
port: portType.value,
file: filecontents
};
// post object to extension so that it will flash the file
port.postMessage(message);
};
// we can send the filecontents to the chrome app as plain text
reader.readAsText(file);
}
/**
* Function verifytxt() will return whether or not the code is compilable.
* This function will NOT flash the device.
*/
function verifytxt(){
$.post(compilerType.value, {'data': $("#codeTextarea").val(), 'boardName': JSON.parse(boardType.value)['builder']}, function(data){
console.log(data);
if(data['err'] !== '') {
lblStatus.innerHTML = 'Status: ERROR! ' + data['err'];
} else {
lblStatus.innerHTML = 'Status: SUCCES! ' + data['out'];
}
});
}
/**
* Function uploadtxt() will flash the code in the textbox to the connected device
* 1. POST code to builder to receive the hex-file
* 2. Send received hex-file to the extension
* 3. extension will flash a connected device
*/
function uploadtxt(){
// First check if extension is installed.
if(!extensionActive) {
lblStatus.innerHTML = 'Status: Extension not installed';
} else {
$.post(compilerType.value, {'data': $("#codeTextarea").val(), 'boardName': JSON.parse(boardType.value)['builder']}, function(data){
console.log(data);
if(data['err'] !== '') {
lblStatus.innerHTML = 'Status: ERROR! ' + data['err'];
} else if(JSON.stringify(data['hex']) !== '') {
// get the message payload ready to send to the chrome app
lblStatus.innerHTML = 'Status: SUCCES! ' + data['out'];
var message = {
board: JSON.parse(boardType.value)['ext'],
port: portType.value,
file: data['hex']
};
// post object to extension so that it will flash the file
lblStatus.innerHTML = "Status: flashing to device";
console.log("Flashing file to device");
port.postMessage(message);
} else {
console.log("Compiler returned empty file - Device not flashed.");
lblStatus.innerHTML = "Compiler returned empty file - Device not flashed";
}
});
}
}
/*
* Function copyToClipboard() will copy all the code in the textbox
* to your clipboard for easy Copy-paste.
*/
function copyToClipboard(){
var textarea = document.getElementById("codeTextarea");
textarea.select();
try {
var success = document.execCommand("Copy");
var msg = success ? "successful" : "unsuccesful";
console.log("Copy to clipboad was: " + msg);
lblStatus.innerHTML = "Status: Copy to clipboad was " + msg;
} catch(err) {
console.log("Copy to clipboard failed: " + err);
lblStatus.innerHTML = "Status: Copy to clipboard failed, " + err;
}
}
// open long lived connection with extension (it takes time for flash to complete)
var extensionid = 'mdakeecljhpcnhpckfldneofndikedpl';
var port = chrome.runtime.connect(extensionid);
// log out any responses we get from the chrome app
port.onMessage.addListener(function(msg) {
console.log('Message from extension:', msg);
lblStatus.innerHTML = 'Message from extension: ' + JSON.stringify(msg);
});
</script>
</body>
</html>