Rev 181 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 181 | Rev 208 | ||
---|---|---|---|
Line 35... | Line 35... | ||
35 | 35 | ||
36 | // version Info from Flight Control |
36 | // version Info from Flight Control |
37 | public int version_major=-1; |
37 | public int version_major=-1; |
38 | public int version_minor=-1; |
38 | public int version_minor=-1; |
39 | public int version_compatible=-1; |
39 | public int version_compatible=-1; |
- | 40 | ||
Line 40... | Line 41... | ||
40 | 41 | public MKLCD LCD; |
|
Line 41... | Line 42... | ||
41 | 42 | ||
Line 50... | Line 51... | ||
50 | 51 | ||
51 | // temp - to be removed |
52 | // temp - to be removed |
52 | String p_msg="--"; |
53 | String p_msg="--"; |
Line -... | Line 54... | ||
- | 54 | public String msg="BT_INIT"; |
|
- | 55 | ||
- | 56 | public int debug_data_count=0; |
|
53 | public String msg="BT_INIT"; |
57 | public int version_data_count=0; |
Line 54... | Line 58... | ||
54 | 58 | public int other_data_count=0; |
|
55 | public int data_count=0; |
59 | public int lcd_data_count=0; |
56 | 60 | ||
- | 61 | ||
57 | 62 | /****************** Section: public Methods ************************************************/ |
|
58 | /****************** Section: public Methods ************************************************/ |
63 | public MKCommunicator(String url) // Constructor with URL string e.g. "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control |
Line 59... | Line 64... | ||
59 | public MKCommunicator(String url) // Constructor with URL string e.g. "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control |
64 | { |
60 | { |
65 | |
Line 74... | Line 79... | ||
74 | writer=connection.openOutputStream(); |
79 | writer=connection.openOutputStream(); |
Line 75... | Line 80... | ||
75 | 80 | ||
76 | connection_start_time=System.currentTimeMillis(); |
81 | connection_start_time=System.currentTimeMillis(); |
77 | connected=true; // if we get here everything seems to be OK |
82 | connected=true; // if we get here everything seems to be OK |
- | 83 | get_version(); |
|
78 | get_version(); |
84 | LCD= new MKLCD(this); |
79 | } |
85 | } |
80 | catch (Exception ex) |
86 | catch (Exception ex) |
81 | { |
87 | { |
82 | // TODO difference fatal errors from those which will lead to reconnection |
88 | // TODO difference fatal errors from those which will lead to reconnection |
Line 123... | Line 129... | ||
123 | public void get_version() |
129 | public void get_version() |
124 | { |
130 | { |
125 | send_command(0,'v',new int[0]); |
131 | send_command(0,'v',new int[0]); |
126 | } |
132 | } |
Line -... | Line 133... | ||
- | 133 | ||
127 | 134 | ||
128 | // send command to FC ( add crc and pack into pseudo Base64 |
135 | // send command to FC ( add crc and pack into pseudo Base64 |
129 | public void send_command(int modul,char cmd,int[] params) |
136 | public void send_command(int modul,char cmd,int[] params) |
130 | { |
137 | { |
131 | char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
138 | char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
132 | send_buff[0]='#'; |
139 | send_buff[0]='#'; |
133 | send_buff[1]=(char)modul; |
140 | send_buff[1]=(char)modul; |
Line -... | Line 141... | ||
- | 141 | send_buff[2]=cmd; |
|
- | 142 | ||
- | 143 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
|
- | 144 | { |
|
- | 145 | int a = (param_pos<params.length)?params[param_pos]:0; |
|
- | 146 | int b = ((param_pos+1)<params.length)?params[param_pos+1]:0; |
|
- | 147 | int c = ((param_pos+2)<params.length)?params[param_pos+2]:0; |
|
- | 148 | ||
- | 149 | send_buff[3+param_pos*4] = (char)((a >> 2)+'=' ); |
|
- | 150 | send_buff[3+param_pos*4+1] = (char)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
|
- | 151 | send_buff[3+param_pos*4+2] = (char)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
|
- | 152 | send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f)); |
|
- | 153 | ||
- | 154 | //send_buff[3+foo]='='; |
|
- | 155 | } |
|
- | 156 | ||
- | 157 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
|
- | 158 | { |
|
- | 159 | int a = (foo<params.length) params[foo]; |
|
- | 160 | int a = params[foo]; |
|
- | 161 | |
|
- | 162 | //send_buff[3+foo]='='; |
|
134 | send_buff[2]=cmd; |
163 | } |
135 | 164 | */ |
|
136 | try |
165 | try |
137 | { |
166 | { |
138 | int tmp_crc=0; |
167 | int tmp_crc=0; |
Line 163... | Line 192... | ||
163 | { |
192 | { |
Line 164... | Line 193... | ||
164 | 193 | ||
165 | case 'D': // debug Data |
194 | case 'D': // debug Data |
166 | decoded_data=Decode64(data,3,50); |
195 | decoded_data=Decode64(data,3,50); |
167 | debug_data=new MKDebugData(decoded_data); |
- | |
168 | msg="Got Debug Data s:" + debug_data.sekunden + "zy:"+debug_data.zyklen + "ze:" + debug_data.zeit + connected + ">" + data_count; |
- | |
169 | for (int ff=0;ff<8;ff++) |
- | |
- | 196 | debug_data=new MKDebugData(decoded_data); |
|
Line -... | Line 197... | ||
- | 197 | ||
170 | msg+="\n d:" + (ff*2+1) + " " + debug_data.analog[ff*2] + " d2:" + (ff*2+2) + " " + debug_data.analog[ff*2+1] ; |
198 | |
Line 171... | Line 199... | ||
171 | 199 | debug_data_count++; |
|
172 | break; |
200 | break; |
173 | 201 | ||
174 | case 'V': // Version Info |
202 | case 'V': // Version Info |
175 | decoded_data=Decode64(data,3,6); |
203 | decoded_data=Decode64(data,3,6); |
- | 204 | version_major=decoded_data[0]; |
|
176 | version_major=decoded_data[0]; |
205 | version_minor=decoded_data[1]; |
177 | version_minor=decoded_data[1]; |
206 | version_compatible=decoded_data[2]; |
Line -... | Line 207... | ||
- | 207 | version_data_count++; |
|
- | 208 | ||
- | 209 | break; |
|
- | 210 | ||
- | 211 | case '0': |
|
- | 212 | case '1': |
|
- | 213 | case '2': |
|
- | 214 | case '3': |
|
- | 215 | LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0'); |
|
- | 216 | lcd_data_count++; |
|
- | 217 | // decoded_data= |
|
- | 218 | ||
- | 219 | ||
178 | version_compatible=decoded_data[2]; |
220 | // if ((data[2]-(int)'0')!=3) |
- | 221 | // get_LCD(); |
|
179 | 222 | break; |
|
Line 180... | Line 223... | ||
180 | break; |
223 | |
Line 181... | Line -... | ||
181 | - | ||
182 | default: |
224 | default: |
183 | break; |
- | |
Line 184... | Line 225... | ||
184 | 225 | other_data_count++; |
|
Line -... | Line 226... | ||
- | 226 | break; |
|
Line -... | Line 227... | ||
- | 227 | ||
Line -... | Line 228... | ||
- | 228 | } |
|
- | 229 | ||
- | 230 | ||
- | 231 | ||
- | 232 | ||
- | 233 | } |
|
- | 234 | ||
- | 235 | String o_msg=""; |
|
- | 236 | ||
- | 237 | public boolean force_disconnect=false; |
|
- | 238 | ||
- | 239 | public void close_connections() |
|
- | 240 | { |
|
- | 241 | force_disconnect=true; |
|
Line 185... | Line 242... | ||
185 | } |
242 | try{ reader.close(); } |
186 | 243 | catch (Exception inner_ex) { } |
|
187 | 244 | ||
188 | msg+=p_msg; |
245 | try{ writer.close(); } |
Line 203... | Line 260... | ||
203 | msg+="!!run started!!"; |
260 | msg+="!!run started!!"; |
204 | while(true) |
261 | while(true) |
205 | { |
262 | { |
206 | if (!connected) |
263 | if (!connected) |
207 | { |
264 | { |
208 | connect(); |
265 | if (!force_disconnect) connect(); |
209 | } |
266 | } |
210 | else |
267 | else |
211 | try{ |
268 | try{ |
Line 212... | Line 269... | ||
212 | 269 | ||
Line 226... | Line 283... | ||
226 | } |
283 | } |
227 | catch (Exception ex) |
284 | catch (Exception ex) |
228 | { |
285 | { |
229 | msg="Problem reading from MK"; |
286 | msg="Problem reading from MK"; |
230 | // close the connection |
287 | // close the connection |
231 | try{ reader.close(); connection.close(); } |
- | |
232 | catch (Exception inner_ex) { } |
- | |
233 | connected=false; |
288 | close_connections(); |
- | 289 | ||
- | 290 | ||
234 | } |
291 | } |
Line 235... | Line 292... | ||
235 | 292 | ||
236 | // sleep a bit to get someting more done |
293 | // sleep a bit to get someting more done |
237 | try { Thread.sleep(50); } |
294 | try { Thread.sleep(50); } |