OSDN Git Service

Modify its project name.
[nxt-jsp/etrobo-atk.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / Poll.java
1 /**
2  * RCX access classes.
3  */
4 package lejos.nxt;
5
6 /**
7  * Provides blocking access to events from the NXT. Poll is a bit
8  * of a misnomer (since you don't 'poll' at all) but it takes its
9  * name from the Unix call of the same name.
10  */
11 public class Poll
12 {
13     public static final short SENSOR1_MASK = 0x01;
14     public static final short SENSOR2_MASK = 0x02;
15     public static final short SENSOR3_MASK = 0x04;
16     public static final short SENSOR4_MASK = 0x08;
17     
18     public static final short ALL_SENSORS  = 0x0F;
19
20     public static final short ENTER_MASK    = 0x10;
21     public static final short LEFT_MASK    = 0x20;
22     public static final short RIGHT_MASK    = 0x40;
23     public static final short ESCAPE_MASK    = 0x80;
24     public static final short ALL_BUTTONS  = 0xF0;
25     public static final short BUTTON_MASK_SHIFT  = 4;
26
27     public static final short SERIAL_MASK = 0x100;
28     public static final short SERIAL_SHIFT = 8;
29
30     private static Poll monitor = new Poll(true);
31     
32     // This is reflected in the kernel structure  
33     private short changed;  // The 'changed' mask.
34
35     /**
36      * Private constructor. Sets up the poller in the kernel.
37      */ 
38     private Poll(boolean dummy) {
39         setPoller();
40     }
41     
42     /**
43      * Constructor.
44      */ 
45     public Poll() {
46     }
47     
48     /**
49      * Wait for the sensor/button values to change then return.
50      *
51      * @param mask bit mask of values to monitor.
52      * @param millis wait for at most millis milliseconds. 0 = forever.
53      * @return a bit mask of the values that have changed
54      * @throws InterruptedException
55      */
56     public final int poll(int mask, int millis) throws InterruptedException {
57         synchronized (monitor) {
58             int ret = mask & monitor.changed;
59             
60             // The inputs we're interested in may have already changed
61             // since we last looked so check before we wait.
62             while (ret == 0) {
63                 monitor.wait(millis);
64                 
65                 // Work out what's changed that we're interested in.
66                 ret = mask & monitor.changed;
67             }
68
69             // Clear the bits that we're monitoring. If anyone else
70             // is also monitoring these bits its tough.
71             monitor.changed &= ~mask;
72             return ret;
73         }
74     }
75
76     /**
77      * Set a throttle on the regularity with which inputs
78      * are polled.
79      * @param throttle number of sensor reads between polls.
80      * Default value is 1. 0 means poll as often as possible.
81      * Sensor reads occur every 3ms.
82      */
83     public native final void setThrottle(int throttle);
84
85
86     /**
87      * Sets up and starts the the poller in the kernel.
88      */
89     private native final void setPoller();
90 /*
91     {
92         new Thread() {
93             public void run() {
94                 do {
95                     synchronized (monitor) {
96                         if (anthing has changed) {
97                             monitor.changed = whatever has changed
98                             notifyAll();
99                         }
100                     }
101                 } while (true);
102             }
103         }.start();
104     }
105 */
106 }