package com.rle.monitor; /* * RLE Surface Crack Detector Graphing Software * * Copyright (C) 2002 RLE Technologies * For more information visit http://www.rletech.com/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ /** * @author Andy Goth * @author Eric Peterson * @author Matt Lane * @version 0.1 */ import java.io.*; import java.lang.*; import java.util.*; /** Scanner device emulated in software. */ public class Emulator extends ScannerAdapter implements Runnable { private Random rand = new Random(); private boolean dataTransfer; private boolean opened; private int gain = 32; private int offset = 128; private int threshhold = 64; private Thread thread; private int nextEcho = GAIN; private int prevSample = 128; /** Returns the next filtered sample. */ private int filter(int value) { return prevSample = (int)(value * 0.1 + prevSample * 0.9); } public void open(String portName) throws IOException { if (opened) throw new IOException("Device already opened"); opened = true; thread = new Thread(this, "Emulation"); thread.start(); beginDataTransfer(); } public void close() throws IOException { if (!opened) throw new IOException("Device not opened"); opened = false; thread.interrupt(); while (true) { try { thread.join(); return; } catch (InterruptedException e) {} } } public void beginDataTransfer() throws IOException { dataTransfer = true; } public void reset() throws IOException { dataTransfer = false; } public void setOffset(int o) throws IOException {offset = o; nextEcho = OFFSET;} public void setGain(int g) throws IOException {gain= g; nextEcho = GAIN;} public void setAutoOffset() throws IOException {} public void setAlarm(boolean f) throws IOException {} public void setRedLED() throws IOException {} public void setGreenLED() throws IOException {} public void setLED(boolean f) throws IOException {} public void setThreshhold(int t) throws IOException {threshhold = t; nextEcho = THRESHHOLD;} public void queryOffset() throws IOException {nextEcho = OFFSET;} public void queryGain() throws IOException {nextEcho = GAIN;} public void queryThreshhold() throws IOException {nextEcho = THRESHHOLD;} /** Emulation thread. */ public void run() { try { while (true) { if (!dataTransfer) { Thread.sleep(100); continue; } for (int i = 0; i < 19; i++) { Thread.sleep(0, 500000); int val; if (gain > 0) { val = rand.nextInt(gain * 2) + offset - gain; if (val < 0) val = 0; else if (val > 255) val = 255; } else val = offset; sendSample(filter(val)); } Thread.sleep(0, 500000); switch (nextEcho) { case GAIN: sendEcho(Scanner.GAIN, gain); nextEcho = OFFSET; break; case OFFSET: sendEcho(Scanner.OFFSET, offset); nextEcho = GAIN; break; case THRESHHOLD: sendEcho(Scanner.THRESHHOLD,threshhold); nextEcho = GAIN; } } } catch (InterruptedException e) {} } }