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