aboutsummaryrefslogtreecommitdiffstats
path: root/Java/com/phidgets/InterfaceKitPhidget.java
blob: 6da48db78f0115d3a0c3159d8e00c27be1989617 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright 2012 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 Interface Kit. All methods
 * to read and write data to and from an Interface Kit are implemented in this class.
 * <p>
 * There are many types of Interface Kits, but each is simply a collection of 0 or more 
 * digital inputs, digital outpus and analog sensor inputs. The inputs can be read and outputs can be set,
 * and event handlers can be set for each of these.
 * <p>
 * See your device's User Guide for more specific API details, 
 * technical information, and revision details. The User Guide, along with other resources, can be found on 
 * the product page for your device.
 * 
 * @author Phidgets Inc.
 */
public final class InterfaceKitPhidget extends Phidget
{
	/**
	 * Class Constructor. Calling this opens a connection to the phidget21 C library creates
	 * an internal handle for this Phidget, ready to call open on.
	 * 
	 * @throws PhidgetException If there was a problem connecting to phidget21 or creating 
	 * the internal handle.
	 */
	public InterfaceKitPhidget() throws PhidgetException {
		super(create());
	}

	private static native long create() throws PhidgetException;

	/**
	 * Returns the number of digital outputs on this Interface Kit. Not all interface kits
	 * have the same number of digital outputs, and some don't have any digital outputs at all.
	 * 
	 * @return Number of digital outputs
	 * @throws PhidgetException If this Phidget is not opened and attached. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native int getOutputCount() throws PhidgetException;

	/**
	 * Returns the number of ditigal inputs on this Interface Kit. Not all interface kits
	 * have the same number of digital inputs, and some don't have any digital inputs at all.
	 * 
	 * @return Number of digital inputs
	 * @throws PhidgetException If this Phidget is not opened and attached. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native int getInputCount() throws PhidgetException;

	/**
	 * Returns the number of analog inputs on the Interface Kit. Not all interface kits
	 * have the same number of analog inputs, and some don't have any analog inputs at all.
	 * 
	 * @return Number of analog inputs
	 * @throws PhidgetException If this Phidget is not opened and attached. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native int getSensorCount() throws PhidgetException;

	/**
	 * Returns the minimum data rate for a sensor, in milliseconds.
	 * 
	 * @param index Index of the input
	 * @return minimum data rate
	 * @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 getDataRateMin(int index) throws PhidgetException;

	/**
	 * Returns the maximum data rate for a sensor, in milliseconds.
	 * 
	 * @param index Index of the input
	 * @return maximum data rate
	 * @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 getDataRateMax(int index) throws PhidgetException;

	/**
	 * Returns the state of a digital input. Digital inputs read True where they are activated and false
	 * when they are in their default state.
	 * <p>
	 * Be sure to check {@link #getInputCount() getInputCount} first if you are unsure as to the number of inputs, so as not to
	 * set an Index that is out of range.
	 * 
	 * @param index Index of the input
	 * @return State of the input
	 * @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 boolean getInputState(int index) throws PhidgetException;

	/**
	 * Returns the state of a digital output. Depending on the Phidget, this value may be either the value that you last
	 * wrote out to the Phidget, or the value that the Phidget last returned. This is because some Phidgets return their output state
	 * and others do not. This means that with some devices, reading the output
	 * state of a pin directly after setting it, may not return the value that you just set.
	 * <p>
	 * Be sure to check {@link #getOutputCount() getOutputCount} first if you are unsure as to the number of outputs, so as not to
	 * attempt to get an Index that is out of range.
	 * 
	 * @param index Index of the output
	 * @return State of the output
	 * @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 boolean getOutputState(int index) throws PhidgetException;

	/**
	 * Returns the value of a analog input.
	 * The analog inputs are where analog sensors are attached on the InterfaceKit 8/8/8. 
	 * On the Linear and Circular touch sensor Phidgets, analog input 0 represents position
	 * on the slider.
	 * <p>
	 * The valid range is 0-1000. In the case of a sensor, this value can be converted to an actual
	 * sensor value using the formulas provided here: http://www.phidgets.com/documentation/Sensors.pdf
	 * 
	 * @param index Index of the sensor
	 * @return Sensor 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 getSensorValue(int index) throws PhidgetException;

	/**
	 * Returns the raw value of a analog input. This is a more accurate version of {@link #getSensorValue(int) getSensorValue}.
	 * The valid range is 0-4095. Note however that the analog outputs on the Interface Kit 8/8/8 are only 10-bit values
	 * and this value represents an oversampling to 12-bit.
	 * 
	 * @param index Index of the sensor
	 * @return Sensor 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 getSensorRawValue(int index) throws PhidgetException;

	/**
	 * Returns the change trigger for an analog input. This is the ammount that an inputs must change
	 * between successive SensorChangeEvents. This is based on the 0-1000 range provided by getSensorValue.
	 * This value is by default set to 10 for most Interface Kits with analog inputs.
	 * 
	 * @param index Index of the sensor
	 * @return Trigger 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 getSensorChangeTrigger(int index)
	  throws PhidgetException;

	/**
	 * Returns the data rate for a sensor, in milliseconds.
	 * 
	 * @param index Index of the sensor
	 * @return data rate
	 * @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 getDataRate(int index)
	  throws PhidgetException;

	/**
	 * Gets the ratiometric state for the analog sensors
	 * 
	 * @return state State
	 * @throws PhidgetException If this Phidget is not opened and attached, or if this phidget does not support ratiometric.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native boolean getRatiometric()
	  throws PhidgetException;

	/**
	 * Sets the state of a digital output. Setting this to true will activate the output, False is the default state.
	 * 
	 * @param index Index of the output
	 * @param newVal State to set the output to
	 * @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 void setOutputState(int index, boolean newVal)
	  throws PhidgetException;

	/**
	 * Sets the change trigger for an analog input. This is the ammount that an inputs must change
	 * between successive SensorChangeEvents. This is based on the 0-1000 range provided by getSensorValue.
	 * This value is by default set to 10 for most Interface Kits with analog inputs.
	 * 
	 * @param index Input
	 * @param newVal 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 void setSensorChangeTrigger(int index, int newVal)
	  throws PhidgetException;

	/**
	 * Sets the data rate of a sensor, in milliseconds.
	 * 
	 * @param index Input
	 * @param newVal data rate
	 * @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 void setDataRate(int index, int newVal)
	  throws PhidgetException;

	/**
	 * Sets the ratiometric state for the analog inputs. The default is for ratiometric to be set on and this
	 * is appropriate for most sensors.
	 * 
	 * @param state State
	 * @throws PhidgetException If this Phidget is not opened and attached, or if this Phidget does not support ratiometric
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 */
	public native void setRatiometric(boolean state)
	  throws PhidgetException;

