| 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.io;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | import java.io.*;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | public class AVRInputStream extends FilterInputStream {
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |    private static final int NEW_LINE = '\r';
 | 
        
           |  |  | 34 |    private static final int END_DUMP_DATA = 0x0F;
 | 
        
           |  |  | 35 |    private static final int END_TRACK_DATA = 0xFF;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |    private static final int SEARCHING_STATE = 0x00;
 | 
        
           |  |  | 38 |    private static final int DUMP_FRAME_STATE = 0x01;
 | 
        
           |  |  | 39 |    private static final int ACCEPTING_STATE = 0x02;
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |    public AVRInputStream(InputStream in) {
 | 
        
           |  |  | 42 |       super(in);
 | 
        
           |  |  | 43 |    }
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |    public int read(byte[] data, int off, int len) throws IOException {
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |       int terminator = NEW_LINE;
 | 
        
           |  |  | 48 |       int state = SEARCHING_STATE;
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |       int value = -1;
 | 
        
           |  |  | 51 |       int bytesRead = 0;
 | 
        
           |  |  | 52 |       boolean cont = false;
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |       do {
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |          value = read();
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |          if(value != -1) {
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |             switch(state) {
 | 
        
           |  |  | 61 |                case SEARCHING_STATE:
 | 
        
           |  |  | 62 |                   //System.out.println("(" + Integer.toHexString(value & 0xFF) + ") ");
 | 
        
           |  |  | 63 |                   data[off] = (byte)value;
 | 
        
           |  |  | 64 |                   if(value == 0x0A) {
 | 
        
           |  |  | 65 |                      terminator = END_TRACK_DATA;
 | 
        
           |  |  | 66 |                      state = ACCEPTING_STATE;
 | 
        
           |  |  | 67 |                      bytesRead++;
 | 
        
           |  |  | 68 |                   } else if(value == 0x0B) {
 | 
        
           |  |  | 69 |                      terminator = END_DUMP_DATA;
 | 
        
           |  |  | 70 |                      state = DUMP_FRAME_STATE;
 | 
        
           |  |  | 71 |                      bytesRead++;
 | 
        
           |  |  | 72 |                   } else if(value == 'A' || value == 'N') {
 | 
        
           |  |  | 73 |                      terminator = NEW_LINE;
 | 
        
           |  |  | 74 |                      state = ACCEPTING_STATE;
 | 
        
           |  |  | 75 |                      bytesRead++;
 | 
        
           |  |  | 76 |                   }
 | 
        
           |  |  | 77 |                   cont = false;
 | 
        
           |  |  | 78 |                   break;
 | 
        
           |  |  | 79 |                case DUMP_FRAME_STATE:
 | 
        
           |  |  | 80 |                   data[off + bytesRead++] = (byte)value;
 | 
        
           |  |  | 81 |                   state = ACCEPTING_STATE;
 | 
        
           |  |  | 82 |                   cont = true;
 | 
        
           |  |  | 83 |                   break;
 | 
        
           |  |  | 84 |                case ACCEPTING_STATE:
 | 
        
           |  |  | 85 |                   //System.out.println(Integer.toHexString(value & 0xFF) + " ");
 | 
        
           |  |  | 86 |                   data[off + bytesRead++] = (byte)value;
 | 
        
           |  |  | 87 |                   cont = false;
 | 
        
           |  |  | 88 |                   break;
 | 
        
           |  |  | 89 |             }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |          }
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 |       } while(cont || value != terminator);
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |       return bytesRead;
 | 
        
           |  |  | 97 |    }
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | }
 |