OSDN Git Service

1d76e49b5702ebe8f0fd3e2ed8bce615956b9cda
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / LightSensor.java
1 package lejos.nxt;\r
2 \r
3 /**\r
4  * Abstraction for a NXT light sensor.\r
5  * The light sensor can be calibrated to low and high values. \r
6  */\r
7 public class LightSensor implements SensorConstants\r
8 {\r
9         ADSensorPort port;\r
10         private int _zero = 1023;\r
11         private int _hundred = 0;\r
12         \r
13         /**\r
14          * Create a light sensor object attached to the specified port.\r
15          * The sensor will be set to floodlit mode, i.e. the LED will be turned on.\r
16          * @param port port, e.g. Port.S1\r
17          */\r
18         public LightSensor(ADSensorPort port)\r
19         {\r
20                 this.port = port;\r
21                 port.setTypeAndMode(TYPE_LIGHT_ACTIVE,\r
22                             MODE_PCTFULLSCALE);\r
23         }\r
24         \r
25         /**\r
26          * Create a light sensor object attached to the specified port,\r
27          * and sets floodlighting on or off.\r
28          * @param port port, e.g. Port.S1\r
29          * @param floodlight true to set floodit mode, false for ambient light.\r
30          */\r
31         public LightSensor(ADSensorPort port, boolean floodlight)\r
32         {\r
33            this.port = port;\r
34        port.setTypeAndMode(\r
35                    (floodlight ? TYPE_LIGHT_ACTIVE\r
36                                        : TYPE_LIGHT_INACTIVE),\r
37                    MODE_PCTFULLSCALE);\r
38                         \r
39            \r
40         }\r
41         \r
42         /**\r
43          * Set floodlighting on or off.\r
44          * @param floodlight true to set floodit mode, false for ambient light.\r
45          */\r
46         public void setFloodlight(boolean floodlight)\r
47         {\r
48                 port.setType((floodlight ? TYPE_LIGHT_ACTIVE\r
49                                          : TYPE_LIGHT_INACTIVE));\r
50         }\r
51 \r
52         /**\r
53          * Read the current sensor value.\r
54          * Use calibrateLow() to set the zero level, and calibrateHigh to set the 100 level.\r
55          * @return Value as a percentage of difference between the low and high calibration values. \r
56          */\r
57         public int readValue()\r
58         {\r
59 //              return ((1023 - port.readRawValue()) * 100/ 1023); \r
60                 if(_hundred == _zero)return 1023;\r
61                 return 100*(port.readRawValue() - _zero)/(_hundred - _zero); \r
62         }\r
63         \r
64         /**\r
65          * Read the current sensor normalized value. Allows more accuracy\r
66          * than readValue(). For LEGO sensor, values typically range from \r
67          * 145 (dark) to 890 (sunlight). \r
68          * @return Value as raw normalized (0 to 1023)\r
69          */\r
70         public int readNormalizedValue() {\r
71                 return 1023 - port.readRawValue();\r
72         }\r
73 \r
74 /**\r
75  * call this method when the light sensor is reading the low value - used by readValue\r
76  **/\r
77         public void calibrateLow()\r
78         {\r
79                 _zero = port.readRawValue();\r
80         }\r
81 /** \r
82  *call this method whtn the light sensor is reading the high value - used by reaeValue\r
83  */     \r
84         public void calibrateHigh()\r
85         {\r
86                 _hundred = port.readRawValue();\r
87         }\r
88 }\r