Skip to content

Commit cffb965

Browse files
author
Arduino Board
committed
fixed bug in the URL that appears in the output window
1 parent 27df5d4 commit cffb965

File tree

9 files changed

+347
-129
lines changed

9 files changed

+347
-129
lines changed

.classpath

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
<classpath>
33
<classpathentry excluding="processing/app/tools/format/|processing/app/tools/format/src/|processing/app/Trace.java|processing/app/RunnerClassLoader.java" kind="src" path="app/src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5-
<classpathentry combineaccessrules="false" kind="src" path="/core"/>
6-
<classpathentry kind="lib" path="build/shared/lib/antlr.jar"/>
7-
<classpathentry kind="lib" path="build/shared/lib/registry.jar"/>
8-
<classpathentry kind="lib" path="build/shared/lib/apple.jar"/>
5+
<classpathentry kind="lib" path="app/lib/egit.jar"/>
6+
<classpathentry kind="lib" path="app/lib/gson.jar"/>
7+
<classpathentry kind="lib" path="app/lib/httpclient.jar"/>
8+
<classpathentry kind="lib" path="app/lib/httpcore.jar"/>
9+
<classpathentry kind="lib" path="app/lib/antlr.jar"/>
10+
<classpathentry kind="lib" path="app/lib/apple.jar"/>
11+
<classpathentry kind="lib" path="app/lib/ecj.jar"/>
12+
<classpathentry kind="lib" path="app/lib/jna.jar"/>
13+
<classpathentry kind="lib" path="app/lib/RXTXcomm.jar"/>
914
<classpathentry kind="output" path="app/bin"/>
1015
</classpath>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
#Thu Jan 10 10:50:38 PST 2008
1+
#Tue Jul 26 14:48:34 CDT 2011
22
eclipse.preferences.version=1
3-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2
4-
org.eclipse.jdt.core.compiler.compliance=1.4
5-
org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning
6-
org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning
7-
org.eclipse.jdt.core.compiler.source=1.3
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.5
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.5
813
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
914
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
1015
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16

app/build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
encoding="UTF-8"
4545
includeAntRuntime="false"
4646
debug="true"
47-
classpath="../core/core.jar; ${env.JAVA_HOME}/lib/tools.jar; lib/ant.jar; lib/ant-launcher.jar; lib/apple.jar; lib/ecj.jar; lib/jna.jar; lib/RXTXcomm.jar" />
47+
classpath="../core/core.jar; ${env.JAVA_HOME}/lib/tools.jar; lib/ant.jar; lib/ant-launcher.jar; lib/apple.jar; lib/ecj.jar; lib/jna.jar; lib/RXTXcomm.jar; lib/gson.jar; lib/httpclient.jar; lib/httpcore.jar; lib/commons-logging.jar; lib/commons-codec.jar; lib/egit.jar" />
4848
</target>
4949

5050
<target name="build" depends="compile" description="Build PDE">

app/src/processing/app/Base.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,19 @@ public void actionPerformed(ActionEvent actionevent) {
10141014
}
10151015
}
10161016
}
1017+
// DV - added this to get current board, maybe there's a better way?
1018+
public String getCurrentBoard() {
1019+
//System.out.println("rebuilding boards menu");
1020+
for (Target target : targetsTable.values()) {
1021+
for (String board : target.getBoards().keySet()) {
1022+
if (target.getName().equals(Preferences.get("target")) &&
1023+
board.equals(Preferences.get("board"))) {
1024+
return target.getBoards().get(board).get("name");
1025+
}
1026+
}
1027+
}
1028+
return "";
1029+
}
10171030

10181031

