Rev 1695 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1695 | Rev 1696 | ||
---|---|---|---|
1 | package dongfang.mkt.comm; |
1 | package dongfang.mkt.comm; |
2 | 2 | ||
3 | import java.io.IOException; |
3 | import java.io.IOException; |
4 | import java.io.OutputStream; |
4 | import java.io.OutputStream; |
5 | 5 | ||
6 | import dongfang.mkt.RequestFrameVisitor; |
6 | import dongfang.mkt.RequestFrameVisitor; |
7 | import dongfang.mkt.frames.AllDisplaysRequestFrame; |
7 | import dongfang.mkt.frames.AllDisplaysRequestFrame; |
8 | import dongfang.mkt.frames.AnalogDebugLabelRequestFrame; |
8 | import dongfang.mkt.frames.AnalogDebugLabelRequestFrame; |
9 | import dongfang.mkt.frames.AttitudeDataRequestFrame; |
9 | import dongfang.mkt.frames.AttitudeDataRequestFrame; |
10 | import dongfang.mkt.frames.ChangeParameterSetRequestFrame; |
10 | import dongfang.mkt.frames.ChangeParameterSetRequestFrame; |
11 | import dongfang.mkt.frames.CompassHeadingRequestFrame; |
11 | import dongfang.mkt.frames.CompassHeadingRequestFrame; |
12 | import dongfang.mkt.frames.DCMMatrixRequestFrame; |
12 | import dongfang.mkt.frames.DCMMatrixRequestFrame; |
13 | import dongfang.mkt.frames.DebugRequestFrame; |
13 | import dongfang.mkt.frames.DebugRequestFrame; |
14 | import dongfang.mkt.frames.ExternalControlRequestFrame; |
14 | import dongfang.mkt.frames.ExternalControlRequestFrame; |
15 | import dongfang.mkt.frames.LoopbackTestRequestFrame; |
15 | import dongfang.mkt.frames.LoopbackTestRequestFrame; |
16 | import dongfang.mkt.frames.MotorTestRequestFrame; |
16 | import dongfang.mkt.frames.MotorTestRequestFrame; |
17 | import dongfang.mkt.frames.OSDDataRequestFrame; |
17 | import dongfang.mkt.frames.OSDDataRequestFrame; |
- | 18 | import dongfang.mkt.frames.ProfilerLabelRequestFrame; |
|
- | 19 | import dongfang.mkt.frames.ProfilerRequestFrame; |
|
18 | import dongfang.mkt.frames.ReadExternalControlRequestFrame; |
20 | import dongfang.mkt.frames.ReadExternalControlRequestFrame; |
19 | import dongfang.mkt.frames.ReadIMUConfigurationRequestFrame; |
21 | import dongfang.mkt.frames.ReadIMUConfigurationRequestFrame; |
20 | import dongfang.mkt.frames.ReadMotorMixerRequestFrame; |
22 | import dongfang.mkt.frames.ReadMotorMixerRequestFrame; |
21 | import dongfang.mkt.frames.ReadParamSetRequestFrame; |
23 | import dongfang.mkt.frames.ReadParamSetRequestFrame; |
22 | import dongfang.mkt.frames.ReadRCChannelsRequestFrame; |
24 | import dongfang.mkt.frames.ReadRCChannelsRequestFrame; |
23 | import dongfang.mkt.frames.ReadVariablesRequestFrame; |
25 | import dongfang.mkt.frames.ReadVariablesRequestFrame; |
24 | import dongfang.mkt.frames.RequestFrame; |
26 | import dongfang.mkt.frames.RequestFrame; |
25 | import dongfang.mkt.frames.ResetRequestFrame; |
27 | import dongfang.mkt.frames.ResetRequestFrame; |
26 | import dongfang.mkt.frames.SetCompassHeadingRequestFrame; |
28 | import dongfang.mkt.frames.SetCompassHeadingRequestFrame; |
27 | import dongfang.mkt.frames.SingleDisplayRequestFrame; |
29 | import dongfang.mkt.frames.SingleDisplayRequestFrame; |
28 | import dongfang.mkt.frames.VersionRequestFrame; |
30 | import dongfang.mkt.frames.VersionRequestFrame; |
29 | import dongfang.mkt.frames.WriteIMUConfigurationRequestFrame; |
31 | import dongfang.mkt.frames.WriteIMUConfigurationRequestFrame; |
30 | import dongfang.mkt.frames.WriteMotorMixerRequestFrame; |
32 | import dongfang.mkt.frames.WriteMotorMixerRequestFrame; |
31 | import dongfang.mkt.frames.WriteParamSetRequestFrame; |
33 | import dongfang.mkt.frames.WriteParamSetRequestFrame; |
32 | 34 | ||
33 | public class MKOutputStream extends OutputStream implements RequestFrameVisitor { |
35 | public class MKOutputStream extends OutputStream implements RequestFrameVisitor { |
34 | public class MKDataOutputStream { |
36 | public class MKDataOutputStream { |
35 | int[] inbuf = new int[3]; |
37 | int[] inbuf = new int[3]; |
36 | int[] outbuf = new int[4]; |
38 | int[] outbuf = new int[4]; |
37 | int inbufptr = 0; |
39 | int inbufptr = 0; |
38 | 40 | ||
39 | void writeByte(int b) throws IOException { |
41 | void writeByte(int b) throws IOException { |
40 | if(inbufptr == inbuf.length) flush(); |
42 | if(inbufptr == inbuf.length) flush(); |
41 | inbuf[inbufptr++] = b; |
43 | inbuf[inbufptr++] = b; |
42 | } |
44 | } |
43 | 45 | ||
44 | void writeSignedByte(int b) throws IOException { |
46 | void writeSignedByte(int b) throws IOException { |
45 | if (b<0) b+= 256; |
47 | if (b<0) b+= 256; |
46 | writeByte(b); |
48 | writeByte(b); |
47 | } |
49 | } |
48 | 50 | ||
49 | public void writeBytes(int[] bs) throws IOException { |
51 | public void writeBytes(int[] bs) throws IOException { |
50 | for (int i=0; i<bs.length; i++) |
52 | for (int i=0; i<bs.length; i++) |
51 | writeByte(bs[i]); |
53 | writeByte(bs[i]); |
52 | } |
54 | } |
53 | 55 | ||
54 | void writeWord(int w) throws IOException { |
56 | void writeWord(int w) throws IOException { |
55 | writeByte(w & 0xff); |
57 | writeByte(w & 0xff); |
56 | writeByte(w >>> 8); |
58 | writeByte(w >>> 8); |
57 | } |
59 | } |
58 | 60 | ||
59 | void writeChars(char[] s) throws IOException { |
61 | void writeChars(char[] s) throws IOException { |
60 | for (int i=0; i<s.length; i++) { |
62 | for (int i=0; i<s.length; i++) { |
61 | // Here, a 1:1 mapping between byte values and char codes is assumed. |
63 | // Here, a 1:1 mapping between byte values and char codes is assumed. |
62 | // That means we're assuming ISO-8859-1 (= the first 256 code points |
64 | // That means we're assuming ISO-8859-1 (= the first 256 code points |
63 | // of Unicode, which Java uses for chars) |
65 | // of Unicode, which Java uses for chars) |
64 | writeByte(s[i]); |
66 | writeByte(s[i]); |
65 | } |
67 | } |
66 | } |
68 | } |
67 | 69 | ||
68 | void flush() throws IOException { |
70 | void flush() throws IOException { |
69 | if (inbufptr == 0) |
71 | if (inbufptr == 0) |
70 | return; |
72 | return; |
71 | 73 | ||
72 | while(inbufptr < inbuf.length) { |
74 | while(inbufptr < inbuf.length) { |
73 | // add padding .. well just clear it, for tidyness. |
75 | // add padding .. well just clear it, for tidyness. |
74 | inbuf[inbufptr++] = 0; |
76 | inbuf[inbufptr++] = 0; |
75 | } |
77 | } |
76 | 78 | ||
77 | MKOutputStream.this.writeByte((inbuf[0] >>> 2) + '='); |
79 | MKOutputStream.this.writeByte((inbuf[0] >>> 2) + '='); |
78 | MKOutputStream.this.writeByte(( ((inbuf[0] & 0x03) << 4) | (inbuf[1] >>> 4) ) + '='); |
80 | MKOutputStream.this.writeByte(( ((inbuf[0] & 0x03) << 4) | (inbuf[1] >>> 4) ) + '='); |
79 | MKOutputStream.this.writeByte(( ((inbuf[1] & 0x0f) << 2) | (inbuf[2] >>> 6)) + '='); |
81 | MKOutputStream.this.writeByte(( ((inbuf[1] & 0x0f) << 2) | (inbuf[2] >>> 6)) + '='); |
80 | MKOutputStream.this.writeByte(((inbuf[2] & 0x3f) + '=')); |
82 | MKOutputStream.this.writeByte(((inbuf[2] & 0x3f) + '=')); |
81 | 83 | ||
82 | inbufptr = 0; |
84 | inbufptr = 0; |
83 | } |
85 | } |
84 | } |
86 | } |
85 | 87 | ||
86 | final OutputStream os; |
88 | final OutputStream os; |
87 | MKDataOutputStream base64OutputStream = new MKDataOutputStream(); |
89 | MKDataOutputStream base64OutputStream = new MKDataOutputStream(); |
88 | int crc; |
90 | int crc; |
89 | 91 | ||
90 | public MKOutputStream(OutputStream os) { |
92 | public MKOutputStream(OutputStream os) { |
91 | this.os = os; |
93 | this.os = os; |
92 | } |
94 | } |
93 | 95 | ||
94 | @Override |
96 | @Override |
95 | public void write(int b) throws IOException { |
97 | public void write(int b) throws IOException { |
96 | os.write(b); |
98 | os.write(b); |
97 | // System.out.println("Writing: " + b); |
99 | // System.out.println("Writing: " + b); |
98 | } |
100 | } |
99 | 101 | ||
100 | public void writeByte(int b) throws IOException { |
102 | public void writeByte(int b) throws IOException { |
101 | crc += b; |
103 | crc += b; |
102 | write(b); |
104 | write(b); |
103 | } |
105 | } |
104 | 106 | ||
105 | public void write(RequestFrame f) throws IOException { |
107 | public void write(RequestFrame f) throws IOException { |
106 | write(crc = '#'); |
108 | write(crc = '#'); |
107 | 109 | ||
108 | int address = f.getAddress() + 'a'; |
110 | int address = f.getAddress() + 'a'; |
109 | writeByte(address); |
111 | writeByte(address); |
110 | 112 | ||
111 | // Will cause one of the "visit" methods below |
113 | // Will cause one of the "visit" methods below |
112 | // to be called, depending on the type of f. |
114 | // to be called, depending on the type of f. |
113 | f.accept(this); |
115 | f.accept(this); |
114 | base64OutputStream.flush(); |
116 | base64OutputStream.flush(); |
115 | 117 | ||
116 | crc %= 4096; |
118 | crc %= 4096; |
117 | write((crc >>> 6) + '='); |
119 | write((crc >>> 6) + '='); |
118 | write((crc & 0x3f) + '='); |
120 | write((crc & 0x3f) + '='); |
119 | 121 | ||
120 | write('\r'); |
122 | write('\r'); |
121 | } |
123 | } |
122 | 124 | ||
123 | /* |
125 | /* |
124 | public void visit(RequestFrame f) { |
126 | public void visit(RequestFrame f) { |
125 | throw new RuntimeException("Unbound RequestFrame type: " |
127 | throw new RuntimeException("Unbound RequestFrame type: " |
126 | + f.getClass().getSimpleName() |
128 | + f.getClass().getSimpleName() |
127 | + ". Don't know how to output."); |
129 | + ". Don't know how to output."); |
128 | } |
130 | } |
129 | */ |
131 | */ |
130 | 132 | ||
131 | public void visit(AnalogDebugLabelRequestFrame f) throws IOException { |
133 | public void visit(AnalogDebugLabelRequestFrame f) throws IOException { |
132 | writeByte('a'); |
134 | writeByte('a'); |
133 | base64OutputStream.writeByte(f.getChannel()); |
135 | base64OutputStream.writeByte(f.getChannel()); |
134 | } |
136 | } |
- | 137 | ||
- | 138 | public void visit(ProfilerLabelRequestFrame f) throws IOException { |
|
- | 139 | writeByte('f'); |
|
- | 140 | base64OutputStream.writeByte(f.getChannel()); |
|
- | 141 | } |
|
135 | 142 | ||
136 | public void visit(AttitudeDataRequestFrame f) throws IOException { |
143 | public void visit(AttitudeDataRequestFrame f) throws IOException { |
137 | writeByte('c'); |
144 | writeByte('c'); |
138 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
145 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
139 | } |
146 | } |
140 | 147 | ||
141 | public void visit(DebugRequestFrame f) throws IOException { |
148 | public void visit(DebugRequestFrame f) throws IOException { |
142 | writeByte('d'); |
149 | writeByte('d'); |
143 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
150 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
- | 151 | } |
|
- | 152 | ||
- | 153 | public void visit(ProfilerRequestFrame f) throws IOException { |
|
- | 154 | writeByte('u'); |
|
- | 155 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
|
144 | } |
156 | } |
145 | 157 | ||
146 | public void visit(DCMMatrixRequestFrame f) throws IOException { |
158 | public void visit(DCMMatrixRequestFrame f) throws IOException { |
147 | writeByte('e'); |
159 | writeByte('e'); |
148 | } |
160 | } |
149 | 161 | ||
150 | public void visit(ChangeParameterSetRequestFrame f) throws IOException { |
162 | public void visit(ChangeParameterSetRequestFrame f) throws IOException { |
151 | writeByte('f'); |
163 | writeByte('f'); |
152 | base64OutputStream.writeByte(f.getParameterSetNumber()); |
164 | base64OutputStream.writeByte(f.getParameterSetNumber()); |
153 | } |
165 | } |
154 | 166 | ||
155 | public void visit(VersionRequestFrame f) throws IOException { |
167 | public void visit(VersionRequestFrame f) throws IOException { |
156 | writeByte('v'); |
168 | writeByte('v'); |
157 | } |
169 | } |
158 | 170 | ||
159 | public void visit(ResetRequestFrame f) throws IOException { |
171 | public void visit(ResetRequestFrame f) throws IOException { |
160 | writeByte('R'); |
172 | writeByte('R'); |
161 | } |
173 | } |
162 | 174 | ||
163 | public void visit(MotorTestRequestFrame f) throws IOException { |
175 | public void visit(MotorTestRequestFrame f) throws IOException { |
164 | writeByte('t'); |
176 | writeByte('t'); |
165 | base64OutputStream.writeBytes(f.getMotorValues()); |
177 | base64OutputStream.writeBytes(f.getMotorValues()); |
166 | } |
178 | } |
167 | 179 | ||
168 | public void visit(SingleDisplayRequestFrame f) throws IOException { |
180 | public void visit(SingleDisplayRequestFrame f) throws IOException { |
169 | writeByte('l'); |
181 | writeByte('l'); |
170 | base64OutputStream.writeByte(f.getMenuItemCode()); // In 0.74 there is not the all in one mode. |
182 | base64OutputStream.writeByte(f.getMenuItemCode()); // In 0.74 there is not the all in one mode. |
171 | } |
183 | } |
172 | 184 | ||
173 | public void visit(AllDisplaysRequestFrame f) throws IOException { |
185 | public void visit(AllDisplaysRequestFrame f) throws IOException { |
174 | writeByte('h'); |
186 | writeByte('h'); |
175 | base64OutputStream.writeByte(f.getPageOrder().getRemoteKeys()); |
187 | base64OutputStream.writeByte(f.getPageOrder().getRemoteKeys()); |
176 | // mdo.writeByte(f.getAutoSendInterval()); |
188 | // mdo.writeByte(f.getAutoSendInterval()); |
177 | } |
189 | } |
178 | 190 | ||
179 | public void visit(ReadMotorMixerRequestFrame f) throws IOException { |
191 | public void visit(ReadMotorMixerRequestFrame f) throws IOException { |
180 | writeByte('n'); |
192 | writeByte('n'); |
181 | } |
193 | } |
182 | 194 | ||
183 | public void visit(WriteMotorMixerRequestFrame f) throws IOException { |
195 | public void visit(WriteMotorMixerRequestFrame f) throws IOException { |
184 | writeByte('m'); |
196 | writeByte('m'); |
185 | base64OutputStream.writeByte(f.getMotorMixerVersionNumber()); |
197 | base64OutputStream.writeByte(f.getMotorMixerVersionNumber()); |
186 | base64OutputStream.writeByte(f.getDataLength()); |
198 | base64OutputStream.writeByte(f.getDataLength()); |
187 | base64OutputStream.writeChars(f.getName()); |
199 | base64OutputStream.writeChars(f.getName()); |
188 | for(int i=0; i<f.getMatrix().length; i++) { |
200 | for(int i=0; i<f.getMatrix().length; i++) { |
189 | int[] row = f.getMatrix()[i]; |
201 | int[] row = f.getMatrix()[i]; |
190 | for(int j=0; j<row.length; j++) { |
202 | for(int j=0; j<row.length; j++) { |
191 | base64OutputStream.writeSignedByte(row[j]); |
203 | base64OutputStream.writeSignedByte(row[j]); |
192 | //System.out.print(row[j] + " "); |
204 | //System.out.print(row[j] + " "); |
193 | } |
205 | } |
194 | //System.out.println(); |
206 | //System.out.println(); |
195 | } |
207 | } |
196 | /* |
208 | /* |
197 | for(int i=0; i<f.getOppositeMotors().length; i++) { |
209 | for(int i=0; i<f.getOppositeMotors().length; i++) { |
198 | base64OutputStream.writeByte(f.getOppositeMotors()[i]); |
210 | base64OutputStream.writeByte(f.getOppositeMotors()[i]); |
199 | } |
211 | } |
200 | */ |
212 | */ |
201 | } |
213 | } |
202 | 214 | ||
203 | public void visit(ReadVariablesRequestFrame f) throws IOException { |
215 | public void visit(ReadVariablesRequestFrame f) throws IOException { |
204 | writeByte('x'); |
216 | writeByte('x'); |
205 | } |
217 | } |
206 | 218 | ||
207 | public void visit(ExternalControlRequestFrame f) throws IOException { |
219 | public void visit(ExternalControlRequestFrame f) throws IOException { |
208 | writeByte('b'); |
220 | writeByte('b'); |
209 | base64OutputStream.writeByte(f.getDigital()[0]); |
221 | base64OutputStream.writeByte(f.getDigital()[0]); |
210 | base64OutputStream.writeByte(f.getDigital()[1]); |
222 | base64OutputStream.writeByte(f.getDigital()[1]); |
211 | base64OutputStream.writeByte(f.getRemoteButtons()); |
223 | base64OutputStream.writeByte(f.getRemoteButtons()); |
212 | base64OutputStream.writeByte(f.getPitch()); |
224 | base64OutputStream.writeByte(f.getPitch()); |
213 | base64OutputStream.writeByte(f.getRoll()); |
225 | base64OutputStream.writeByte(f.getRoll()); |
214 | base64OutputStream.writeByte(f.getYaw()); |
226 | base64OutputStream.writeByte(f.getYaw()); |
215 | base64OutputStream.writeByte(f.getThrottle()); |
227 | base64OutputStream.writeByte(f.getThrottle()); |
216 | base64OutputStream.writeByte(f.getHeight()); |
228 | base64OutputStream.writeByte(f.getHeight()); |
217 | base64OutputStream.writeByte(f.getCommand()); |
229 | base64OutputStream.writeByte(f.getCommand()); |
218 | base64OutputStream.writeByte(f.getFrameNum()); |
230 | base64OutputStream.writeByte(f.getFrameNum()); |
219 | base64OutputStream.writeByte(f.getArgument()); |
231 | base64OutputStream.writeByte(f.getArgument()); |
220 | } |
232 | } |
221 | 233 | ||
222 | public void visit(ReadExternalControlRequestFrame f) throws IOException { |
234 | public void visit(ReadExternalControlRequestFrame f) throws IOException { |
223 | writeByte('g'); |
235 | writeByte('g'); |
224 | } |
236 | } |
225 | 237 | ||
226 | public void visit(LoopbackTestRequestFrame f) throws IOException { |
238 | public void visit(LoopbackTestRequestFrame f) throws IOException { |
227 | writeByte('0'); |
239 | writeByte('0'); |
228 | base64OutputStream.writeByte(f.getByte()); |
240 | base64OutputStream.writeByte(f.getByte()); |
229 | base64OutputStream.writeWord(f.getWord()); |
241 | base64OutputStream.writeWord(f.getWord()); |
230 | base64OutputStream.writeChars(f.getChararray()); |
242 | base64OutputStream.writeChars(f.getChararray()); |
231 | } |
243 | } |
232 | 244 | ||
233 | public void visit(ReadParamSetRequestFrame f) throws IOException { |
245 | public void visit(ReadParamSetRequestFrame f) throws IOException { |
234 | writeByte('q'); |
246 | writeByte('q'); |
235 | base64OutputStream.writeByte(f.getConfigurationSetNumber()); |
247 | base64OutputStream.writeByte(f.getConfigurationSetNumber()); |
236 | } |
248 | } |
237 | 249 | ||
238 | public void visit(WriteParamSetRequestFrame f) throws IOException { |
250 | public void visit(WriteParamSetRequestFrame f) throws IOException { |
239 | writeByte('s'); |
251 | writeByte('s'); |
240 | base64OutputStream.writeByte(f.getConfigurationSetNumber()); |
252 | base64OutputStream.writeByte(f.getConfigurationSetNumber()); |
241 | base64OutputStream.writeByte(f.getConfigurationVersionNumber()); |
253 | base64OutputStream.writeByte(f.getConfigurationVersionNumber()); |
242 | base64OutputStream.writeByte(f.getDataLength() + 12); |
254 | base64OutputStream.writeByte(f.getDataLength() + 12); |
243 | base64OutputStream.writeBytes(f.getData()); |
255 | base64OutputStream.writeBytes(f.getData()); |
244 | base64OutputStream.writeChars(f.getName()); |
256 | base64OutputStream.writeChars(f.getName()); |
245 | } |
257 | } |
246 | 258 | ||
247 | public void visit(SetCompassHeadingRequestFrame f) throws IOException { |
259 | public void visit(SetCompassHeadingRequestFrame f) throws IOException { |
248 | writeByte('K'); |
260 | writeByte('K'); |
249 | } |
261 | } |
250 | 262 | ||
251 | public void visit(CompassHeadingRequestFrame f) throws IOException { |
263 | public void visit(CompassHeadingRequestFrame f) throws IOException { |
252 | writeByte('w'); |
264 | writeByte('w'); |
253 | } |
265 | } |
254 | 266 | ||
255 | public void visit(OSDDataRequestFrame f) throws IOException { |
267 | public void visit(OSDDataRequestFrame f) throws IOException { |
256 | writeByte('o'); |
268 | writeByte('o'); |
257 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
269 | base64OutputStream.writeByte(f.getAutoSendInterval()); |
258 | } |
270 | } |
259 | 271 | ||
260 | public void visit(ReadIMUConfigurationRequestFrame f) throws IOException { |
272 | public void visit(ReadIMUConfigurationRequestFrame f) throws IOException { |
261 | writeByte('i'); |
273 | writeByte('i'); |
262 | } |
274 | } |
263 | 275 | ||
264 | public void visit(WriteIMUConfigurationRequestFrame f) throws IOException { |
276 | public void visit(WriteIMUConfigurationRequestFrame f) throws IOException { |
265 | writeByte('j'); |
277 | writeByte('j'); |
266 | base64OutputStream.writeByte(f.getConfigurationVersionNumber()); |
278 | base64OutputStream.writeByte(f.getConfigurationVersionNumber()); |
267 | base64OutputStream.writeByte(f.getDataLength()); |
279 | base64OutputStream.writeByte(f.getDataLength()); |
268 | base64OutputStream.writeBytes(f.getData()); |
280 | base64OutputStream.writeBytes(f.getData()); |
269 | } |
281 | } |
270 | 282 | ||
271 | public void visit(ReadRCChannelsRequestFrame f) throws IOException { |
283 | public void visit(ReadRCChannelsRequestFrame f) throws IOException { |
272 | writeByte('p'); |
284 | writeByte('p'); |
273 | } |
285 | } |
274 | } |
286 | } |
275 | 287 |