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