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