Rev 210 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
164 | ligi | 1 | /********************************************************************************************************************************* |
2 | * * |
||
3 | * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de ) * |
||
4 | * * |
||
5 | * Author: Marcus -LiGi- Bueschleb * |
||
6 | * Project-Start: 9/2007 * |
||
7 | * Version: 0.07 * |
||
8 | * Mailto: ligi@smart4mobile.de * |
||
9 | * Licence: Creative Commons / Non Commercial * |
||
10 | * Big Up: Holger&Ingo * |
||
11 | * ChangeLog: * |
||
12 | * 0.01 - initial Version ( initialize connection / main Thread with reading data from MK) * |
||
13 | * 0.02 - reconnect after connection loss ( e.g. switching on/off ) * |
||
14 | * 0.03 - added send_command ( with CRC ) * |
||
15 | * 0.04 - added decode64 to decode 'pseudo' BASE64 * |
||
16 | * 0.05 - added get_version * |
||
17 | * 0.06 - added parsing of DebugData * |
||
18 | * 0.07 - Code-(Doc&&Cleanup) && initial svn commit * |
||
19 | * * |
||
20 | *********************************************************************************************************************************/ |
||
21 | |||
22 | import javax.microedition.io.*; |
||
23 | import java.io.*; |
||
24 | |||
25 | public class MKCommunicator |
||
26 | implements Runnable |
||
27 | { |
||
28 | /***************** Section: public Attributes **********************************************/ |
||
29 | public boolean connected=false; // flag for the connection state |
||
30 | public boolean fatal=false; // flag which is set when an error is so fatal that reconnecting won't be tried - e.g. unknown version number. |
||
31 | |||
32 | |||
33 | public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes |
||
34 | |||
208 | ligi | 35 | public MKLCD LCD; |
210 | ligi | 36 | public MKVersion version; |
37 | public MKDebugData debug_data; |
||
221 | ligi | 38 | public MKWatchDog watchdog; |
208 | ligi | 39 | |
181 | ligi | 40 | public long connection_start_time=-1; |
164 | ligi | 41 | |
42 | |||
43 | /****************** Section: private Attributes **********************************************/ |
||
44 | private javax.microedition.io.StreamConnection connection; |
||
45 | private java.io.InputStream reader; |
||
46 | private java.io.OutputStream writer; |
||
47 | |||
48 | |||
49 | // temp - to be removed |
||
50 | String p_msg="--"; |
||
51 | public String msg="BT_INIT"; |
||
52 | |||
208 | ligi | 53 | public int debug_data_count=0; |
54 | public int version_data_count=0; |
||
55 | public int other_data_count=0; |
||
56 | public int lcd_data_count=0; |
||
164 | ligi | 57 | |
58 | |||
59 | /****************** Section: public Methods ************************************************/ |
||
60 | 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 |
||
61 | { |
||
210 | ligi | 62 | version=new MKVersion(); |
181 | ligi | 63 | debug_data=new MKDebugData(); |
164 | ligi | 64 | mk_url=url; // remember URL for connecting / reconnecting later |
181 | ligi | 65 | |
164 | ligi | 66 | new Thread( this ).start(); // fire up main Thread |
67 | } |
||
68 | |||
69 | |||
70 | /****************** Section: private Methods ************************************************/ |
||
71 | private void connect() |
||
72 | { |
||
73 | try{ |
||
74 | connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE); |
||
75 | reader=connection.openInputStream(); |
||
76 | writer=connection.openOutputStream(); |
||
181 | ligi | 77 | |
78 | connection_start_time=System.currentTimeMillis(); |
||
164 | ligi | 79 | connected=true; // if we get here everything seems to be OK |
181 | ligi | 80 | get_version(); |
221 | ligi | 81 | lcd_data_count=0; |
82 | debug_data_count=0; |
||
83 | version_data_count=0; |
||
84 | |||
208 | ligi | 85 | LCD= new MKLCD(this); |
221 | ligi | 86 | watchdog=new MKWatchDog(this); |
164 | ligi | 87 | } |
88 | catch (Exception ex) |
||
89 | { |
||
90 | // TODO difference fatal errors from those which will lead to reconnection |
||
91 | msg="Problem connecting" + "\n" + ex; |
||
92 | } |
||
181 | ligi | 93 | |
94 | |||
95 | |||
164 | ligi | 96 | } |
97 | |||
98 | |||
99 | |||
100 | public int[] Decode64(int[] in_arr, int offset,int len) |
||
101 | { |
||
102 | int ptrIn=offset; |
||
103 | int a,b,c,d,x,y,z; |
||
104 | int ptr=0; |
||
105 | |||
106 | int[] out_arr=new int[len]; |
||
107 | |||
108 | while(len!=0) |
||
109 | { |
||
110 | a = in_arr[ptrIn++] - '='; |
||
111 | b = in_arr[ptrIn++] - '='; |
||
112 | c = in_arr[ptrIn++] - '='; |
||
113 | d = in_arr[ptrIn++] - '='; |
||
114 | //if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
115 | |||
116 | x = (a << 2) | (b >> 4); |
||
117 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
118 | z = ((c & 0x03) << 6) | d; |
||
119 | |||
120 | if((len--)!=0) out_arr[ptr++] = x; else break; |
||
121 | if((len--)!=0) out_arr[ptr++] = y; else break; |
||
122 | if((len--)!=0) out_arr[ptr++] = z; else break; |
||
123 | } |
||
124 | |||
125 | return out_arr; |
||
126 | |||
127 | } |
||
128 | |||
129 | |||
130 | // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives |
||
131 | public void get_version() |
||
132 | { |
||
133 | send_command(0,'v',new int[0]); |
||
134 | } |
||
135 | |||
208 | ligi | 136 | |
164 | ligi | 137 | // send command to FC ( add crc and pack into pseudo Base64 |
138 | public void send_command(int modul,char cmd,int[] params) |
||
139 | { |
||
140 | 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 |
||
141 | send_buff[0]='#'; |
||
142 | send_buff[1]=(char)modul; |
||
143 | send_buff[2]=cmd; |
||
144 | |||
208 | ligi | 145 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
146 | { |
||
147 | int a = (param_pos<params.length)?params[param_pos]:0; |
||
148 | int b = ((param_pos+1)<params.length)?params[param_pos+1]:0; |
||
149 | int c = ((param_pos+2)<params.length)?params[param_pos+2]:0; |
||
150 | |||
151 | send_buff[3+param_pos*4] = (char)((a >> 2)+'=' ); |
||
152 | send_buff[3+param_pos*4+1] = (char)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
||
153 | send_buff[3+param_pos*4+2] = (char)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
||
154 | send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f)); |
||
155 | |||
156 | //send_buff[3+foo]='='; |
||
157 | } |
||
158 | |||
159 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
||
160 | { |
||
161 | int a = (foo<params.length) params[foo]; |
||
162 | int a = params[foo]; |
||
163 | |||
164 | //send_buff[3+foo]='='; |
||
165 | } |
||
166 | */ |
||
164 | ligi | 167 | try |
168 | { |
||
169 | int tmp_crc=0; |
||
170 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
||
171 | { |
||
172 | tmp_crc+=(int)send_buff[tmp_i]; |
||
173 | writer.write(send_buff[tmp_i]); |
||
174 | } |
||
175 | tmp_crc%=4096; |
||
176 | writer.write( (char)(tmp_crc/64 + '=')); |
||
177 | writer.write( (char)(tmp_crc%64 + '=')); |
||
178 | writer.write('\r'); |
||
179 | writer.flush(); |
||
180 | } |
||
181 | catch (Exception e) |
||
182 | { // problem sending data to FC |
||
183 | } |
||
184 | |||
185 | } |
||
186 | |||
210 | ligi | 187 | |
164 | ligi | 188 | public void process_data(int[] data,int len) |
189 | { |
||
190 | int[] decoded_data; |
||
191 | |||
181 | ligi | 192 | |
164 | ligi | 193 | switch((char)data[2]) |
194 | { |
||
195 | |||
196 | case 'D': // debug Data |
||
208 | ligi | 197 | debug_data_count++; |
210 | ligi | 198 | debug_data.set_by_mk_data(Decode64(data,3,50),version); |
164 | ligi | 199 | break; |
200 | |||
201 | case 'V': // Version Info |
||
208 | ligi | 202 | version_data_count++; |
210 | ligi | 203 | version.set_by_mk_data(Decode64(data,3,6)); |
208 | ligi | 204 | break; |
205 | |||
206 | case '0': |
||
207 | case '1': |
||
208 | case '2': |
||
209 | case '3': |
||
210 | LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0'); |
||
211 | lcd_data_count++; |
||
164 | ligi | 212 | break; |
208 | ligi | 213 | |
164 | ligi | 214 | default: |
208 | ligi | 215 | other_data_count++; |
164 | ligi | 216 | break; |
217 | |||
218 | } |
||
219 | |||
220 | |||
208 | ligi | 221 | |
164 | ligi | 222 | |
223 | } |
||
224 | |||
208 | ligi | 225 | String o_msg=""; |
181 | ligi | 226 | |
208 | ligi | 227 | public boolean force_disconnect=false; |
181 | ligi | 228 | |
221 | ligi | 229 | public void close_connections(boolean force) |
208 | ligi | 230 | { |
221 | ligi | 231 | force_disconnect=force; |
208 | ligi | 232 | try{ reader.close(); } |
233 | catch (Exception inner_ex) { } |
||
181 | ligi | 234 | |
208 | ligi | 235 | try{ writer.close(); } |
236 | catch (Exception inner_ex) { } |
||
237 | |||
238 | try{ connection.close(); } |
||
239 | catch (Exception inner_ex) { } |
||
240 | |||
241 | connected=false; |
||
242 | } |
||
243 | |||
164 | ligi | 244 | // Thread to recieve data from Connection |
245 | public void run() |
||
246 | { |
||
247 | int[] data_set=new int[150]; |
||
248 | int input; |
||
249 | int pos=0; |
||
250 | msg+="!!run started!!"; |
||
251 | while(true) |
||
252 | { |
||
253 | if (!connected) |
||
181 | ligi | 254 | { |
208 | ligi | 255 | if (!force_disconnect) connect(); |
181 | ligi | 256 | } |
164 | ligi | 257 | else |
258 | try{ |
||
181 | ligi | 259 | |
164 | ligi | 260 | pos=0; |
261 | input=0; |
||
262 | // recieve data-set |
||
263 | while ((input != 13)) // &&(input!=-1)) |
||
264 | { |
||
265 | input = reader.read() ; |
||
266 | if (input==-1) throw new Exception("test"); |
||
267 | data_set[pos]=input; |
||
268 | pos++; |
||
269 | |||
270 | } |
||
271 | process_data(data_set,pos); |
||
272 | |||
273 | } |
||
274 | catch (Exception ex) |
||
275 | { |
||
276 | msg="Problem reading from MK"; |
||
277 | // close the connection |
||
221 | ligi | 278 | close_connections(false); |
208 | ligi | 279 | |
280 | |||
164 | ligi | 281 | } |
282 | |||
283 | // sleep a bit to get someting more done |
||
284 | try { Thread.sleep(50); } |
||
285 | catch (Exception e) { } |
||
286 | |||
287 | } // while |
||
288 | |||
289 | |||
290 | } // run() |
||
291 | |||
292 | |||
293 | } |