aboutsummaryrefslogtreecommitdiffstats
path: root/Java/com/phidgets/WeightSensorPhidget.java
blob: e1c57280583f085c33fd05374db71e8b06ad43cd (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
/*
 * 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 Weight Sensor. All methods
 * to read weight data from the weight sensor are implemented in this class.
 * <p>
 * The Phidget Weight Sensor is simply an electronic scale with a USB interface.
 * It provides one weight value, in kg.
 * 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 Phidget Inc.
 */
public final class WeightSensorPhidget extends Phidget
{
	public WeightSensorPhidget () throws PhidgetException
	{
		super (create ());
	}
	private static native long create () throws PhidgetException;
	/**
	 * Returns the sensed weight in kg. This weight is in kg (kilograms), but can
	 * easily be converted into other units. The accuracy, sesitivity and range depends on the scale and version.
	 *
	 * @return The sensed weight
	 * @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 double getWeight () throws PhidgetException;
	/**
	 * Sets the weight change trigger. This is the ammount by which the sensed weight must
	 * change between WeightChangeEvents. By default this is set to 5.
	 *
	 * @param newVal The new trigger value
	 * @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 setWeightChangeTrigger (double newVal) throws PhidgetException;
	/**
	 * Returns the weight change trigger. This is the ammount by which the sensed weight must
	 * change between WeightChangeEvents. By default this is set to 5.
	 *
	 * @return The trigger value
	 * @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 double getWeightChangeTrigger () throws PhidgetException;

	private final void enableDeviceSpecificEvents (boolean b)
	{
		enableWeightChangeEvents (b && weightChangeListeners.size () > 0);
	}
	/**
	 * Adds a weight change listener. The weight change handler is a method that will be called when the weight 
	 * has changed by at least the {@link #getWeightChangeTrigger() Trigger} that has been set.
	 * <p>
	 * There is no limit on the number of weight change handlers that can be registered for a particular Phidget.
	 * 
	 * @param l An implemetation of the {@link com.phidgets.event.WeightChangeListener WeightChangeListener} interface
	 */
	public final void addWeightChangeListener (WeightChangeListener l)
	{
		synchronized (weightChangeListeners)
		{
			weightChangeListeners.add (l);
			enableWeightChangeEvents (true);
	}} private LinkedList weightChangeListeners = new LinkedList ();
	private long nativeWeightChangeHandler = 0;
	public final void removeWeightChangeListener (WeightChangeListener l)
	{
		synchronized (weightChangeListeners)
		{
			weightChangeListeners.remove (l);
			enableWeightChangeEvents (weightChangeListeners.size () > 0);
	}} private void fireWeightChange (WeightChangeEvent e)
	{
		synchronized (weightChangeListeners)
		{
			for (Iterator it = weightChangeListeners.iterator (); it.hasNext ();)
				((WeightChangeListener) it.next ()).weightChanged (e);
		}
	}
	private native void enableWeightChangeEvents (boolean b);
}