Subversion Repositories Projects

Rev

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