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