-
Notifications
You must be signed in to change notification settings - Fork 2
/
set_paths.pde
291 lines (243 loc) · 7.7 KB
/
set_paths.pde
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Location where we store the previously selected binary's path
String binPathInfoFile = "path.txt";
String pythonPath = "PYTHON_PATH";
String progFilePath = "PROG.PY_PATH";
String binHexFilePath = "BIN_PATH";
String binHexFileName = "binary file [xxx.hex]";
// To be used later by UI, after selecting a port, this will be inserted in upload command
String uploadPortName = "UPLOAD_PORT";
String debugPortName = "DEBUG_PORT";
char[] splChars = {' ', '*', '\\', '/'}; // special characters in file anmes to look out for
String[] flash_cmd = {
pythonPath,
"-u",
progFilePath,
"-t",
"uart",
"-u",
uploadPortName,
"-b",
"921600",
"-wd",
"1",
"-d",
"attiny1607",
"--fuses",
"0:0b00000000",
"2:0x02",
"6:0x00",
"7:0x00",
"8:0x00",
"-f",
binHexFilePath,
"-a",
"write"
};
void sysinfo() {
//println( "SYS INFO :");
println( "System:\t" + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch") );
//println( "JAVA:\t" + System.getProperty("java.home") + " rev: " +javaVersionName);
println("\nOPERATION STEPS:");
println("[1]. Select a port");
println("[2]. Select firmware.");
println("[3]. Upload.");
}
void printFlashCommand(String[] cmd) {
StringBuffer cmd_buffer = new StringBuffer();
for (int i = 0; i < cmd.length; i++) {
cmd_buffer.append(cmd[i] + " ");
}
println("\nFLASHING CMD:\t" + cmd_buffer.toString());
}
// An utility fucntion to check if strings have spaces or "*" in them or not
// Will be used to notify users to not have spaces in the binary file names.
boolean fileNameOk(String fp) {
boolean ok = false;
ArrayList<Boolean> containsSplChar = new ArrayList<Boolean>();
for (int i=0; i<splChars.length; i++) {
containsSplChar.add(false);
}
for (int i=0; i<splChars.length; i++) {
String[] p1 = split(fp, splChars[i]);
if (p1.length > 1) {
containsSplChar.set(i, true);
}
}
if (containsSplChar.contains(true)) {
ok = false;
} else {
ok = true;
}
return ok;
}
int OS() {
int osn = 0;
String fullOSName = System.getProperty("os.name");
String shortOSName = fullOSName.substring(0, 3).toLowerCase();
if (shortOSName.equals("mac")) {
// TBD accomodate other mac OS name types (may be, if they vary)
osn = 0;
}
if (shortOSName.equals("lin")) {
// TBD accomodate other windows name types (may be, if they vary)
osn = 1;
}
if (shortOSName.equals("win")) {
// TBD accomodate other windows name types (may be, if they vary)
osn = 2;
}
return osn;
}
String getPythonPath(int _osn) {
String pyPath = "";
switch (_osn) {
case 0:
// macOS specific portable python3 from tools dir
// TBD fix bundled python3 issues.
// pyPath = sketchPath() + "/tools/python3/macos/python3/python3";
pyPath = "python3";
break;
case 1:
// linux specific portable python3 from tools dir
pyPath = sketchPath() + "/tools/python3/linux/python3";
break;
case 2:
// windows specific portable python3 from tools dir
pyPath = sketchPath() + "\\tools\\python3\\windows\\python3.exe";
break;
}
return pyPath;
}
String getPythonProgScptPath(int _osn) {
String pyScptPath = "";
switch (_osn) {
case 0:
// macOS: where the prog.py is kept
pyScptPath = sketchPath() + "/tools/prog.py";
break;
case 1:
// linux: where the prog.py is kept
pyScptPath = sketchPath() + "/tools/prog.py";
break;
case 2:
// windows: where the prog.py is kept
pyScptPath = sketchPath() + "\\tools\\prog.py";
break;
}
return pyScptPath;
}
// ------------------------------------- //
// function to strip file name form path //
// ------------------------------------- //
String getJustFileName(String filePath) {
IntList arrayOfSlashIndices = new IntList();
int idxOfSlash = 0;
// For mac or linux
if (OS() == 0 || OS() == 1) {
idxOfSlash = filePath.indexOf("/");
while (idxOfSlash >= 0) {
//println(idxOfSlash);
if (idxOfSlash!=0) {
arrayOfSlashIndices.append(idxOfSlash);
}
idxOfSlash=filePath.indexOf("/", idxOfSlash + 1);
}
}
// For windows
if (OS() == 2) {
idxOfSlash = filePath.indexOf("\\");
while (idxOfSlash >= 0) {
if (idxOfSlash!=0) {
arrayOfSlashIndices.append(idxOfSlash);
}
idxOfSlash=filePath.indexOf("\\", idxOfSlash + 1);
}
}
int lastIdxOfSlash = arrayOfSlashIndices.get(arrayOfSlashIndices.size()-1);
String filename = filePath.substring(lastIdxOfSlash+1, filePath.length());
return filename;
}
void binaryFileSelected(File selection) {
if (selection == null) {
println("\nSELECTION ABORTED");
} else {
binHexFilePath = selection.getAbsolutePath();
binHexFileName = getJustFileName(binHexFilePath);
// Mitigate some in file names and re-format the Path
fileNameOk(binHexFileName);
if (fileNameOk(binHexFileName) == false) {
enableFlashing = false;
println("\n[WARNING]");
println("INVALID FILE NAME/TYPE");
println("The File has either SPACE/S or one of these characters (/ \\ *) in it.");
println("Please RENAME the file without these characters");
println("\nNOTE: Although you can use \"_\" in the file name");
enableFlashing = false;
//selection = null;
return ;
}
// Save the file path info in a text file, for next time loading
try {
PrintWriter output = createWriter(binPathInfoFile); // create a new file
output.println(binHexFilePath);
output.flush(); // Writes the remaining data to the file
output.close();
println("\nINFO FILE SAVED WITH BIN PATH INFO");
}
catch (Exception e) {
println("\nERROR: [X] INFO FILE containing just loaded firmware's path could not be saved");
return ;
}
println("\nSELECTED BINARY FILE PATH:\t" + binHexFilePath);
println("\nSELECTED BINARY FILE:\t" + binHexFileName);
binFileLabel.setText(binHexFileName);
// Update binary PATH in flash command
flash_cmd[20] = binHexFilePath;
enableFlashing = true;
}
}
// On first load ...
void loadAndSetBinaryFilePath(String filename) {
try {
BufferedReader reader = createReader(filename);
String line = "";
line = reader.readLine();
if (line == null) {
return ;
}
print("\nFOUND BIN PATH FROM INFO FILE:");
binHexFilePath = line; // the first line is the path of the binary
binHexFileName = getJustFileName(binHexFilePath);
// Mitigate some spl chars in file names and re-format the Path
// binHexFilePath = accountForSpaces(binHexFilePath);
if (fileNameOk(binHexFileName) == false) {
enableFlashing = false;
println("\n[WARNING] INVALID FILE NAME/TYPE");
println("Tempered the name in the log (where we logged the last used file's location)");
println("Now it has either SPACE/S or these characters (/ \\ *) in it.");
println("Please RENAME the file name there (in the log file) and remove these characters.");
println("\nNOTE: You can use \"_\" in the file name");
enableFlashing = false;
return ;
}
// Also check if the file truely exists or not?
File f = new File(binHexFilePath);
if (!f.exists()) {
println("\n[WARNING]");
println("Previously used file", binHexFileName, "doesn't exist in the path we recorded!");
println("Or may be it was renamed.");
println("Reload the firmware file to flash");
enableFlashing = false;
return ;
}
println("\nPreviously used file exists!");
println(binHexFilePath);
binFileLabel.setText(binHexFileName);
// Update binary PATH in flash command
flash_cmd[20] = binHexFilePath;
enableFlashing = true;
}
catch (Exception e) {
return ;
}
}