BowlerKernel
ArduinoLoader.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.scripting;
2 
3 import java.io.File;
4 import java.io.InputStreamReader;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 
9 public class ArduinoLoader implements IScriptingLanguage {
10 
11  private static String ARDUINO = "arduino";
12 
13  HashMap<String, HashMap<String, Object>> database;
14 
15  private static String defaultPort = null;
16  private static String defaultBoard = null;
17  private static boolean loadedBowler = false;
18 
19  @SuppressWarnings("unchecked")
20  @Override
21  public Object inlineScriptRun(File code, ArrayList<Object> args) throws Exception {
22  if (args == null) {
23  args = new ArrayList<>();
24  }
25  if (database == null) {
26  database = (HashMap<String, HashMap<String, Object>>) ScriptingEngine
27  .gitScriptRun("https://github.com/madhephaestus/Arduino-Boards-JSON.git",
28  "boards.json", null);
29  }
30  String execString = getARDUINOExec();
31 
32  if (args.size() > 0) {
33  setDefaultBoard((String) args.get(0));
34  }
35  if (getDefaultBoard() != null) {
36  execString += " --board " + getDefaultBoard();
37  if (args.size() > 1) {
38  setDefaultPort((String) args.get(1));
39  }
40  }
41  if (getDefaultPort() != null) {
42  execString += " --port " + getDefaultPort();
43  }
44  HashMap<String, Object> configs = database.get(getDefaultBoard());
45  File ino = findIno(code);
46  if (ino == null) {
47  //System.out.println("Error: no .ino file found!");
48  return null;
49  }
50  execString += " --upload " + ino.getAbsolutePath().replaceAll(" ", "\\ ");
51  ;
52 
53  //System.out.println("Arduino Load: \n"+execString);
54  if (!loadedBowler) {
55  loadedBowler = true;
56  run(getARDUINOExec() + " --install-library BowlerCom");
57  }
58  run(execString);
59 
60  return null;
61  }
62 
63  public static void installBoard(String product, String arch) throws Exception {
64  run(getARDUINOExec() + " --install-boards " + product + ":" + arch);
65  }
66 
67  public static void installLibrary(String lib) throws Exception {
68  run(getARDUINOExec() + " --install-library " + lib);
69  }
70 
71  public static void run(String execString) throws Exception {
72  System.out.println("Running:\n" + execString);
73  // Get runtime
74  java.lang.Runtime rt = java.lang.Runtime.getRuntime();
75  // Start a new process
76  java.lang.Process p = rt.exec(execString);
77  // You can or maybe should wait for the process to complete
78  p.waitFor();
79  // Get process' output: its InputStream
80  java.io.InputStream is = p.getInputStream();
81  java.io.InputStream err = p.getInputStream();
82  java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
83  java.io.BufferedReader readerErr = new java.io.BufferedReader(new InputStreamReader(err));
84 
85  // And print each line
86  String s = null;
87  while ((s = reader.readLine()) != null) {
88  System.out.println(s);// This is how the scripts output to the print stream
89  }
90 
91  s = null;
92  while ((s = readerErr.readLine()) != null) {
93  System.out.println(s);// This is how the scripts output to the print stream
94  }
95  is.close();
96  err.close();
97  }
98 
99  private File findIno(File start) {
100  if (start == null) {
101  return null;
102  }
103  if (start.getName().endsWith(".ino")) {
104  return start;
105  } else {
106  File dir = start.getParentFile();
107  if (dir != null) {
108  for (File f : dir.listFiles()) {
109  if (findIno(f) != null) {
110  return f;
111  }
112  }
113  }
114  }
115  return null;
116 
117  }
118 
119  @Override
120  public Object inlineScriptRun(String code, ArrayList<Object> args) throws Exception {
121  // TODO Auto-generated method stub
122  return null;
123  }
124 
125  @Override
126  public String getShellType() {
127  return "Arduino";
128  }
129 
130  @Override
131  public boolean getIsTextFile() {
132  return true;
133  }
134 
135  public static String getDefaultPort() {
136  return defaultPort;
137  }
138 
139  public static void setDefaultPort(String defaultPort) {
141  }
142 
143  public static String getDefaultBoard() {
144  return defaultBoard;
145  }
146 
147  public static void setDefaultBoard(String defaultBoard) {
149  }
154  public String getDefaultContents() {
155  return "/*\n"
156  + " Blink\n"
157  + "\n"
158  + " Turns an LED on for one second, then off for one second, repeatedly.\n"
159  + "\n"
160  + " Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO\n"
161  + " it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to\n"
162  + " the correct LED pin independent of which board is used.\n"
163  + " If you want to know what pin the on-board LED is connected to on your Arduino\n"
164  + " model, check the Technical Specs of your board at:\n"
165  + " https://www.arduino.cc/en/Main/Products\n"
166  + "\n"
167  + " modified 8 May 2014\n"
168  + " by Scott Fitzgerald\n"
169  + " modified 2 Sep 2016\n"
170  + " by Arturo Guadalupi\n"
171  + " modified 8 Sep 2016\n"
172  + " by Colby Newman\n"
173  + "\n"
174  + " This example code is in the public domain.\n"
175  + "\n"
176  + " https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink\n"
177  + "*/\n"
178  + "\n"
179  + "// the setup function runs once when you press reset or power the board\n"
180  + "void setup() {\n"
181  + " // initialize digital pin LED_BUILTIN as an output.\n"
182  + " pinMode(LED_BUILTIN, OUTPUT);\n"
183  + "}\n"
184  + "\n"
185  + "// the loop function runs over and over again forever\n"
186  + "void loop() {\n"
187  + " digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)\n"
188  + " delay(1000); // wait for a second\n"
189  + " digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW\n"
190  + " delay(1000); // wait for a second\n"
191  + "}\n"
192  + "";
193  }
194 
195  public static String getARDUINOExec() {
196  return ARDUINO;
197  }
198 
199  public static void setARDUINOExec(String aRDUINO) {
200  ARDUINO = aRDUINO;
201  }
202 
203  @Override
204  public ArrayList<String> getFileExtenetion() {
205  // TODO Auto-generated method stub
206  return new ArrayList<>(Arrays.asList( ".ino",".c", ".h", ".cpp", ".hpp"));
207  }
208 
209 }
static void installBoard(String product, String arch)
Object inlineScriptRun(File code, ArrayList< Object > args)
Object inlineScriptRun(String code, ArrayList< Object > args)