Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.swing;
28
 
29
import java.awt.*;
30
import java.awt.event.*;
31
import java.io.*;
32
import java.nio.*;
33
import java.nio.channels.*;
34
import java.util.*;
35
import javax.swing.*;
36
import javax.swing.border.*;
37
import javax.swing.event.*;
38
 
39
import avr.lang.*;
40
import avr.connection.event.ConnectionListener;
41
import avr.connection.event.ConnectionEvent;
42
 
43
public class JTrackingInternalFrame extends JInternalFrame {
44
 
45
   private static final int BLOB_LENGTH = 5;
46
 
47
   private JTrackingPanel trackingP;
48
 
49
   private AbstractButton recordB;
50
   private AbstractButton sendCameraDataB;
51
 
52
   private Action recordAction;
53
   private Action playAction;
54
   private Action pauseAction;
55
   private Action stopAction;
56
 
57
   private FileInputStream inStream;
58
   private FileChannel inChannel;
59
 
60
   private Map indexMap;
61
   private int numTracked;
62
 
63
   private boolean paused;
64
 
65
   private javax.swing.Timer timer;
66
 
67
   private JSlider playbackS;
68
 
69
   private ConnectionListener connectionHandler;
70
 
71
   // common initializer for both constructors
72
   {
73
      trackingP = new JTrackingPanel();
74
 
75
      createActions();
76
 
77
      setJMenuBar(createMenuBar());
78
      getContentPane().add(trackingP, BorderLayout.CENTER);
79
   }
80
 
81
   public JTrackingInternalFrame() {
82
      super("Tracking", true, false, true, false);
83
      getContentPane().add(createToolBar(), BorderLayout.NORTH);
84
   }
85
 
86
   public JTrackingInternalFrame(File trackingData) throws FileNotFoundException, IOException {
87
      super("Tracking: " + trackingData.toString(), true, true, true, true);
88
 
89
      inStream = new FileInputStream(trackingData);
90
      inChannel = inStream.getChannel();
91
 
92
      indexMap = new HashMap();
93
      numTracked = -1;
94
 
95
      paused = false;
96
 
97
      // build an index map to map an index to a position in a file
98
      indexMap.put(new Integer(0), new Integer(0));
99
 
100
      ByteBuffer numTrackedBuffer = ByteBuffer.allocate(1);
101
      int currentIndex = 0;
102
 
103
      while(inChannel.read(numTrackedBuffer) != -1) {
104
 
105
         numTrackedBuffer.flip();
106
         numTracked = numTrackedBuffer.get() & 0xFF;
107
 
108
         currentIndex++;
109
         int position = (int)(inChannel.position() + (numTracked * BLOB_LENGTH));
110
 
111
         indexMap.put(new Integer(currentIndex), new Integer(position));
112
 
113
         inChannel.position(position);
114
         numTrackedBuffer.clear();
115
 
116
      }
117
 
118
      sendCameraDataB = new JCheckBox(new ProxyAction(this, "sendCameraData", "Send To Serial Port"));
119
      sendCameraDataB.setEnabled(AVRSystem.DEVICE.isConnected());
120
 
121
      playbackS = new JSlider(JSlider.HORIZONTAL, 0, currentIndex - 1, 0);
122
      playbackS.setMinorTickSpacing(1);
123
      playbackS.setMajorTickSpacing(20);
124
      playbackS.setPaintTicks(true);
125
      playbackS.setPaintTrack(true);
126
      playbackS.setPaintLabels(true);
127
      playbackS.setSnapToTicks(true);
128
      playbackS.addChangeListener(new PlaybackHandler());
129
 
130
      JPanel southP = new JPanel(new BorderLayout());
131
      southP.setBorder(new EmptyBorder(5, 0, 0, 0));
132
 
133
//      JPanel controlP = new JPanel(new BorderLayout());
134
      JPanel controlP = new JPanel();
135
 
136
      controlP.add(new JButton(playAction));
137
      controlP.add(new JButton(pauseAction));
138
      controlP.add(new JButton(stopAction));
139
 
140
//
141
//      JPanel controlNorthP = new JPanel();
142
//      controlNorthP.add(new JButton(playAction));
143
//      controlNorthP.add(new JButton(pauseAction));
144
//      controlNorthP.add(new JButton(stopAction));
145
//
146
//      JPanel controlSouthP = new JPanel();
147
//      controlSouthP.add(sendCameraDataB);
148
//
149
//      controlP.add(controlNorthP, BorderLayout.NORTH);
150
//      controlP.add(controlSouthP, BorderLayout.SOUTH);
151
 
152
      southP.add(playbackS, BorderLayout.NORTH);
153
      southP.add(controlP, BorderLayout.SOUTH);
154
 
155
      getContentPane().add(southP, BorderLayout.SOUTH);
156
 
157
      trackingP.setTrackingData(read(0));
158
 
159
      connectionHandler = new ConnectionHandler();
160
      AVRSystem.DEVICE.addConnectionListener(connectionHandler);
161
 
162
   }
163
 
164
   private void createActions() {
165
      recordAction = new ProxyAction(this, "record", "Start Recording", 's');
166
      playAction = new ProxyAction(this, "play", "Play", 'p');
167
      stopAction = new ProxyAction(this, "stop", "Stop", 's');
168
      pauseAction = new ProxyAction(this, "pause", "Pause");
169
 
170
      stopAction.setEnabled(false);
171
      pauseAction.setEnabled(false);
172
 
173
   }
174
 
175
   private JMenuBar createMenuBar() {
176
 
177
      JMenuBar menubar = new JMenuBar();
178
 
179
      JMenu fileM = new JMenu("File");
180
 
181
      fileM.add(new ProxyAction(this, "pack", "Reset Size", 'R'));
182
      fileM.addSeparator();
183
      fileM.add(new ProxyAction(this, "dispose", "Exit", 'X'));
184
 
185
      menubar.add(fileM);
186
 
187
      return menubar;
188
 
189
   }
190
 
191
   private JToolBar createToolBar() {
192
 
193
      JToolBar toolbar = new JToolBar();
194
      toolbar.setFloatable(false);
195
 
196
      recordB = new JToggleButton(recordAction);
197
 
198
      toolbar.add(recordB);
199
 
200
      return toolbar;
201
 
202
   }
203
 
204
   public void dispose() {
205
      if(inStream != null) {
206
         if(timer != null) {
207
            stop();
208
         }
209
         try {
210
            inStream.close();
211
            inChannel.close();
212
         } catch(Exception e) {
213
            e.printStackTrace();
214
         }
215
      }
216
      AVRSystem.DEVICE.removeConnectionListener(connectionHandler);
217
      super.dispose();
218
   }
219
 
220
   public void play() {
221
      if(timer == null) {
222
         // set the timer to fire for 30 frames per second
223
         timer = new javax.swing.Timer((int)(1000 / 30), new UpdateSliderHandler());
224
      }
225
      timer.start();
226
      playAction.setEnabled(false);
227
      stopAction.setEnabled(true);
228
      pauseAction.setEnabled(true);
229
   }
230
 
231
   public void pause() {
232
      if(!paused) {
233
         timer.stop();
234
         playAction.setEnabled(false);
235
         stopAction.setEnabled(false);
236
         pauseAction.putValue(Action.NAME, "Resume");
237
         paused = true;
238
      } else {
239
         timer.start();
240
         playAction.setEnabled(false);
241
         stopAction.setEnabled(true);
242
         pauseAction.putValue(Action.NAME, "Pause");
243
         paused = false;
244
      }
245
   }
246
 
247
   public void stop() {
248
      timer.stop();
249
      playAction.setEnabled(true);
250
      stopAction.setEnabled(false);
251
      pauseAction.setEnabled(false);
252
      playbackS.setValue(0);
253
   }
254
 
255
   public void sendCameraData() {
256
      if(sendCameraDataB.isSelected()) {
257
         trackingP.startSendingCameraData();
258
      } else {
259
         trackingP.stopSendingCameraData();
260
      }
261
   }
262
 
263
   private ByteBuffer read(int position) throws IOException {
264
 
265
      inChannel.position(position);
266
 
267
      ByteBuffer numTrackedBuffer = ByteBuffer.allocate(1);
268
      inChannel.read(numTrackedBuffer);
269
      numTrackedBuffer.flip();
270
 
271
      int numTracked = numTrackedBuffer.get() & 0xFF;
272
 
273
      ByteBuffer blobBuffer = ByteBuffer.allocate(1 + (numTracked * BLOB_LENGTH));
274
 
275
      inChannel.position(inChannel.position() - 1);
276
      inChannel.read(blobBuffer);
277
      blobBuffer.flip();
278
 
279
      return blobBuffer;
280
 
281
   }
282
 
283
   public void record() {
284
      if(recordB.isSelected()) {
285
         try {
286
            trackingP.startRecording();
287
            recordB.setText("Stop Recording");
288
         } catch(Exception e) {
289
            recordB.setSelected(false);
290
         }
291
      } else {
292
         stopRecording();
293
      }
294
   }
295
 
296
   private void stopRecording() {
297
      try {
298
         trackingP.stopRecording();
299
         recordB.setText("Start Recording");
300
      } catch(Exception e) {
301
         recordB.setSelected(true);
302
      }
303
   }
304
 
305
   public void startTracking() {
306
      trackingP.startTracking();
307
   }
308
 
309
   public void stopTracking() {
310
      if(recordB.isSelected()) {
311
         stopRecording();
312
      }
313
      trackingP.stopTracking();
314
   }
315
 
316
   private final class PlaybackHandler implements ChangeListener {
317
      public void stateChanged(ChangeEvent ce) {
318
         try {
319
            trackingP.setTrackingData(read(((Integer)indexMap.get(new Integer(playbackS.getValue()))).intValue()));
320
         } catch(IOException ioe) {
321
            ioe.printStackTrace();
322
            AVRSystem.LOG.severe(ioe.getMessage());
323
         }
324
      }
325
   }
326
 
327
   private final class UpdateSliderHandler implements ActionListener {
328
      public void actionPerformed(ActionEvent ae) {
329
         if(playbackS.getValue() == playbackS.getMaximum()) {
330
            stop();
331
         } else {
332
            try {
333
               playbackS.setValue(playbackS.getValue() + 1);
334
            } catch(Exception e) {
335
               e.printStackTrace();
336
            }
337
         }
338
      }
339
   }
340
 
341
   private final class ConnectionHandler implements ConnectionListener {
342
      public void connected(ConnectionEvent ce) {
343
         sendCameraDataB.setEnabled(true);
344
      }
345
 
346
      public void disconnected(ConnectionEvent ce) {
347
         trackingP.stopSendingCameraData();
348
         sendCameraDataB.setSelected(false);
349
         sendCameraDataB.setEnabled(false);
350
      }
351
 
352
   }
353
 
354
}