OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / rcxcomm / LLC.java
1 package lejos.rcxcomm;\r
2 \r
3 import lejos.nxt.*;\r
4 \r
5 /**\r
6  * Emulates RCX LLC class using the RCXLink class.\r
7  **/\r
8 public class LLC {\r
9   private static RCXLink link;\r
10   private static byte[] data = new byte[1];\r
11   \r
12   private LLC() {\r
13   }\r
14 \r
15   /** \r
16    * Initialize LLC and set port\r
17    **/\r
18   public static void init(SensorPort port){\r
19           link = new RCXLink(port);\r
20           link.setDefaultSpeed();\r
21           link.flush();\r
22   }\r
23   \r
24   /** \r
25    * Initialize LLC an\r
26    **/\r
27   public static void init() {\r
28           link.setDefaultSpeed();\r
29           link.flush();\r
30   }\r
31   \r
32   /** \r
33    * Set sensor port\r
34    **/\r
35   public static void setPort(SensorPort port) {\r
36           link = new RCXLink(port);\r
37   }\r
38 \r
39   /**\r
40    * read a single byte, if available\r
41    * @return the byte read, or -1 if no byte is available\r
42    **/\r
43   public static int read() {\r
44           int numBytes = link.bytesAvailable();   \r
45           if (numBytes == 0) return -1;\r
46           data[0] = 0;\r
47           link.readBytes(data);\r
48           return (data[0] & 0xFF);\r
49   }\r
50 \r
51   /**\r
52    * Write raw bytes\r
53    * \r
54    * @param buf the bytes to write\r
55    * @param len the number of bytes\r
56    */\r
57   private static void write(byte [] buf, int len) {\r
58           link.sendBytes(buf, len);\r
59   }\r
60 \r
61   /**\r
62    * Indicate whether the last send is still active\r
63    * @return true if still sending, else false\r
64    **/\r
65   public static boolean isSending() {\r
66           return false;\r
67   }\r
68 \r
69   /**\r
70    * Return the error status of the last send\r
71    * @return true if still sending, else false\r
72    **/\r
73   public static boolean isSendError() {\r
74           return false;\r
75   }\r
76 \r
77   /**\r
78    * Send a number of bytes and wait for completion of transmission\r
79    * @param buf the array of bytes to send\r
80    * @param len the number of bytes to send\r
81    * @return true if the send is successful, else false\r
82    **/\r
83   public static boolean sendBytes(byte [] buf, int len) {\r
84           LLC.write(buf, len);\r
85           return true;\r
86   }\r
87 \r
88   /**\r
89    * wait a little while for a byte to become available\r
90    * @return the byte received, or -1 if no byte available\r
91    **/\r
92   public static int receive() {\r
93           int r;\r
94           for(int i=0;i<10;i++) {\r
95                   r = LLC.read();\r
96                   if (r >= 0) return r; \r
97                   Thread.yield();\r
98           }\r
99           return -1;\r
100   }\r
101 \r
102   /**\r
103    * Sets long range transmision.\r
104    */\r
105   public static void setRangeLong()\r
106   {\r
107           link.setRangeLong();  \r
108   }\r
109 \r
110   /**\r
111    * Sets short range transmision.\r
112    */\r
113   public static void setRangeShort()\r
114   {\r
115           link.setRangeLong();    \r
116   }\r
117   \r
118   /**\r
119    * Return the RCXLink object associated with LLC\r
120    * \r
121    * @return the RCXLink used\r
122    */\r
123   public static RCXLink getLink() {\r
124           return link;\r
125   }\r
126 }\r
127 \r
128 \r
129 \r