aboutsummaryrefslogtreecommitdiffstats
path: root/Java/com/phidgets/FrequencyCounterPhidget.java
blob: b6b138ef6f1de006a9447ce9d96b2063683f623e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright 2011 Phidgets Inc.  All rights reserved.
 */

package com.phidgets;
import java.util.Iterator;
import java.util.LinkedList;
import com.phidgets.event.*;
/**
 * This class represents a Phidget Frequency Counter. All methods
 * to read manipulate the Phidget Frequency Counter are implemented in this class.
 * <p>
 * 
 * @author Phidgets Inc.
 */
public final class FrequencyCounterPhidget extends Phidget
{
	public FrequencyCounterPhidget () throws PhidgetException
	{
		super (create ());
	}
	private static native long create () throws PhidgetException;
	/**
	 * Zero-Crossing Filter Type. This is used with {@link #getFilter(int) getFilter} and {@link #setFilter(int, int) setFilter}
	 */
	public static final int PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING = 1;
	/**
	 * Logic-Level Filter Type. This is used with {@link #getFilter(int) getFilter} and {@link #setFilter(int, int) setFilter}
	 */
	public static final int PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL = 2;
	/**
	 * Unknown Filter Type. This is used with {@link #getFilter(int) getFilter} and {@link #setFilter(int, int) setFilter}
	 */
	public static final int PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN = 3;
	/**
	 * Returns the number of channels.
	 * @return Number of inputs
	 */
	public native int getFrequencyInputCount () throws PhidgetException;
	/**
	 * Returns the last calcualted frequency on the specified channel, in Hz. This function will return 0 if the {@link #getTimeout(int) getTimeout} value elapses without detecting a signal.
	 * Frequency is recalculated up to 31.25 times a second, depending on the pulse rate.  
	 * @param index Index of the channel
	 * @return frequency of the channel
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is out of range. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native double getFrequency (int index) throws PhidgetException;
	/**
	 * Returns the total number of pulses detected on the specified channel since the Phidget was opened, or since the last reset.
	 * @param index Index of the channel
	 * @return total number of pulses
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is out of range. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native long getTotalCount (int index) throws PhidgetException;
	/**
	 * Returns the total elapsed tiem since Phidget was opened, or since the last reset, in microseconds. This time corresponds to the {@link #getTotalCount(int) getTotalCount} property.
	 * @param index Index of the channel
	 * @return total time, in microseconds
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is out of range. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native long getTotalTime (int index) throws PhidgetException;
	/**
	 * Returns the timeout value, in microseconds. This value is used to set the time to wait without detecting a signal before reporting 0 Hz. The valid range is 0.1 - 100 seconds(100,000 - 100,000,000 microseconds).
	 * 0.1 timeout represents the lowest frequency that will be measurable.
	 * @param index Index of the channel
	 * @return timeout value
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is out of range. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native int getTimeout (int index) throws PhidgetException;
	/**
	* Sets the timeout value, in microseconds. This value is used to set the time to wait without detecting a signal before reporting 0 Hz. The valid range is 0.1 - 100 seconds(100,000 - 100,000,000 microseconds).
	 * 0.1 timeout represents the lowest frequency that will be measurable.
	 * @param index Index of the channel
	 * @param timeout new timeout value
	 * @throws PhidgetException If this Phidget is not opened and attached, the index is out of range or the the timeout value is out of range. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native void setTimeout (int index, int timeout) throws PhidgetException;
	/**
	 * Returns the enabled state on the specified channel. When the channel is disabled, it will no longer register counts. TotalTime and TotalCount properties will not be incremented until
	 * the channel is re-enabled.
	 * @param index Index of the channel
	 * @return state
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is invalid. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native boolean getEnabled (int index) throws PhidgetException;
	/**
	 * Gets the enabled state on the specified channel. When the channel is disabled, it will no longer register counts. TotalTime and TotalCount properties will not be incremented until
	 * the channel is re-enabled.
	 * @param index Index of the channel
	 * @param state new enabled state
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is invalid. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native void setEnabled (int index, boolean state) throws PhidgetException;
	/**
	 * Gets the channel filter mode. This controls the type of signal that the Frequency Counter will respond to - either a zero-centered or a logic-level signal.
	 * <p>
	 * The possible values for type are {@link #PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING},
	 * {@link #PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL}, {@link #PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN}
	 * <p>
	 * @param index Index of the channel
	 * @return filter type
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is invalid. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native int getFilter (int index) throws PhidgetException;
	/**
	 * Sets the channel filter mode. This controls the type of signal that the Frequency Counter will respond to - either a zero-centered or a logic-level signal.
	 * <p>
	 * The possible values for type are {@link #PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING},
	 * {@link #PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL}, {@link #PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN}
	 * <p>
	 * @param index Index of the channel
	 * @param type new filter type
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is invalid. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native void setFilter (int index, int type) throws PhidgetException;
	/**
	 * Resets the {@link #getTotalCount(int) getTotalCount} and {@link #getTotalTime(int) getTotalTime} counters to 0 for the specified channel. For best performance, this should be called when the channel is disabled.
	 * @param index Index of the channel
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the index is invalid. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native void reset (int index) throws PhidgetException;

	private final void enableDeviceSpecificEvents (boolean b)
	{
		enableFrequencyCounterCountEvents (b && frequencyCounterCountListeners.size () > 0);
	}
	/**
	 * Adds a count listener. The count handler is a method that will be called whenever some counts have been detected.
	 * This event will fire up to 31.25 times a second, depending on the pulse rate. The time is in microseconds and represents the amount
	 * of time in which the number of counts occured. This event can be used to calculate frequency independently of the phidget21 library frequency implementation.
	 * This event will fire with a count of 0 once, after the Timeout time has elapsed with no counts for a channel, to indicate 0 Hz.
	 * <p>
	 * There is no limit on the number of count handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.FrequencyCounterCountListener FrequencyCounterCountListener} interface
	 */
	public final void addFrequencyCounterCountListener (FrequencyCounterCountListener l)
	{
		synchronized (frequencyCounterCountListeners)
		{
			frequencyCounterCountListeners.add (l);
			enableFrequencyCounterCountEvents (true);
	}} private LinkedList frequencyCounterCountListeners = new LinkedList ();
	private long nativeFrequencyCounterCountHandler = 0;
	public final void removeFrequencyCounterCountListener (FrequencyCounterCountListener l)
	{
		synchronized (frequencyCounterCountListeners)
		{
			frequencyCounterCountListeners.remove (l);
			enableFrequencyCounterCountEvents (frequencyCounterCountListeners.size () > 0);
	}} private void fireFrequencyCounterCount (FrequencyCounterCountEvent e)
	{
		synchronized (frequencyCounterCountListeners)
		{
			for (Iterator it = frequencyCounterCountListeners.iterator (); it.hasNext ();)
				((FrequencyCounterCountListener) it.next ()).frequencyCounterCounted (e);
		}
	}
	private native void enableFrequencyCounterCountEvents (boolean b);
}