Skip to content

Commit ec8b027

Browse files
author
Arduino Board
committed
alpha build
1 parent 27df5d4 commit ec8b027

File tree

7 files changed

+326
-119
lines changed

7 files changed

+326
-119
lines changed

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: 204 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,182 @@ 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+
foundMatchingGist=true;
2387+
}
2388+
}
2389+
}
2390+
if(foundMatchingGist==false){ //if no gist exists for the board
2391+
gist = new Gist().setDescription(new String("The file that is currently on an "+base.getCurrentBoard() + " with a serial number of "+serialNumber));
2392+
gist.setPublic(true); //this should be an option in the future, but keep in mind these cannot be edited
2393+
file.setContent(sketch.getCurrentCode().getProgram());
2394+
gist.setFiles(Collections.singletonMap(new String(sketch.getCurrentCode().getPrettyName()+".pde"), file));
2395+
gist = service.createGist(gist);
2396+
2397+
}
2398+
System.out.println(new String("You can find the source online at: " + gist.getHtmlUrl()));
2399+
}catch(IOException e){
2400+
System.out.println(e.getMessage());
2401+
}
2402+
}
2403+
}
2404+
2405+
class DefaultRetrieveFromGitHubHandler implements Runnable {
2406+
public void run() {
2407+
String serialNumber;
2408+
try {
2409+
int timeout = 2000;
2410+
InetAddress address = InetAddress.getByName("api.github.com");
2411+
if (address.isReachable(timeout)){
2412+
serialNumber=findSerialNumber();
2413+
if (!serialNumber.isEmpty()){
2414+
retrieveFromGitHub(serialNumber);
2415+
}else{
2416+
System.out.println("Could not find your board, make sure it's plugged into USB.");
2417+
statusNotice("");
2418+
}
2419+
}else{
2420+
System.out.println("github service is unavailable, cannot retrieve source.");
2421+
statusNotice("");
2422+
}
2423+
} catch (Exception e) {
2424+
System.out.println("You are not connected to the internet, cannot retrieve source.");
2425+
statusNotice("");
2426+
}
2427+
toolbar.deactivate(EditorToolbar.RETRIEVE);
2428+
}
2429+
2430+
private void retrieveFromGitHub(String serialNumber) {
2431+
GitHubClient client = new GitHubClient().setCredentials("arduinoboard", "1knowmysource");
2432+
GistService service = new GistService(client);
2433+
GistFile file = new GistFile();
2434+
Gist gist = new Gist();
2435+
2436+
try{
2437+
List<Gist> gists = service.getGists("arduinoboard");
2438+
Boolean foundMatchingGist=false;
2439+
//for (Gist gist : gists) {
2440+
for (int i = gists.size(); --i >= 0;){ //backwards so the first one found is the oldest one
2441+
gist = (Gist)gists.get(i);
2442+
if(gist.getDescription().contains(serialNumber)){ //found the last matching gist
2443+
if(foundMatchingGist==true){ //if one has already been found then an extra was made in error and needs to be cleaned up
2444+
//delete the spurious gist
2445+
service.deleteGist(gist.getId());
2446+
}else{
2447+
statusNotice("Found Source");
2448+
gist=service.getGist(gist.getId());//get it again because the other capture only gets the meta-data
2449+
setText(gist.getFiles().get(gist.getFiles().keySet().iterator().next()).getContent()); //gets the first sketch, puts it in the window
2450+
foundMatchingGist=true;
2451+
}
2452+
}
2453+
}
2454+
if(foundMatchingGist==false){ //if no gist exists for the board
2455+
System.out.println("No source was found for this board.");
2456+
statusNotice("");
2457+
}
2458+
}catch(IOException e){
2459+
System.out.println(e.getMessage());
2460+
statusNotice("");
2461+
}
2462+
}
2463+
}
22822464

2465+
2466+
22832467
// DAM: in Arduino, this is upload
22842468
class DefaultExportHandler implements Runnable {
22852469
public void run() {
@@ -2292,7 +2476,23 @@ public void run() {
22922476

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

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());

build/build.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<include name="app/lib/RXTXcomm.jar" />
2929
<include name="app/lib/ant.jar" />
3030
<include name="app/lib/ant-launcher.jar" />
31+
<include name="app/lib/gson.jar" />
32+
<include name="app/lib/httpclient.jar" />
33+
<include name="app/lib/httpcore.jar" />
34+
<include name="app/lib/commons-codec.jar" />
35+
<include name="app/lib/commons-logging.jar" />
36+
<include name="app/lib/egit.jar" />
3137
</fileset>
3238

3339
<target name="build" description="Build Arduino.">

0 commit comments

Comments
 (0)