BowlerKernel
NRBoot.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.bootloader;
2 
3 
4 import java.io.IOException;
5 import java.util.ArrayList;
6 
7 import javax.swing.JOptionPane;
8 
9 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
10 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
11 import com.neuronrobotics.sdk.serial.SerialConnection;
12 
13 // TODO: Auto-generated Javadoc
17 public class NRBoot {
18 
20  private NRBootLoader boot;
21 
23  private CoreLoader loader;
24 
26  private int progressMax=0;
27 
29  private int progressValue=0;
30 
37  try {
38  boot=(NRBootLoader)pm;
39  }catch(RuntimeException e) {
40  //e.printStackTrace();
41  String message = "Not a bootloader device";
42  //JOptionPane.showMessageDialog(null, message, message, JOptionPane.ERROR_MESSAGE);
43  throw e;
44  }
45  //System.out.println("Connection to bowler device ready");
46  }
47 
53  public NRBoot(String serialPort){
54  this.boot=new NRBootLoader(new SerialConnection(serialPort));
55  boot.connect();
56  if (boot.ping()){
57  //System.out.println("Connection to bowler device ready");
58  return;
59  }
60  //System.out.println("Not a Bowler Device");
61  boot.disconnect();
62  boot=null;
63  }
64 
71  public boolean load(Core core) {
72 
73  String id = getDevice().getBootloaderID();
74  if (id==null){
75  System.err.println("Device is not a bootloader");
76  return false;
77  }else if (id.contains(core.getType().getReadableName())) {
78  //System.out.println("Bootloader ID:"+core.getType().getReadableName());
79  }else{
80  System.err.println("##core is Invalid##\nExpected:"+core.getType().getReadableName()+" got: "+id);
81  return false;
82  }
83 
84  IntelHexParser parse = getParser(core);
85  if(parse==null)
86  return false;
87  send(parse,core.getIndex());
88  return true;
89  }
90 
97  private IntelHexParser getParser(Core core) {
98  try {
99  return new IntelHexParser(core.getLines(),core.getType());
100  } catch (IOException e) {
101  return null;
102  }
103  }
104 
111  private void send(IntelHexParser parse,int core){
112  boot.erase(core);
113  //System.out.println("Writing to flash");
114  int printLine=0;
115  ByteData line = parse.getNext();
116  while (line != null){
117 
118  if(!boot.write(core, line)){
119  //System.out.println("Failed to write, is the device in bootloader mode?");
120  return;
121  }
122 
123  line = parse.getNext();
124  //System.out.print(".");
125  progressValue++;
126  printLine++;
127  if (printLine>100){
128  printLine=0;
129  //System.out.print("\n");
130  }
131  }
132  //System.out.print("\n");
133  }
134 
138  public void reset(){
139  try{
140  boot.reset();
141  }catch(Exception e){
142  boot.disconnect();
143  }
144  }
145 
152  return boot;
153  }
154 
160  public void loadCores(ArrayList<Core> cores) {
161  loader = new CoreLoader(cores);
162  loader.start();
163  }
164 
170  public boolean isLoadDone() {
171  return loader.isDone;
172  }
173 
177  private class CoreLoader extends Thread{
178 
180  ArrayList<Core> cores;
181 
183  public boolean isDone=false;
184 
186  public int val=0;
187 
193  public CoreLoader(ArrayList<Core> cores){
194  this.cores=cores;
195  progressMax=0;
196  for (Core b:cores){
197  progressMax += getParser(b).size();
198  }
199  progressValue=0;
200  }
201 
202  /* (non-Javadoc)
203  * @see java.lang.Thread#run()
204  */
205  public void run() {
206  try {
207  for (Core b:cores){
208  val++;
209  load(b);
210  }
211  reset();
212  }catch(Exception e) {
213  e.printStackTrace();
214  String message = "This device is not a bootloader";
215  JOptionPane.showMessageDialog(null, message, message, JOptionPane.ERROR_MESSAGE);
216  }
217  progressValue=0;
218  isDone=true;
219  }
220  }
221 
227  public int getProgressMax() {
228  // TODO Auto-generated method stub
229  return progressMax;
230  }
231 
237  public int getProgressValue() {
238  return progressValue;
239  }
240 
241 }
ArrayList< hexLine > getLines()
Definition: Core.java:119
boolean write(int core, ByteData flashData)
NRBoot(BowlerAbstractDevice pm)
Definition: NRBoot.java:36
void loadCores(ArrayList< Core > cores)
Definition: NRBoot.java:160
void send(IntelHexParser parse, int core)
Definition: NRBoot.java:111
IntelHexParser getParser(Core core)
Definition: NRBoot.java:97