	private final void enableDeviceSpecificEvents(boolean b) {
		enableInputChangeEvents(b && inputChangeListeners.size() > 0);
		enableOutputChangeEvents(b && outputChangeListeners.size() > 0);
		enableSensorChangeEvents(b && sensorChangeListeners.size() > 0);
	}

	private LinkedList inputChangeListeners = new LinkedList();
	private long nativeInputChangeHandler = 0;

	/**
	 * Adds an input change listener. The input change handler is a method that will be called when an input on this
	 * Interface Kit has changed.
	 * <p>
	 * There is no limit on the number of input change handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.InputChangeListener InputChangeListener} interface
	 */
	public final void addInputChangeListener(InputChangeListener l) {
		synchronized (inputChangeListeners) {
			inputChangeListeners.add(l);
			enableInputChangeEvents(true);
		}
	}

	/**
	 * Removes an input change listener. This will remove a previously added input change listener.
	 */
	public final void removeInputChangeListener(InputChangeListener l) {
		synchronized (inputChangeListeners) {
			inputChangeListeners.remove(l);
			enableInputChangeEvents(inputChangeListeners.size() >
			  0);
		}
	}
	private void fireInputChange(InputChangeEvent e) {
		synchronized (inputChangeListeners) {
			for (Iterator it = inputChangeListeners.iterator();
			  it.hasNext(); )
				((InputChangeListener)it.next()).inputChanged(
				  e);
		}
	}
	private native void enableInputChangeEvents(boolean b);

	private LinkedList outputChangeListeners = new LinkedList();
	private long nativeOutputChangeHandler = 0;

	/**
	 * Adds an output change listener. The output change handler is a method that will be called when an output on this
	 * Interface Kit has changed.
	 * <p>
	 * There is no limit on the number of output change handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.OutputChangeListener OutputChangeListener} interface
	 */
	public final void addOutputChangeListener(OutputChangeListener l) {
		synchronized (outputChangeListeners) {
			outputChangeListeners.add(l);
			enableOutputChangeEvents(true);
		}
	}

	/**
	 * Removes an output change listener. This will remove a previously added output change listener.
	 */
	public final void removeOutputChangeListener(OutputChangeListener l) {
		synchronized (outputChangeListeners) {
			outputChangeListeners.remove(l);
			enableOutputChangeEvents(outputChangeListeners.size()
			  > 0);
		}
	}
	private void fireOutputChange(OutputChangeEvent e) {
		synchronized (outputChangeListeners) {
			for (Iterator it = outputChangeListeners.iterator();
			  it.hasNext(); )
				((OutputChangeListener)it.next())
				  .outputChanged(e);
		}
	}
	private native void enableOutputChangeEvents(boolean b);

	private LinkedList sensorChangeListeners = new LinkedList();
	private long nativeSensorChangeHandler = 0;

	/**
	 * Adds a sensor change listener. The sensor change handler is a method that will be called when a sensor on this
	 * Interface Kit has changed by at least the {@link #getSensorChangeTrigger(int) Trigger} that has been set for this sensor.
	 * <p>
	 * There is no limit on the number of sensor change handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.SensorChangeListener SensorChangeListener} interface
	 */
	public final void addSensorChangeListener(SensorChangeListener l) {
		synchronized (sensorChangeListeners) {
			sensorChangeListeners.add(l);
			enableSensorChangeEvents(true);
		}
	}

	/**
	 * Removes a sensor change listener. This will remove a previously added sensor change listener.
	 */
	public final void removeSensorChangeListener(SensorChangeListener l) {
		synchronized (sensorChangeListeners) {
			sensorChangeListeners.remove(l);
			enableSensorChangeEvents(sensorChangeListeners.size()
			  > 0);
		}
	}
	private void fireSensorChange(SensorChangeEvent e) {
		synchronized (sensorChangeListeners) {
			for (Iterator it = sensorChangeListeners.iterator();
			  it.hasNext(); )
				((SensorChangeListener)it.next())
				  .sensorChanged(e);
		}
	}
	private native void enableSensorChangeEvents(boolean b);
}