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