aboutsummaryrefslogtreecommitdiffstats
path: root/Java/com/phidgets/IRPhidget.java
blob: 3a11f706269814164ea79886605b42e483f4d542 (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
/*
 * Copyright 2010 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 IR. All methods
 * to send and receive IR data are implemented in this class.
 * <p>
 * The Phidget IR Receiver-Transmitter can send and receive Consumer-IR signals. Ability to learn and re-transmit codes, 
 * as well as low-level access to raw data, is provided.
 * 
 * @author Phidgets Inc.
 */
public final class IRPhidget extends Phidget
{
	public IRPhidget () throws PhidgetException
	{
		super (create ());
	}
	private static native long create () throws PhidgetException;

	/**
	 * Represents a long space (greater then 327,670 microseconds) in raw data. 
	 * This can be considered a period of no IR activity. This is used with {@link #readRaw readRaw}
	 */
	public static final int RAWDATA_LONGSPACE = 0x7fffffff;

	/**
	 * Transmits a code.
	 * @param code the code to transmit
	 * @param codeInfo the code specification
	 * @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 void transmit (IRCode code, IRCodeInfo codeInfo) throws PhidgetException;
	/**
	 * Transmits a repeat. This needs to be called within the gap time of a transmit to be meaningful.
	 * @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 void transmitRepeat () throws PhidgetException;
	/**
	 * Transmits raw data.
	 * @param data data in microseconds, must start and end with a pulse
	 * @param offset offset in the data array to start transmitting
	 * @param count number of elements of data to transmit
	 * @param gap gap size in microseconds
	 * @param carrierFrequency carrier frequency in kHz
	 * @param dutyCycle duty cycle in percent
	 * @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 void transmitRaw (int[]data, int offset, int count, int gap, int carrierFrequency, int dutyCycle) throws PhidgetException;
	/**
	 * Transmits raw data.
	 * @param data data in microseconds, must start and end with a pulse
	 * @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 final void transmitRaw (int[]data) throws PhidgetException
	{
		transmitRaw (data, 0, data.length, 0, 0, 0);
	}
	/**
	 * Transmits raw data.
	 * @param data data in microseconds, must start and end with a pulse
	 * @param gap gap size in microseconds
	 * @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 final void transmitRaw (int[]data, int gap) throws PhidgetException
	{
		transmitRaw (data, 0, data.length, gap, 0, 0);
	}
	/**
	 * Transmits raw data.
	 * @param data data in microseconds, must start and end with a pulse
	 * @param offset offset in the data array to start transmitting
	 * @param count number of elements of data to transmit
	 * @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 final void transmitRaw (int[]data, int offset, int count) throws PhidgetException
	{
		transmitRaw (data, offset, count, 0, 0, 0);
	}
	/**
	 * Transmits raw data.
	 * @param data data in microseconds, must start and end with a pulse
	 * @param offset offset in the data array to start transmitting
	 * @param count number of elements of data to transmit
	 * @param gap gap size in microseconds
	 * @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 final void transmitRaw (int[]data, int offset, int count, int gap) throws PhidgetException
	{
		transmitRaw (data, offset, count, gap, 0, 0);
	}
	/**
	 * Reads raw data. Use {@link #RAWDATA_LONGSPACE RAWDATA_LONGSPACE} to detect gaps in IR data.
	 * @param buffer array into which data will be read.
	 * @param offset offset in data to start writing
	 * @param count maximum ammount of data to read
	 * @return ammount of data read
	 * @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 readRaw (int[]buffer, int offset, int count) throws PhidgetException;
	/**
	 * Reads raw data. Use {@link #RAWDATA_LONGSPACE RAWDATA_LONGSPACE} to detect gaps in IR data.
	 * @param buffer array into which data will be read.
	 * @return ammount of data read
	 * @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 final int readRaw (int[]buffer) throws PhidgetException
	{
		return readRaw (buffer, 0, buffer.length);
	}
	/**
	 * Returns the last received code. This is updated right after the code event returns.
	 * @return last code
	 * @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 IRCode getLastCode () throws PhidgetException;
	/**
	 * Returns the last learned code. This is updated right after the learn event returns.
	 * @return last learned code
	 * @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 IRLearnedCode getLastLearnedCode () throws PhidgetException;

	private final void enableDeviceSpecificEvents (boolean b)
	{
		enableCodeEvents (b && codeListeners.size () > 0);
		enableLearnEvents (b && learnListeners.size () > 0);
		enableRawDataEvents (b && rawDataListeners.size () > 0);
	}
	/**
	 * Adds a code listener. The code handler is a method that will be called when a new code is
	 seen by the reader. The event is fired on each code, including repetitions.
	 * <p>
	 * There is no limit on the number of code handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.CodeListener CodeListener} interface
	 */
	public final void addCodeListener (CodeListener l)
	{
		synchronized (codeListeners)
		{
			codeListeners.add (l);
			enableCodeEvents (true);
	}} private LinkedList codeListeners = new LinkedList ();
	private long nativeCodeHandler = 0;
	public final void removeCodeListener (CodeListener l)
	{
		synchronized (codeListeners)
		{
			codeListeners.remove (l);
			enableCodeEvents (codeListeners.size () > 0);
	}} private void fireCode (CodeEvent e)
	{
		synchronized (codeListeners)
		{
			for (Iterator it = codeListeners.iterator (); it.hasNext ();)
				((CodeListener) it.next ()).code (e);
		}
	}
	private native void enableCodeEvents (boolean b);
	/**
	 * Adds a code learn listener. The learn handler is a method that will be called when a new code is
	 learned by the reader. This requires that the code be repeated several times.
	 * <p>
	 * There is no limit on the number of learn handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.LearnListener LearnListener} interface
	 */
	public final void addLearnListener (LearnListener l)
	{
		synchronized (learnListeners)
		{
			learnListeners.add (l);
			enableLearnEvents (true);
	}} private LinkedList learnListeners = new LinkedList ();
	private long nativeLearnHandler = 0;
	public final void removeLearnListener (LearnListener l)
	{
		synchronized (learnListeners)
		{
			learnListeners.remove (l);
			enableLearnEvents (learnListeners.size () > 0);
	}} private void fireLearn (LearnEvent e)
	{
		synchronized (learnListeners)
		{
			for (Iterator it = learnListeners.iterator (); it.hasNext ();)
				((LearnListener) it.next ()).learn (e);
		}
	}
	private native void enableLearnEvents (boolean b);
	/**
	 * Adds a rawData listener. The rawData handler is a method that will be called when a raw IR data is received.
	 * <p>
	 * There is no limit on the number of rawData handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.RawDataListener RawDataListener} interface
	 */
	public final void addRawDataListener (RawDataListener l)
	{
		synchronized (rawDataListeners)
		{
			rawDataListeners.add (l);
			enableRawDataEvents (true);
	}} private LinkedList rawDataListeners = new LinkedList ();
	private long nativeRawDataHandler = 0;
	public final void removeRawDataListener (RawDataListener l)
	{
		synchronized (rawDataListeners)
		{
			rawDataListeners.remove (l);
			enableRawDataEvents (rawDataListeners.size () > 0);
	}} private void fireRawData (RawDataEvent e)
	{
		synchronized (rawDataListeners)
		{
			for (Iterator it = rawDataListeners.iterator (); it.hasNext ();)
				((RawDataListener) it.next ()).rawData (e);
		}
	}
	private native void enableRawDataEvents (boolean b);
}