OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / remote / RemoteSensorPort.java
1 package lejos.nxt.remote;\r
2 \r
3 import lejos.nxt.*;\r
4 import lejos.nxt.comm.*;\r
5 import java.io.*;\r
6 \r
7 /**\r
8  * Emulates a Sensor Port using LCP\r
9  */\r
10 public class RemoteSensorPort implements NXTProtocol, ADSensorPort {\r
11         \r
12         private int id;\r
13         private int type, mode;\r
14         private NXTCommand nxtCommand;\r
15 \r
16         public RemoteSensorPort(NXTCommand nxtCommand, int id) {\r
17                 this.nxtCommand = nxtCommand;\r
18                 this.id = id;\r
19         }\r
20         \r
21         public int getId() {\r
22                 return id;\r
23         }\r
24         \r
25         public int getType() {\r
26                 return type;\r
27         }\r
28         \r
29         public int getMode() {\r
30                 return mode;\r
31         }\r
32         \r
33         public void setTypeAndMode(int type, int mode) {\r
34                 this.type = type;\r
35                 this.mode = mode;\r
36                 try {\r
37                         nxtCommand.setInputMode(id, type, mode);\r
38                 } catch (IOException ioe) {}\r
39         }\r
40         \r
41         public void setType(int type) {\r
42                 this.type = type;\r
43                 setTypeAndMode(type, mode);\r
44         }\r
45         \r
46         public void setMode(int mode) {\r
47                 this.mode = mode;\r
48                 setTypeAndMode(type, mode);\r
49         }\r
50         \r
51         /**\r
52          * Reads the boolean value of the sensor.\r
53          * @return Boolean value of sensor.\r
54          */\r
55         public boolean readBooleanValue() {\r
56                 try {\r
57                         InputValues vals = nxtCommand.getInputValues(id);\r
58                         return (vals.rawADValue<600);                   \r
59                 } catch (IOException ioe) {\r
60                         return false;\r
61                 }\r
62         }\r
63         \r
64     /**\r
65      * Reads the raw value of the sensor.\r
66      * @return Raw sensor value. Range is device dependent.\r
67      */\r
68         public int readRawValue() {\r
69                 try {\r
70                         InputValues vals = nxtCommand.getInputValues(id);\r
71                         return vals.rawADValue;\r
72                 } catch (IOException ioe) {\r
73                         return 0;\r
74                 }\r
75         }\r
76         \r
77         /**\r
78          * Returns value compatible with Lego firmware. \r
79          */\r
80         public int readValue()\r
81           {\r
82             int rawValue = readRawValue();\r
83             \r
84             if (mode == MODE_BOOLEAN)\r
85             {\r
86                 return (rawValue < 600 ? 1 : 0);\r
87             }\r
88             \r
89             if (mode == MODE_PCTFULLSCALE)\r
90             {\r
91                 return ((1023 - rawValue) * 100/ 1023);\r
92             }\r
93             \r
94             return rawValue;\r
95         }\r
96 }\r
97 \r