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