10191032
public void rebuildBurnBootloaderMenu(JMenu menu) {

app/src/processing/app/Editor.java

Lines changed: 205 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@
3434
import java.io.*;
3535
import java.net.*;
3636
import java.util.*;
37+
import java.util.List;
3738
import java.util.zip.*;
3839

3940
import javax.swing.*;
4041
import javax.swing.event.*;
4142
import javax.swing.text.*;
4243
import javax.swing.undo.*;
4344

45+
import org.eclipse.egit.github.core.Gist;
46+
import org.eclipse.egit.github.core.GistFile;
47+
import org.eclipse.egit.github.core.client.GitHubClient;
48+
import org.eclipse.egit.github.core.service.GistService;
49+
4450
import gnu.io.*;
4551

4652
/**
@@ -145,6 +151,8 @@ public class Editor extends JFrame implements RunnerListener {
145151
Runnable stopHandler;
146152
Runnable exportHandler;
147153
Runnable exportAppHandler;
154+
Runnable sendToGitHubHandler;
155+
Runnable retrieveFromGitHubHandler;
148156

149157

150158
public Editor(Base ibase, String path, int[] location) {
@@ -1362,13 +1370,16 @@ protected void updateRedoState() {
13621370

13631371

13641372
public void setHandlers(Runnable runHandler, Runnable presentHandler,
1365-
Runnable stopHandler,
1366-
Runnable exportHandler, Runnable exportAppHandler) {
1373+
Runnable stopHandler, Runnable exportHandler,
1374+
Runnable exportAppHandler, Runnable sendToGitHubHandler,
1375+
Runnable retrieveFromGitHubHandler) {
13671376
this.runHandler = runHandler;
13681377
this.presentHandler = presentHandler;
13691378
this.stopHandler = stopHandler;
13701379
this.exportHandler = exportHandler;
13711380
this.exportAppHandler = exportAppHandler;
1381+
this.sendToGitHubHandler = sendToGitHubHandler;
1382+
this.retrieveFromGitHubHandler = retrieveFromGitHubHandler;
13721383
}
13731384

13741385

@@ -1378,6 +1389,8 @@ public void resetHandlers() {
13781389
stopHandler = new DefaultStopHandler();
13791390
exportHandler = new DefaultExportHandler();
13801391
exportAppHandler = new DefaultExportAppHandler();
1392+
sendToGitHubHandler = new DefaultSendToGitHubHandler();
1393+
retrieveFromGitHubHandler = new DefaultRetrieveFromGitHubHandler();
13811394
}
13821395

13831396

@@ -2275,11 +2288,183 @@ synchronized public void handleExport(final boolean verbose) {
22752288
//if (!handleExportCheckModified()) return;
22762289
toolbar.activate(EditorToolbar.EXPORT);
22772290
console.clear();
2291+
22782292
statusNotice("Uploading to I/O Board...");
2279-
22802293
new Thread(verbose ? exportAppHandler : exportHandler).start();
22812294
}
2295+
2296+
// DV: this is the added code for sending to a github gist
2297+
2298+
synchronized public void handleRetrieve() {
2299+
toolbar.activate(EditorToolbar.RETRIEVE);
2300+
console.clear();
2301+
2302+
statusNotice("Retrieving Source");
2303+
new Thread(retrieveFromGitHubHandler).start();
2304+
}
2305+
2306+
2307+
public String findSerialNumber() {
2308+
if (Base.isMacOS()) {
2309+
String getUsbArgs[] = new String[2];
2310+
getUsbArgs[0]="system_profiler";
2311+
getUsbArgs[1]="SPUSBDataType";
2312+
try{
2313+
Process process = new ProcessBuilder(getUsbArgs).start();
2314+
InputStream is = process.getInputStream();
2315+
InputStreamReader isr = new InputStreamReader(is);
2316+
BufferedReader br = new BufferedReader(isr);
2317+
String line;
2318+
2319+
boolean foundArduino=false;
2320+
boolean foundSerial=false;
2321+
int serialNumPosition;
2322+
while ((line = br.readLine()) != null && !foundSerial) {
2323+
if(line.indexOf("Arduino") > 0 || line.indexOf("FT232R") > 0){
2324+
foundArduino=true;
2325+
}
2326+
if(foundArduino){
2327+
serialNumPosition = line.indexOf("Serial Number");
2328+
if(serialNumPosition > 0){
2329+
foundSerial=true;
2330+
return line.substring((serialNumPosition+15));
2331+
}
2332+
}
2333+
}
2334+
if(foundSerial==false){
2335+
return "";
2336+
}
2337+
}
2338+
catch(IOException e){
2339+
System.out.println(e.getMessage());
2340+
}
2341+
}
2342+
return "";
2343+
}
2344+
2345+
class DefaultSendToGitHubHandler implements Runnable {
2346+
public void run() {
2347+
String serialNumber=findSerialNumber();
2348+
if (!serialNumber.isEmpty()){
2349+
uploadToGitHub(serialNumber);
2350+
statusNotice("Source sent to github");
2351+
}else{
2352+
System.out.println("Could not find your board, make sure it's plugged into USB.");
2353+
statusNotice("");
2354+
}
2355+
}
2356+
2357+
private void uploadToGitHub(String serialNumber) {
2358+
GitHubClient client = new GitHubClient().setCredentials("arduinoboard", "1knowmysource");
2359+
GistService service = new GistService(client);
2360+
GistFile file = new GistFile();
2361+
Gist gist = new Gist();
2362+
2363+
try{
2364+
List<Gist> gists = service.getGists("arduinoboard");
2365+
Boolean foundMatchingGist=false;
2366+
//for (Gist gist : gists) {
2367+
for (int i = gists.size(); --i >= 0;){ //backwards so the first one found is the oldest one
2368+
gist = (Gist)gists.get(i);
2369+
if(gist.getDescription().contains(serialNumber)){ //found the last matching gist
2370+
if(foundMatchingGist==true){ //if one has already been found then an extra was made in error and needs to be cleaned up
2371+
//delete the spurious gist
2372+
service.deleteGist(gist.getId());
2373+
}else{
2374+
//edit the current gist
2375+
file.setContent(sketch.getCurrentCode().getProgram());
2376+
String filename = new String(sketch.getCurrentCode().getPrettyName()+".pde");
2377+
//remove old files
2378+
for (String key : gist.getFiles().keySet()) {
2379+
if(!key.equals(filename)){
2380+
service.updateGist(gist.setFiles(Collections.singletonMap(key, new GistFile())));
2381+
}
2382+
}
2383+
2384+
gist.setFiles(Collections.singletonMap(filename, file));
2385+
service.updateGist(gist);
2386+
System.out.println(new String("You can find the source online at: " + gist.getHtmlUrl()));
2387+
foundMatchingGist=true;
2388+
}
2389+
}
2390+
}
2391+
if(foundMatchingGist==false){ //if no gist exists for the board
2392+
gist = new Gist().setDescription(new String("The file that is currently on an "+base.getCurrentBoard() + " with a serial number of "+serialNumber));
2393+
gist.setPublic(true); //this should be an option in the future, but keep in mind these cannot be edited
2394+
file.setContent(sketch.getCurrentCode().getProgram());
2395+
gist.setFiles(Collections.singletonMap(new String(sketch.getCurrentCode().getPrettyName()+".pde"), file));
2396+
gist = service.createGist(gist);
2397+
System.out.println(new String("You can find the source online at: " + gist.getHtmlUrl()));
2398+
2399+
}
2400+
}catch(IOException e){
2401+
System.out.println(e.getMessage());
2402+
}
2403+
}
2404+
}
2405+
2406+
class DefaultRetrieveFromGitHubHandler implements Runnable {
2407+
public void run() {
2408+
String serialNumber;
2409+
try {
2410+
int timeout = 2000;
2411+
InetAddress address = InetAddress.getByName("api.github.com");
2412+
if (address.isReachable(timeout)){
2413+
serialNumber=findSerialNumber();
2414+
if (!serialNumber.isEmpty()){
2415+
retrieveFromGitHub(serialNumber);
2416+
}else{
2417+
System.out.println("Could not find your board, make sure it's plugged into USB.");
2418+
statusNotice("");
2419+
}
2420+
}else{
2421+
System.out.println("github service is unavailable, cannot retrieve source.");
2422+
statusNotice("");
2423+
}
2424+
} catch (Exception e) {
2425+
System.out.println("You are not connected to the internet, cannot retrieve source.");
2426+
statusNotice("");
2427+
}
2428+
toolbar.deactivate(EditorToolbar.RETRIEVE);
2429+
}
2430+
2431+
private void retrieveFromGitHub(String serialNumber) {
2432+
GitHubClient client = new GitHubClient().setCredentials("arduinoboard", "1knowmysource");
2433+
GistService service = new GistService(client);
2434+
GistFile file = new GistFile();
2435+
Gist gist = new Gist();
2436+
2437+
try{
2438+
List<Gist> gists = service.getGists("arduinoboard");
2439+
Boolean foundMatchingGist=false;
2440+
//for (Gist gist : gists) {
2441+
for (int i = gists.size(); --i >= 0;){ //backwards so the first one found is the oldest one
2442+
gist = (Gist)gists.get(i);
2443+
if(gist.getDescription().contains(serialNumber)){ //found the last matching gist
2444+
if(foundMatchingGist==true){ //if one has already been found then an extra was made in error and needs to be cleaned up
2445+
//delete the spurious gist
2446+
service.deleteGist(gist.getId());
2447+
}else{
2448+
statusNotice("Found Source");
2449+
gist=service.getGist(gist.getId());//get it again because the other capture only gets the meta-data
2450+
setText(gist.getFiles().get(gist.getFiles().keySet().iterator().next()).getContent()); //gets the first sketch, puts it in the window
2451+
foundMatchingGist=true;
2452+
}
2453+
}
2454+
}
2455+
if(foundMatchingGist==false){ //if no gist exists for the board
2456+
System.out.println("No source was found for this board.");
2457+
statusNotice("");
2458+
}
2459+
}catch(IOException e){
2460+
System.out.println(e.getMessage());
2461+
statusNotice("");
2462+
}
2463+
}
2464+
}
22822465

2466+
2467+
22832468
// DAM: in Arduino, this is upload
22842469
class DefaultExportHandler implements Runnable {
22852470
public void run() {
@@ -2292,7 +2477,23 @@ public void run() {
22922477

22932478
boolean success = sketch.exportApplet(false);
22942479
if (success) {
2295-
statusNotice("Done uploading.");
2480+
//this is where the github code starts
2481+
try {
2482+
int timeout = 2000;
2483+
InetAddress address = InetAddress.getByName("api.github.com");
2484+
if (address.isReachable(timeout)){
2485+
statusNotice("Done uploading, sending source to github...");
2486+
new Thread(sendToGitHubHandler).start();
2487+
}else{
2488+
statusNotice("Done uploading.");
2489+
System.out.println("github service is unavailable, source will not be sent.");
2490+
System.out.println("Make sure to save locally!");
2491+
}
2492+
} catch (Exception e) {
2493+
statusNotice("Done uploading.");
2494+
System.out.println("You are not connected to the internet, source will not be sent.");
2495+
System.out.println("Make sure to save locally!");
2496+
}
22962497
} else {
22972498
// error message will already be visible
22982499
}

app/src/processing/app/EditorToolbar.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public class EditorToolbar extends JComponent implements MouseInputListener, Key
3737

3838
/** Rollover titles for each button. */
3939
static final String title[] = {
40-
"Verify", "Stop", "New", "Open", "Save", "Upload", "Serial Monitor"
40+
"Verify", "Stop", "New", "Open", "Save", "Retrieve Source", "Upload", "Serial Monitor"
4141
};
4242

4343
/** Titles for each button when the shift key is pressed. */
4444
static final String titleShift[] = {
45-
"Verify (w/ Verbose Output)", "Stop", "New Editor Window", "Open in Another Window", "Save", "Upload (w/ Verbose Output)", "Serial Monitor"
45+
"Verify (w/ Verbose Output)", "Stop", "New Editor Window", "Open in Another Window", "Save", "Retrieve Source", "Upload (w/ Verbose Output)", "Serial Monitor"
4646
};
4747

4848
static final int BUTTON_COUNT = title.length;
@@ -62,9 +62,10 @@ public class EditorToolbar extends JComponent implements MouseInputListener, Key
6262
static final int NEW = 2;
6363
static final int OPEN = 3;
6464
static final int SAVE = 4;
65-
static final int EXPORT = 5;
65+
static final int RETRIEVE = 5;
66+
static final int EXPORT = 6;
6667

67-
static final int SERIAL = 6;
68+
static final int SERIAL = 7;
6869

6970
static final int INACTIVE = 0;
7071
static final int ROLLOVER = 1;
@@ -109,6 +110,7 @@ public EditorToolbar(Editor editor, JMenu menu) {
109110
which[buttonCount++] = NEW;
110111
which[buttonCount++] = OPEN;
111112
which[buttonCount++] = SAVE;
113+
which[buttonCount++] = RETRIEVE;
112114
which[buttonCount++] = EXPORT;
113115
which[buttonCount++] = SERIAL;
114116

@@ -168,7 +170,7 @@ public void paintComponent(Graphics screen) {
168170
int offsetX = 3;
169171
for (int i = 0; i < buttonCount; i++) {
170172
x1[i] = offsetX;
171-
if (i == 2 || i == 6) x1[i] += BUTTON_GAP;
173+
if (i == 2 || i == 7) x1[i] += BUTTON_GAP;
172174
x2[i] = x1[i] + BUTTON_WIDTH;
173175
offsetX = x2[i];
174176
}
@@ -335,6 +337,10 @@ public void mousePressed(MouseEvent e) {
335337
case SAVE:
336338
editor.handleSave(false);
337339
break;
340+
341+
case RETRIEVE:
342+
editor.handleRetrieve();
343+
break;
338344

339345
case EXPORT:
340346
editor.handleExport(e.isShiftDown());

0 commit comments

Comments
 (0)