OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[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         byte [] buf1 = new byte[1];
22         
23         public I2CSensor(I2CPort port)
24         {
25                 this.port = port;
26                 port.i2cEnable();
27                 port.setType(TYPE_LOWSPEED);
28         }
29         
30         /**
31          * Executes an I2C read transaction and waits for the result.
32          * 
33          * @param register I2C register, e.g 0x41
34          * @param buf Buffer to return data
35          * @param len Length of the return data
36          * @return status zero=success, non-zero=failure
37          */
38         public int getData(int register, byte [] buf, int len) {        
39                 int ret = port.i2cStart(address, register, len, buf, len, 0);
40                 
41                 if (ret != 0) return ret;
42                 
43                 while (port.i2cBusy() != 0) {
44                         Thread.yield();
45                 }
46                 
47                 return 0;
48         }
49         
50         /**
51          *  Executes an I2C write transaction.
52          *  
53          * @param register I2C register, e.g 0x42
54          * @param buf Buffer containing data to send
55          * @param len Length of data to send
56          * @return status zero=success, non-zero=failure
57          */
58         public int sendData(int register, byte [] buf, int len) {
59         int ret = port.i2cStart(address, register, len, buf, len, 1);
60                 if (ret != 0) return ret;
61                 
62                 while (port.i2cBusy() != 0) {
63                         Thread.yield();
64                 }
65                 
66                 return 0;
67         }
68         
69         /**
70          *  Executes an I2C write transaction.
71          *  
72          * @param register I2C register, e.g 0x42
73          * @param value single byte to send
74          * @return status zero=success, non-zero=failure
75          */
76         public int sendData(int register, byte value) {
77                 buf1[0] = value;
78                 return sendData(register, buf1, 1);
79         }
80         
81         /**
82          * Return the sensor version number.
83          * 
84          * @return 8-byte string
85          */
86         public String getVersion() {
87                 int ret = getData(0x00, byteBuff, 8);
88
89                 for(int i=0;i<8;i++) {
90                         versionChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
91                 }       
92                 return version;
93         }
94         
95         /**
96          * Return the sensor product identifier.
97          * 
98          * @return 8-byte string
99          */
100         public String getProductID() {
101                 int ret = getData(0x08, byteBuff, 8);
102
103                 for(int i=0;i<8;i++) {
104                         productIDChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
105                 }       
106                 return productID;
107         }
108         
109         /**
110          * Return the sensor type.
111          * 
112          * @return 8-byte string
113          */
114         public String getSensorType() {
115                 int ret = getData(0x10, byteBuff, 8);
116
117                 for(int i=0;i<8;i++) {
118                         sensorTypeChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
119                 }       
120                 return sensorType;
121         }
122         
123         public void setAddress(int addr) {
124                 address = addr;
125         }
126 }