OSDN Git Service

f4a218fa813e59888603a3cc3c7c23a94b3819ba
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / I2CSensor.java
1 package lejos.nxt;
2
3 /**
4  * Abstract class that implements common methods for all I2C sensors.
5  * 
6  * Extend this class to implement new I2C sensors.
7  * 
8  * @author Lawrie Griffiths (lawrie.griffiths@ntlworld.com)
9  *
10  */
11 public abstract class I2CSensor implements SensorConstants {
12         I2CPort port;
13         int address = 1;
14         String version = "        ";
15         String productID = "        ";
16         String sensorType = "        ";
17         char [] versionChars = StringUtils.getCharacters(version);
18         char [] productIDChars = StringUtils.getCharacters(productID);
19         char [] sensorTypeChars = StringUtils.getCharacters(sensorType);
20         byte[] byteBuff = new byte[8]; 
21         
22         public I2CSensor(I2CPort port)
23         {
24                 this.port = port;
25                 port.i2cEnable();
26                 port.setType(TYPE_LOWSPEED);
27         }
28         
29         /**
30          * Executes an I2C read transaction and waits for the result.
31          * 
32          * @param register I2C register, e.g 0x41
33          * @param buf Buffer to return data
34          * @param len Length of the return data
35          * @return status zero=success, non-zero=failure
36          */
37         public int getData(int register, byte [] buf, int len) {        
38                 int ret = port.i2cStart(address, register, len, buf, len, 0);
39                 
40                 if (ret != 0) return ret;
41                 
42                 while (port.i2cBusy() != 0) {
43                         Thread.yield();
44                 }
45                 
46                 return 0;
47         }
48         
49         /**
50          *  Executes an I2C write transaction - not yet working.
51          *  
52          * @param register I2C register, e.g 0x42
53          * @param buf Buffer containing data to send
54          * @param len Length of data to send
55          * @return status zero=success, non-zero=failure
56          */
57         public int sendData(int register, byte [] buf, int len) {       
58                 int ret = port.i2cStart(address, register, len, buf, len, 1);
59                 
60                 if (ret != 0) return ret;
61                 
62                 while (port.i2cBusy() != 0) {
63                         Thread.yield();
64                 }
65                 
66                 return 0;
67         }
68         
69         /**
70          * Return the sensor version number.
71          * 
72          * @return 8-byte string
73          */
74         public String getVersion() {
75                 int ret = getData(0x00, byteBuff, 8);
76
77                 for(int i=0;i<8;i++) {
78                         versionChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
79                 }       
80                 return version;
81         }
82         
83         /**
84          * Return the sensor product identifier.
85          * 
86          * @return 8-byte string
87          */
88         public String getProductID() {
89                 int ret = getData(0x08, byteBuff, 8);
90
91                 for(int i=0;i<8;i++) {
92                         productIDChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
93                 }       
94                 return productID;
95         }
96         
97         /**
98          * Return the sensor type.
99          * 
100          * @return 8-byte string
101          */
102         public String getSensorType() {
103                 int ret = getData(0x10, byteBuff, 8);
104
105                 for(int i=0;i<8;i++) {
106                         sensorTypeChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
107                 }       
108                 return sensorType;
109         }
110 }