Details | 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; |
||
208 | ligi | 38 | |
181 | ligi | 39 | public long connection_start_time=-1; |
164 | ligi | 40 | |
41 | |||
42 | /****************** Section: private Attributes **********************************************/ |
||
43 | private javax.microedition.io.StreamConnection connection; |
||
44 | private java.io.InputStream reader; |
||
45 | private java.io.OutputStream writer; |
||
46 | |||
47 | |||
48 | // temp - to be removed |
||
49 | String p_msg="--"; |
||
50 | public String msg="BT_INIT"; |
||
51 | |||
208 | ligi | 52 | public int debug_data_count=0; |
53 | public int version_data_count=0; |
||
54 | public int other_data_count=0; |
||
55 | public int lcd_data_count=0; |
||
164 | ligi | 56 | |
57 | |||
58 | /****************** Section: public Methods ************************************************/ |
||
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 |
||
60 | { |
||
210 | ligi | 61 | version=new MKVersion(); |
181 | ligi | 62 | debug_data=new MKDebugData(); |
164 | ligi | 63 | mk_url=url; // remember URL for connecting / reconnecting later |
181 | ligi | 64 | |
164 | ligi | 65 | new Thread( this ).start(); // fire up main Thread |
66 | } |
||
67 | |||
68 | |||
69 | /****************** Section: private Methods ************************************************/ |
||
70 | private void connect() |
||
71 | { |
||
72 | try{ |
||
73 | connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE); |
||
74 | reader=connection.openInputStream(); |
||
75 | writer=connection.openOutputStream(); |
||
181 | ligi | 76 | |
77 | connection_start_time=System.currentTimeMillis(); |
||
164 | ligi | 78 | connected=true; // if we get here everything seems to be OK |
181 | ligi | 79 | get_version(); |
208 | ligi | 80 | LCD= new MKLCD(this); |
164 | ligi | 81 | } |
82 | catch (Exception ex) |
||
83 | { |
||
84 | // TODO difference fatal errors from those which will lead to reconnection |
||
85 | msg="Problem connecting" + "\n" + ex; |
||
86 | } |
||
181 | ligi | 87 | |
88 | |||
89 | |||
164 | ligi | 90 | } |
91 | |||
92 | |||
93 | |||
94 | public int[] Decode64(int[] in_arr, int offset,int len) |
||
95 | { |
||
96 | int ptrIn=offset; |
||
97 | int a,b,c,d,x,y,z; |
||
98 | int ptr=0; |
||
99 | |||
100 | int[] out_arr=new int[len]; |
||
101 | |||
102 | while(len!=0) |
||
103 | { |
||
104 | a = in_arr[ptrIn++] - '='; |
||
105 | b = in_arr[ptrIn++] - '='; |
||
106 | c = in_arr[ptrIn++] - '='; |
||
107 | d = in_arr[ptrIn++] - '='; |
||
108 | //if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
109 | |||
110 | x = (a << 2) | (b >> 4); |
||
111 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
112 | z = ((c & 0x03) << 6) | d; |
||
113 | |||
114 | if((len--)!=0) out_arr[ptr++] = x; else break; |
||
115 | if((len--)!=0) out_arr[ptr++] = y; else break; |
||
116 | if((len--)!=0) out_arr[ptr++] = z; else break; |
||
117 | } |
||
118 | |||
119 | return out_arr; |
||
120 | |||
121 | } |
||
122 | |||
123 | |||
124 | // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives |
||
125 | public void get_version() |
||
126 | { |
||
127 | send_command(0,'v',new int[0]); |
||
128 | } |
||
129 | |||
208 | ligi | 130 | |
164 | ligi | 131 | // send command to FC ( add crc and pack into pseudo Base64 |
132 | public void send_command(int modul,char cmd,int[] params) |
||
133 | { |
||
134 | 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 |
||
135 | send_buff[0]='#'; |
||
136 | send_buff[1]=(char)modul; |
||
137 | send_buff[2]=cmd; |
||
138 | |||
208 | ligi | 139 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
140 | { |
||
141 | int a = (param_pos<params.length)?params[param_pos]:0; |
||
142 | int b = ((param_pos+1)<params.length)?params[param_pos+1]:0; |
||
143 | int c = ((param_pos+2)<params.length)?params[param_pos+2]:0; |
||
144 | |||
145 | send_buff[3+param_pos*4] = (char)((a >> 2)+'=' ); |
||
146 | send_buff[3+param_pos*4+1] = (char)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
||
147 | send_buff[3+param_pos*4+2] = (char)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
||
148 | send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f)); |
||
149 | |||
150 | //send_buff[3+foo]='='; |
||
151 | } |
||
152 | |||
153 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
||
154 | { |
||
155 | int a = (foo<params.length) params[foo]; |
||
156 | int a = params[foo]; |
||
157 | |||
158 | //send_buff[3+foo]='='; |
||
159 | } |
||
160 | */ |
||
164 | ligi | 161 | try |
162 | { |
||
163 | int tmp_crc=0; |
||
164 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
||
165 | { |
||
166 | tmp_crc+=(int)send_buff[tmp_i]; |
||
167 | writer.write(send_buff[tmp_i]); |
||
168 | } |
||
169 | tmp_crc%=4096; |
||
170 | writer.write( (char)(tmp_crc/64 + '=')); |
||
171 | writer.write( (char)(tmp_crc%64 + '=')); |
||
172 | writer.write('\r'); |
||
173 | writer.flush(); |
||
174 | } |
||
175 | catch (Exception e) |
||
176 | { // problem sending data to FC |
||
177 | } |
||
178 | |||
179 | } |
||
180 | |||
210 | ligi | 181 | |
164 | ligi | 182 | public void process_data(int[] data,int len) |
183 | { |
||
184 | int[] decoded_data; |
||
185 | |||
181 | ligi | 186 | |
164 | ligi | 187 | switch((char)data[2]) |
188 | { |
||
189 | |||
190 | case 'D': // debug Data |
||
208 | ligi | 191 | debug_data_count++; |
210 | ligi | 192 | debug_data.set_by_mk_data(Decode64(data,3,50),version); |
164 | ligi | 193 | break; |
194 | |||
195 | case 'V': // Version Info |
||
208 | ligi | 196 | version_data_count++; |
210 | ligi | 197 | version.set_by_mk_data(Decode64(data,3,6)); |
208 | ligi | 198 | break; |
199 | |||
200 | case '0': |
||
201 | case '1': |
||
202 | case '2': |
||
203 | case '3': |
||
204 | LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0'); |
||
205 | lcd_data_count++; |
||
206 | // decoded_data= |
||
164 | ligi | 207 | |
208 | ligi | 208 | |
209 | // if ((data[2]-(int)'0')!=3) |
||
210 | // get_LCD(); |
||
164 | ligi | 211 | break; |
208 | ligi | 212 | |
164 | ligi | 213 | default: |
208 | ligi | 214 | other_data_count++; |
164 | ligi | 215 | break; |
216 | |||
217 | } |
||
218 | |||
219 | |||
208 | ligi | 220 | |
164 | ligi | 221 | |
222 | } |
||
223 | |||
208 | ligi | 224 | String o_msg=""; |
181 | ligi | 225 | |
208 | ligi | 226 | public boolean force_disconnect=false; |
181 | ligi | 227 | |
208 | ligi | 228 | public void close_connections() |
229 | { |
||
230 | force_disconnect=true; |
||
231 | try{ reader.close(); } |
||
232 | catch (Exception inner_ex) { } |
||
181 | ligi | 233 | |
208 | ligi | 234 | try{ writer.close(); } |
235 | catch (Exception inner_ex) { } |
||
236 | |||
237 | try{ connection.close(); } |
||
238 | catch (Exception inner_ex) { } |
||
239 | |||
240 | connected=false; |
||
241 | } |
||
242 | |||
164 | ligi | 243 | // Thread to recieve data from Connection |
244 | public void run() |
||
245 | { |
||
246 | int[] data_set=new int[150]; |
||
247 | int input; |
||
248 | int pos=0; |
||
249 | msg+="!!run started!!"; |
||
250 | while(true) |
||
251 | { |
||
252 | if (!connected) |
||
181 | ligi | 253 | { |
208 | ligi | 254 | if (!force_disconnect) connect(); |
181 | ligi | 255 | } |
164 | ligi | 256 | else |
257 | try{ |
||
181 | ligi | 258 | |
164 | ligi | 259 | pos=0; |
260 | input=0; |
||
261 | // recieve data-set |
||
262 | while ((input != 13)) // &&(input!=-1)) |
||
263 | { |
||
264 | input = reader.read() ; |
||
265 | if (input==-1) throw new Exception("test"); |
||
266 | data_set[pos]=input; |
||
267 | pos++; |
||
268 | |||
269 | } |
||
270 | process_data(data_set,pos); |
||
271 | |||
272 | } |
||
273 | catch (Exception ex) |
||
274 | { |
||
275 | msg="Problem reading from MK"; |
||
276 | // close the connection |
||
208 | ligi | 277 | close_connections(); |
278 | |||
279 | |||
164 | ligi | 280 | } |
281 | |||
282 | // sleep a bit to get someting more done |
||
283 | try { Thread.sleep(50); } |
||
284 | catch (Exception e) { } |
||
285 | |||
286 | } // while |
||
287 | |||
288 | |||
289 | } // run() |
||
290 | |||
291 | |||
292 | } |