|
| 1 | +import struct |
| 2 | + |
| 3 | +# Minimal constants carried over from Arduino library |
| 4 | +ADXL345_ADDRESS = 0x53 |
| 5 | +ADXL345_REG_DEVID = 0x00 # Device ID |
| 6 | +ADXL345_REG_DATAX0 = 0x32 # X-axis data 0 (6 bytes for X/Y/Z) |
| 7 | +ADXL345_REG_POWER_CTL = 0x2D # Power-saving features control |
| 8 | +ADXL345_REG_DATA_FORMAT = 0x31 |
| 9 | +ADXL345_REG_BW_RATE = 0x2C |
| 10 | +ADXL345_DATARATE_0_10_HZ = 0x00 |
| 11 | +ADXL345_DATARATE_0_20_HZ = 0x01 |
| 12 | +ADXL345_DATARATE_0_39_HZ = 0x02 |
| 13 | +ADXL345_DATARATE_0_78_HZ = 0x03 |
| 14 | +ADXL345_DATARATE_1_56_HZ = 0x04 |
| 15 | +ADXL345_DATARATE_3_13_HZ = 0x05 |
| 16 | +ADXL345_DATARATE_6_25HZ = 0x06 |
| 17 | +ADXL345_DATARATE_12_5_HZ = 0x07 |
| 18 | +ADXL345_DATARATE_25_HZ = 0x08 |
| 19 | +ADXL345_DATARATE_50_HZ = 0x09 |
| 20 | +ADXL345_DATARATE_100_HZ = 0x0A # (default) |
| 21 | +ADXL345_DATARATE_200_HZ = 0x0B |
| 22 | +ADXL345_DATARATE_400_HZ = 0x0C |
| 23 | +ADXL345_DATARATE_800_HZ = 0x0D |
| 24 | +ADXL345_DATARATE_1600_HZ = 0x0E |
| 25 | +ADXL345_DATARATE_3200_HZ = 0x0F |
| 26 | +ADXL345_RANGE_2_G = 0x00 # +/- 2g (default) |
| 27 | +ADXL345_RANGE_4_G = 0x01 # +/- 4g |
| 28 | +ADXL345_RANGE_8_G = 0x02 # +/- 8g |
| 29 | +ADXL345_RANGE_16_G = 0x03 # +/- 16g |
| 30 | + |
| 31 | + |
| 32 | +class ADXL345(object): |
| 33 | + """ADXL345 triple-axis accelerometer.""" |
| 34 | + |
| 35 | + def __init__(self, address=ADXL345_ADDRESS, i2c=None, **kwargs): |
| 36 | + """Initialize the ADXL345 accelerometer using its I2C interface. |
| 37 | + """ |
| 38 | + # Setup I2C interface for the device. |
| 39 | + if i2c is None: |
| 40 | + import Adafruit_GPIO.I2C as I2C |
| 41 | + i2c = I2C |
| 42 | + self._device = i2c.get_i2c_device(address, **kwargs) |
| 43 | + # Check that the acclerometer is connected, then enable it. |
| 44 | + if self._device.readU8(ADXL345_REG_DEVID) == 0xE5: |
| 45 | + self._device.write8(ADXL345_REG_POWER_CTL, 0x08) |
| 46 | + else: |
| 47 | + raise RuntimeError('Failed to find the expected device ID register value, check your wiring.') |
| 48 | + |
| 49 | + def set_range(self, value): |
| 50 | + """Set the range of the accelerometer to the provided value. Range value |
| 51 | + should be one of these constants: |
| 52 | + - ADXL345_RANGE_2_G = +/-2G |
| 53 | + - ADXL345_RANGE_4_G = +/-4G |
| 54 | + - ADXL345_RANGE_8_G = +/-8G |
| 55 | + - ADXL345_RANGE_16_G = +/-16G |
| 56 | + """ |
| 57 | + # Read the data format register to preserve bits. Update the data |
| 58 | + # rate, make sure that the FULL-RES bit is enabled for range scaling |
| 59 | + format_reg = self._device.readU8(ADXL345_REG_DATA_FORMAT) & ~0x0F |
| 60 | + format_reg |= value |
| 61 | + format_reg |= 0x08 # FULL-RES bit enabled |
| 62 | + # Write the updated format register. |
| 63 | + self._device.write8(ADXL345_REG_DATA_FORMAT, format_reg) |
| 64 | + |
| 65 | + def get_range(self): |
| 66 | + """Retrieve the current range of the accelerometer. See set_range for |
| 67 | + the possible range constant values that will be returned. |
| 68 | + """ |
| 69 | + return self._device.readU8(ADXL345_REG_DATA_FORMAT) & 0x03 |
| 70 | + |
| 71 | + def set_data_rate(self, rate): |
| 72 | + """Set the data rate of the aceelerometer. Rate should be one of the |
| 73 | + following constants: |
| 74 | + - ADXL345_DATARATE_0_10_HZ = 0.1 hz |
| 75 | + - ADXL345_DATARATE_0_20_HZ = 0.2 hz |
| 76 | + - ADXL345_DATARATE_0_39_HZ = 0.39 hz |
| 77 | + - ADXL345_DATARATE_0_78_HZ = 0.78 hz |
| 78 | + - ADXL345_DATARATE_1_56_HZ = 1.56 hz |
| 79 | + - ADXL345_DATARATE_3_13_HZ = 3.13 hz |
| 80 | + - ADXL345_DATARATE_6_25HZ = 6.25 hz |
| 81 | + - ADXL345_DATARATE_12_5_HZ = 12.5 hz |
| 82 | + - ADXL345_DATARATE_25_HZ = 25 hz |
| 83 | + - ADXL345_DATARATE_50_HZ = 50 hz |
| 84 | + - ADXL345_DATARATE_100_HZ = 100 hz |
| 85 | + - ADXL345_DATARATE_200_HZ = 200 hz |
| 86 | + - ADXL345_DATARATE_400_HZ = 400 hz |
| 87 | + - ADXL345_DATARATE_800_HZ = 800 hz |
| 88 | + - ADXL345_DATARATE_1600_HZ = 1600 hz |
| 89 | + - ADXL345_DATARATE_3200_HZ = 3200 hz |
| 90 | + """ |
| 91 | + # Note: The LOW_POWER bits are currently ignored, |
| 92 | + # we always keep the device in 'normal' mode |
| 93 | + self._device.write8(ADXL345_REG_BW_RATE, rate & 0x0F) |
| 94 | + |
| 95 | + def get_data_rate(self): |
| 96 | + """Retrieve the current data rate. See set_data_rate for the possible |
| 97 | + data rate constant values that will be returned. |
| 98 | + """ |
| 99 | + return self._device.readU8(ADXL345_REG_BW_RATE) & 0x0F |
| 100 | + |
| 101 | + def read(self): |
| 102 | + """Read the current value of the accelerometer and return it as a tuple |
| 103 | + of signed 16-bit X, Y, Z axis values. |
| 104 | + """ |
| 105 | + raw = self._device.readList(ADXL345_REG_DATAX0, 6) |
| 106 | + return struct.unpack('<hhh', raw) |
0 commit comments