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