Subversion Repositories Projects

Rev

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