rfid
Class TagReader

java.lang.Object
  extended by java.lang.Thread
      extended by rfid.TagReader
All Implemented Interfaces:
Runnable

public class TagReader
extends Thread

This class is intended to abstract away the hardware interface to the phidget. The sample JNI classes provided by Phidgets.com are not exactly pretty Java code. They're great for C-style, but do not fit into the Java paridigm as well as they should. Instead of re-writing all of these classes, I've shown how you can create a layer of abstraction on top in order to simplify the work needed to do.

This class is used to create TagReader objects that will connect to RFID Tag Reader Phidgets and poll them for tags. They implement a simple Event-based model in order to alert a listener when a tag enters range and when it leaves. This class also provides interface to the onboard digital outputs namely the onboard LED, external LED and external +5v output. In order to catch events, this class creates a producer-consumer relationship on a queue of tag events. The producer ensures that when a tag is present, this class knows. It sets the value of isTagPresent and updates the tag value stored inside this class. The consumer, however, keeps expiring these values. Because of this, we throttle the consumer down to 100ms in order to prevent it from raising enter and exit events very frequently even when the Phidget has not noted a change. The Producer is the only part of this class that actually interfaces with the hardware. The rest is essentially a "model" of how we believe it to behave.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static int CONSUME_DELAY
           
static int LISTENER_DELAY
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TagReader()
          Constructs a new TagReader -- no arguments needed.
 
Method Summary
 void addTagReaderListener(TagReaderListener fred)
           
 String getTag()
          Returns the tag.
 boolean removeTagReaderListener(TagReaderListener fred)
           
 void run()
          This is the event loop that notifies the listeners.
 void setTag(String tag)
          The producer calls this every time a tag is sensed.
 void toggleOnboardLED()
           
 boolean touch()
          The consumer calls this, and it expires tag presence -- setting isTagPresent to false.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONSUME_DELAY

public static final int CONSUME_DELAY
See Also:
Constant Field Values

LISTENER_DELAY

public static final int LISTENER_DELAY
See Also:
Constant Field Values
Constructor Detail

TagReader

public TagReader()
          throws Exception
Constructs a new TagReader -- no arguments needed.

Throws:
Exception - when the producer cannot connect to the hardware.
Method Detail

run

public void run()
This is the event loop that notifies the listeners. Note that this loop is independent from producer/consumer since we don't want to let the listeners know what happens EVERY TIME a tag is read. We also don't want flicker, so these loops should not have the same repeat times.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

addTagReaderListener

public void addTagReaderListener(TagReaderListener fred)

removeTagReaderListener

public boolean removeTagReaderListener(TagReaderListener fred)

toggleOnboardLED

public void toggleOnboardLED()

getTag

public String getTag()
Returns the tag. Synchronized so it's not changing while being read.

Returns:
the tag

touch

public boolean touch()
The consumer calls this, and it expires tag presence -- setting isTagPresent to false.

Returns:
whether or not the tag was present before this touch.

setTag

public void setTag(String tag)
The producer calls this every time a tag is sensed.

Parameters:
tag - the tag that was sensed.