-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
244 lines (223 loc) · 7.16 KB
/
Copy pathtest.html
File metadata and controls
244 lines (223 loc) · 7.16 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
239
240
241
242
243
244
<!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>
<div>
<a href="https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi">Install chrome CORS extension here.</a>
</div>
<div>
<label>Arduino board:
<select id="boardType">
<option value="uno">Uno</option>
<option value="mega">Mega</option>
<option value="micro">Micro</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="https://codebender.cc/utilities/compile/">Codebender Compiler</option>
<option value="http://localhost:8000/blockly4arduino/v1">Local Compiler</option>
</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 verifyBtn = document.getElementById('verifyTxtBtn');
var uploadFileBtn = document.getElementById('uploadFileBtn');
var lblStatus = document.getElementById('lblStatus');
uploadFileForm.addEventListener('submit', handleSubmit, false);
function handleSubmit(e) {
e.preventDefault();
// get the file chosen from the form
var file = fileInput.files[0];
var reader = new FileReader();
// get the requested board name
var board = boardType.value;
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: board,
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(){
var data = {
"files":[
{
"filename":"hello.ino",
"content": $("#codeTextarea").val()
}
],
"libraries":{},
"format":"syntax", // == syntax, for verification
"version":"105",
"build":{
"mcu":"atmega328p",
"f_cpu":"16000000L",
"core":"arduino",
"variant":"standard"
}
};
$.ajax({
url: compilerType.value,
type:"POST",
dataType:"json",
data: JSON.stringify(data),
success: function(data) {
lblStatus.innerHTML = "Status: finished compiling hex-file";
console.log("Success: Finished compiling hex-file");
console.log(data);
// get the message payload ready to send to the chrome app
if(data["success"] == true){
lblStatus.innerHTML = "Status: Code contains no errors!";
console.log("Verification returns true: code contains no errors");
} else {
lblStatus.innerHTML = "Status: Error in code, " + data["debug"];
console.log("Verification failed: error in code, " + data["debug"]);
}
},
error: function(data){
console.log("Compile error or no connection with the compiler: " + data);
}
});
}
/**
* Function uploadtxt() will flash the code in the textbox to the connected device
* 1. POST code to compiler to receive the hex-file
* 2. Send received hex-file to the extension
* 3. extension will flash the device
*/
function uploadtxt(){
var data = {
"files":[
{
"filename":"hello.ino",
"content": $("#codeTextarea").val()
}
],
"libraries":{},
"format":"hex",
"version":"105",
"build":{
"mcu":"atmega328p",
"f_cpu":"16000000L",
"core":"arduino",
"variant":"standard"
}
};
$.ajax({
url:"http://localhost:8000/blockly4arduino/v1",
type:"POST",
dataType:"json",
data: JSON.stringify(data),
success: function(data) {
lblStatus.innerHTML = "Status: finished compiling hex-file";
console.log("Success: Finished compiling hex-file");
console.log(data);
// get the message payload ready to send to the chrome app
if(JSON.stringify(data["output"]) !== "{}"){
var message = {
board: boardType.value,
file: data["output"]
};
// 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";
}
},
error: function(data){
console.log("Compile error or no connection with the compiler: " + data);
}
});
}
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 = 'bjcbappagjehfagohgcjcklbfbdhcdkk';
var port = chrome.runtime.connect(extensionid);
// log out any responses we get from the chrome app
port.onMessage.addListener(function(msg) {
console.log('extension is done:', msg);
lblStatus.innerHTML = 'extension is done: ' + msg;
});
</script>
</body>
</html>