aboutsummaryrefslogtreecommitdiffstats
path: root/Java/com/phidgets/GPSPhidget.java
blob: 4e6e27483e77de06dcf1f7f1b5a9fd267b5ba1f3 (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
/*
 * Copyright 2012 Phidgets Inc.  All rights reserved.
 */

package com.phidgets;
import java.util.Iterator;
import java.util.LinkedList;
import com.phidgets.event.*;
import java.util.Calendar;
/**
* This class represents a Phidget GPS. All methods
* to manipulate the Phidget GPS are implemented in this class.
* 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.
* <p>
* 
* @author Phidgets Inc.
*/
public final class GPSPhidget extends Phidget
{
	public GPSPhidget () throws PhidgetException
	{
		super (create ());
	}
	private static native long create () throws PhidgetException;

	/**
	 * Returns the current latitude, in signed degrees format. 
	 * @return Current latitude,  in signed degrees format.
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board. 
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If the latitude is not available, an EPHIDGET_UNKNOWNVAL exception is thrown.
	 */
	public native double getLatitude () throws PhidgetException;
	/**
	 * Returns the current longitude, in signed degrees format. 
	 * @return Current longitude,  in signed degrees format.
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If the longitude is not available, an EPHIDGET_UNKNOWNVAL exception is thrown.
	 */
	public native double getLongitude () throws PhidgetException;
	/**
	 * Returns the current altitude, in meters. 
	 * @return Current altitude, in meters.
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If the altitude is not available, an EPHIDGET_UNKNOWNVAL exception is thrown.
	 */
	public native double getAltitude () throws PhidgetException;
	/**
	 * Returns the current heading, in degrees - compass bearing format. Heading is only accurate if the GPS is moving, and it represents 
	 * a heading over time, and not the actual direction the PhidgetGPS is pointing.
     * exception is thrown.
	 * @return Heading in degrees(compass bearing format).
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If the heading is not available, an EPHIDGET_UNKNOWNVAL.
	 */
	public native double getHeading () throws PhidgetException;
	/**
	 * Returns the current velocity, in km/h. Velocity is only accurate if the PhidgetGPS is moving. 
	 * @return Velocity in km/h.
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If the velocity is not available, an EPHIDGET_UNKNOWNVAL exception is thrown.
	 */
	public native double getVelocity () throws PhidgetException;
	/**
	 * Returns the current GPS date and time, in UTC. The time is updated 10 times a second and is accurate to within at least 500ms when {@link #getPositionFixStatus getPositionFixStatus} is true.
	 * @return Current GPS date and time.
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If the date or time are not available, an EPHIDGET_UNKNOWNVAL exception is thrown. 
	 */
	public native Calendar getDateAndTime () throws PhidgetException;
	/**
	 * Returns the current position fix status. If true, all of the above properties will be available. Time and date may or may not be available, but they can only be trusted
	 * as accurate when the PositionFixStatus is true. 
	 * @return Current position fix status.
	 * @throws PhidgetException If this Phidget is not opened and attached, or if the GPS is not plugged into the board.
	 * See {@link com.phidgets.Phidget#open(int) open} for information on determining if a device is attached.
	 * If false, the {@link #getHeading getHeading} / {@link #getVelocity getVelocity} will throw an EPHIDGET_UNKNOWN exception. 
	 */
	public native boolean getPositionFixStatus () throws PhidgetException;

	private final void enableDeviceSpecificEvents (boolean b)
	{
		enableGPSPositionFixStatusChangeEvents (b && gpsPositionFixStatusChangeListeners.size () > 0);
		enableGPSPositionChangeEvents (b && gpsPositionChangeListeners.size () > 0);
	}
	/**
	 * Adds a position fix status change listener. The position fix status change handler is a method that will be called when the position fix status changes.
	 * <p>
	 * There is no limit on the number of position fix status change handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.GPSPositionFixStatusChangeListener GPSPositionFixStatusChangeListener} interface
	 */
	public final void addGPSPositionFixStatusChangeListener (GPSPositionFixStatusChangeListener l)
	{
		synchronized (gpsPositionFixStatusChangeListeners)
		{
			gpsPositionFixStatusChangeListeners.add (l);
			enableGPSPositionFixStatusChangeEvents (true);
	}} private LinkedList gpsPositionFixStatusChangeListeners = new LinkedList ();
	private long nativeGPSPositionFixStatusChangeHandler = 0;
	public final void removeGPSPositionFixStatusChangeListener (GPSPositionFixStatusChangeListener l)
	{
		synchronized (gpsPositionFixStatusChangeListeners)
		{
			gpsPositionFixStatusChangeListeners.remove (l);
			enableGPSPositionFixStatusChangeEvents (gpsPositionFixStatusChangeListeners.size () > 0);
	}} private void fireGPSPositionFixStatusChange (GPSPositionFixStatusChangeEvent e)
	{
		synchronized (gpsPositionFixStatusChangeListeners)
		{
			for (Iterator it = gpsPositionFixStatusChangeListeners.iterator (); it.hasNext ();)
				((GPSPositionFixStatusChangeListener) it.next ()).gpsPositionFixStatusChanged (e);
		}
	}
	private native void enableGPSPositionFixStatusChangeEvents (boolean b);
	/**
	 * Adds a position change listener. The position change handler is a method that will be called when the position changes.
	 * <p>
	 * There is no limit on the number of position change handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.GPSPositionChangeListener GPSPositionChangeListener} interface
	 */
	public final void addGPSPositionChangeListener (GPSPositionChangeListener l)
	{
		synchronized (gpsPositionChangeListeners)
		{
			gpsPositionChangeListeners.add (l);
			enableGPSPositionChangeEvents (true);
	}} private LinkedList gpsPositionChangeListeners = new LinkedList ();
	private long nativeGPSPositionChangeHandler = 0;
	public final void removeGPSPositionChangeListener (GPSPositionChangeListener l)
	{
		synchronized (gpsPositionChangeListeners)
		{
			gpsPositionChangeListeners.remove (l);
			enableGPSPositionChangeEvents (gpsPositionChangeListeners.size () > 0);
	}} private void fireGPSPositionChange (GPSPositionChangeEvent e)
	{
		synchronized (gpsPositionChangeListeners)
		{
			for (Iterator it = gpsPositionChangeListeners.iterator (); it.hasNext ();)
				((GPSPositionChangeListener) it.next ()).gpsPositionChanged (e);
		}
	}
	private native void enableGPSPositionChangeEvents (boolean b);
}