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