Subversion Repositories Projects

Rev

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