4 * Abstract class that implements common methods for all I2C sensors.
6 * Extend this class to implement new I2C sensors.
8 * @author Lawrie Griffiths (lawrie.griffiths@ntlworld.com)
11 public abstract class I2CSensor implements SensorConstants {
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];
22 public I2CSensor(I2CPort port)
26 port.setType(TYPE_LOWSPEED);
30 * Executes an I2C read transaction and waits for the result.
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
37 public int getData(int register, byte [] buf, int len) {
38 int ret = port.i2cStart(address, register, len, buf, len, 0);
40 if (ret != 0) return ret;
42 while (port.i2cBusy() != 0) {
50 * Executes an I2C write transaction - not yet working.
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
57 public int sendData(int register, byte [] buf, int len) {
58 int ret = port.i2cStart(address, register, len, buf, len, 1);
60 if (ret != 0) return ret;
62 while (port.i2cBusy() != 0) {
70 * Return the sensor version number.
72 * @return 8-byte string
74 public String getVersion() {
75 int ret = getData(0x00, byteBuff, 8);
77 for(int i=0;i<8;i++) {
78 versionChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
84 * Return the sensor product identifier.
86 * @return 8-byte string
88 public String getProductID() {
89 int ret = getData(0x08, byteBuff, 8);
91 for(int i=0;i<8;i++) {
92 productIDChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');
98 * Return the sensor type.
100 * @return 8-byte string
102 public String getSensorType() {
103 int ret = getData(0x10, byteBuff, 8);
105 for(int i=0;i<8;i++) {
106 sensorTypeChars[i] = (ret == 0 ? (char) byteBuff[i] : ' ');