org.openuat.sensors
Class SamplesSource

java.lang.Object
  extended by org.openuat.sensors.SamplesSource
Direct Known Subclasses:
AsciiLineReaderBase, SymbianTCPAccelerometerReader

public abstract class SamplesSource
extends java.lang.Object

This is a base class for emitting samples to a list of registers SamplesSink objects. It imlements handling the listeners and the background thread for doing the sampling.

Version:
1.0
Author:
Rene Mayrhofer

Field Summary
protected  int maxNumLines
          The maximum number of data lines to read from the device - depends on the sensor.
 
Constructor Summary
protected SamplesSource(int maxNumLines, int sleepBetweenReads)
          Initializes the reader base object.
 
Method Summary
 void addSink(int[] lines, SamplesSink_Int[] intSinks)
          Registers a sink, which will receive all new values as they are sampled.
 void addSink(int[] lines, SamplesSink[] doubleSinks)
          Registers a sink, which will receive all new values as they are sampled.
 void dispose()
          This causes the reader to be shut down properly by calling stop().
protected  void emitSample(double[] sample)
          This method should be called by the parseLine method to send samples to all registered listeners.
protected  void emitSample(int[] sample)
          This method should be called by the parseLine method to send samples to all registered listeners.
 int getMaxNumLines()
          Returns the maximum number of lines that can be sampled.
abstract  TimeSeries_Int.Parameters getParameters_Int()
           
abstract  TimeSeries.Parameters getParameters()
          Each time samples source must be able to provide the appropriate parameters for normalizing its values to the [-1;1] range.
protected abstract  boolean handleSample()
          This method is called whenever a sample should be read from the respective source, and it should in turn call emitSample to send the new sample to all registered listeners.
 boolean removeSink_Int(int[] lines, SamplesSink_Int[] intSinks)
          Removes a previously registered sink.
 boolean removeSink(int[] lines, SamplesSink[] doubleSinks)
          Removes a previously registered sink.
 void simulateSampling()
          Simulate sampling by reading all available lines from the spcified file.
 void start()
          Starts a new background thread to read from the file and create sample values as the lines are read.
 void stop()
          Stops the background thread, if started previously.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxNumLines

protected int maxNumLines
The maximum number of data lines to read from the device - depends on the sensor.

Constructor Detail

SamplesSource

protected SamplesSource(int maxNumLines,
                        int sleepBetweenReads)
Initializes the reader base object. It only saves the passed parameters, but the member variable @see #port needs to be initialized separately before starting and sampling.

Parameters:
filename - The log to read from. This may either be a normal log file when simulation is intended or it can be a FIFO/pipe to read online data.
maxNumLines - The maximum number of data lines to read from the device - depends on the sensor.
sleepBetweenReads - The number of milliseconds to sleep between two reads from filename. Set to 0 to do blocking reads (i.e. as fast as the file can give something back).
Method Detail

addSink

public void addSink(int[] lines,
                    SamplesSink[] doubleSinks)
             throws java.lang.IllegalArgumentException
Registers a sink, which will receive all new values as they are sampled.

Parameters:
doubleSinks - The time series to fill. This array must have the same number of elements as the number of lines specified to the constructor.
lines - The set of lines on the device to read. Must be an integer array with a minimum length of 1 and a maximum length specified to the constructor, containing the indices of the lines to read. These indices are counted from 0 to maxNumLines-1. E.g. for a parallel port (see ParallelPortPWMReader), this corresponds to data lines DATA0 to DATA7. E.g. for a 3D accelerometer (see WiTiltRawReader), this corresponds to 0=X, 1=Y, 2=Z.
Throws:
java.lang.IllegalArgumentException

addSink

public void addSink(int[] lines,
                    SamplesSink_Int[] intSinks)
             throws java.lang.IllegalArgumentException
Registers a sink, which will receive all new values as they are sampled. This is the integer sinks variant.

Parameters:
intSinks - The time series to fill. This array must have the same number of elements as the number of lines specified to the constructor.
lines - The set of lines on the device to read. Must be an integer array with a minimum length of 1 and a maximum length specified to the constructor, containing the indices of the lines to read. These indices are counted from 0 to maxNumLines-1. E.g. for a parallel port (see ParallelPortPWMReader), this corresponds to data lines DATA0 to DATA7. E.g. for a 3D accelerometer (see WiTiltRawReader), this corresponds to 0=X, 1=Y, 2=Z.
Throws:
java.lang.IllegalArgumentException

removeSink

public boolean removeSink(int[] lines,
                          SamplesSink[] doubleSinks)
Removes a previously registered sink.

Parameters:
doubleSinks - The time series to stop filling.
lines - The set of lines with which this sink has been registered.
Returns:
true if removed, false if not (i.e. if they have not been added previously).
See Also:
addSink(int[], SamplesSink[])

removeSink_Int

public boolean removeSink_Int(int[] lines,
                              SamplesSink_Int[] intSinks)
Removes a previously registered sink. This is the integer sinks variant.

Parameters:
intSinks - The time series to stop filling.
lines - The set of lines with which this sink has been registered.
Returns:
true if removed, false if not (i.e. if they have not been added previously).
See Also:
addSink(int[], SamplesSink[])

start

public void start()
Starts a new background thread to read from the file and create sample values as the lines are read.


stop

public void stop()
Stops the background thread, if started previously.


dispose

public void dispose()
This causes the reader to be shut down properly by calling stop(). #see stop


getMaxNumLines

public int getMaxNumLines()
Returns the maximum number of lines that can be sampled. This depends on the specific sensor implementation.

Returns:
The value of @see maxNumLines.

simulateSampling

public void simulateSampling()
Simulate sampling by reading all available lines from the spcified file.


emitSample

protected void emitSample(double[] sample)
This method should be called by the parseLine method to send samples to all registered listeners. Note: When integer sinks have been registered, the double values will be truncated for sending to these listeners!

Parameters:
sample - The current sample.

emitSample

protected void emitSample(int[] sample)
This method should be called by the parseLine method to send samples to all registered listeners.

Parameters:
sample - The current sample.

handleSample

protected abstract boolean handleSample()
This method is called whenever a sample should be read from the respective source, and it should in turn call emitSample to send the new sample to all registered listeners.

Returns:
true if more samples are available, false otherwise.

getParameters

public abstract TimeSeries.Parameters getParameters()
Each time samples source must be able to provide the appropriate parameters for normalizing its values to the [-1;1] range.


getParameters_Int

public abstract TimeSeries_Int.Parameters getParameters_Int()


2005-2006, Rene Mayrhofer.