|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Python library for ADXL345 accelerometer. |
| 4 | + |
| 5 | +# Copyright 2013 Adafruit Industries |
| 6 | + |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | +# copy of this software and associated documentation files (the "Software"), |
| 9 | +# to deal in the Software without restriction, including without limitation |
| 10 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 12 | +# Software is furnished to do so, subject to the following conditions: |
| 13 | + |
| 14 | +# The above copyright notice and this permission notice shall be included |
| 15 | +# in all copies or substantial portions of the Software. |
| 16 | + |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | +# DEALINGS IN THE SOFTWARE. |
| 24 | + |
| 25 | +from Adafruit_I2C import Adafruit_I2C |
| 26 | + |
| 27 | + |
| 28 | +class Adafruit_ADXL345(Adafruit_I2C): |
| 29 | + |
| 30 | + # Minimal constants carried over from Arduino library |
| 31 | + |
| 32 | + ADXL345_ADDRESS = 0x53 |
| 33 | + |
| 34 | + ADXL345_REG_DEVID = 0x00 # Device ID |
| 35 | + ADXL345_REG_DATAX0 = 0x32 # X-axis data 0 (6 bytes for X/Y/Z) |
| 36 | + ADXL345_REG_POWER_CTL = 0x2D # Power-saving features control |
| 37 | + |
| 38 | + ADXL345_DATARATE_0_10_HZ = 0x00 |
| 39 | + ADXL345_DATARATE_0_20_HZ = 0x01 |
| 40 | + ADXL345_DATARATE_0_39_HZ = 0x02 |
| 41 | + ADXL345_DATARATE_0_78_HZ = 0x03 |
| 42 | + ADXL345_DATARATE_1_56_HZ = 0x04 |
| 43 | + ADXL345_DATARATE_3_13_HZ = 0x05 |
| 44 | + ADXL345_DATARATE_6_25HZ = 0x06 |
| 45 | + ADXL345_DATARATE_12_5_HZ = 0x07 |
| 46 | + ADXL345_DATARATE_25_HZ = 0x08 |
| 47 | + ADXL345_DATARATE_50_HZ = 0x09 |
| 48 | + ADXL345_DATARATE_100_HZ = 0x0A # (default) |
| 49 | + ADXL345_DATARATE_200_HZ = 0x0B |
| 50 | + ADXL345_DATARATE_400_HZ = 0x0C |
| 51 | + ADXL345_DATARATE_800_HZ = 0x0D |
| 52 | + ADXL345_DATARATE_1600_HZ = 0x0E |
| 53 | + ADXL345_DATARATE_3200_HZ = 0x0F |
| 54 | + |
| 55 | + ADXL345_RANGE_2_G = 0x00 # +/- 2g (default) |
| 56 | + ADXL345_RANGE_4_G = 0x01 # +/- 4g |
| 57 | + ADXL345_RANGE_8_G = 0x02 # +/- 8g |
| 58 | + ADXL345_RANGE_16_G = 0x03 # +/- 16g |
| 59 | + |
| 60 | + |
| 61 | + def __init__(self, busnum=-1, debug=False): |
| 62 | + |
| 63 | + self.accel = Adafruit_I2C(self.ADXL345_ADDRESS, busnum, debug) |
| 64 | + |
| 65 | + if self.accel.readU8(self.ADXL345_REG_DEVID) == 0xE5: |
| 66 | + # Enable the accelerometer |
| 67 | + self.accel.write8(self.ADXL345_REG_POWER_CTL, 0x08) |
| 68 | + |
| 69 | + |
| 70 | + def setRange(self, range): |
| 71 | + # Read the data format register to preserve bits. Update the data |
| 72 | + # rate, make sure that the FULL-RES bit is enabled for range scaling |
| 73 | + format = ((self.accel.readU8(self.ADXL345_REG_DATA_FORMAT) & ~0x0F) | |
| 74 | + range | 0x08) |
| 75 | + # Write the register back to the IC |
| 76 | + seld.accel.write8(self.ADXL345_REG_DATA_FORMAT, format) |
| 77 | + |
| 78 | + |
| 79 | + def getRange(self): |
| 80 | + return self.accel.readU8(self.ADXL345_REG_DATA_FORMAT) & 0x03 |
| 81 | + |
| 82 | + |
| 83 | + def setDataRate(self, dataRate): |
| 84 | + # Note: The LOW_POWER bits are currently ignored, |
| 85 | + # we always keep the device in 'normal' mode |
| 86 | + self.accel.write8(self.ADXL345_REG_BW_RATE, dataRate & 0x0F) |
| 87 | + |
| 88 | + |
| 89 | + def getDataRate(self): |
| 90 | + return self.accel.readU8(self.ADXL345_REG_BW_RATE) & 0x0F |
| 91 | + |
| 92 | + |
| 93 | + # Read the accelerometer |
| 94 | + def read(self): |
| 95 | + raw = self.accel.readList(self.ADXL345_REG_DATAX0, 6) |
| 96 | + res = [] |
| 97 | + for i in range(0, 6, 2): |
| 98 | + g = raw[i] | (raw[i+1] << 8) |
| 99 | + if g > 32767: g -= 65536 |
| 100 | + res.append(g) |
| 101 | + return res |
| 102 | + |
| 103 | + |
| 104 | +# Simple example prints accelerometer data once per second: |
| 105 | +if __name__ == '__main__': |
| 106 | + |
| 107 | + from time import sleep |
| 108 | + |
| 109 | + accel = Adafruit_ADXL345() |
| 110 | + |
| 111 | + print '[Accelerometer X, Y, Z]' |
| 112 | + while True: |
| 113 | + print accel.read() |
| 114 | + sleep(1) # Output is fun to watch if this is commented out |
0 commit comments