|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2016 Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +import struct |
| 23 | + |
| 24 | + |
| 25 | +# Minimal constants carried over from Arduino library: |
| 26 | +LSM303_ADDRESS_ACCEL = (0x32 >> 1) # 0011001x |
| 27 | +LSM303_ADDRESS_MAG = (0x3C >> 1) # 0011110x |
| 28 | + # Default Type |
| 29 | +LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20 # 00000111 rw |
| 30 | +LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23 # 00000000 rw |
| 31 | +LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28 |
| 32 | +LSM303_REGISTER_MAG_CRB_REG_M = 0x01 |
| 33 | +LSM303_REGISTER_MAG_MR_REG_M = 0x02 |
| 34 | +LSM303_REGISTER_MAG_OUT_X_H_M = 0x03 |
| 35 | + |
| 36 | +# Gain settings for set_mag_gain() |
| 37 | +LSM303_MAGGAIN_1_3 = 0x20 # +/- 1.3 |
| 38 | +LSM303_MAGGAIN_1_9 = 0x40 # +/- 1.9 |
| 39 | +LSM303_MAGGAIN_2_5 = 0x60 # +/- 2.5 |
| 40 | +LSM303_MAGGAIN_4_0 = 0x80 # +/- 4.0 |
| 41 | +LSM303_MAGGAIN_4_7 = 0xA0 # +/- 4.7 |
| 42 | +LSM303_MAGGAIN_5_6 = 0xC0 # +/- 5.6 |
| 43 | +LSM303_MAGGAIN_8_1 = 0xE0 # +/- 8.1 |
| 44 | + |
| 45 | + |
| 46 | +class LSM303(object): |
| 47 | + """LSM303 accelerometer & magnetometer.""" |
| 48 | + |
| 49 | + def __init__(self, hires=True, accel_address=LSM303_ADDRESS_ACCEL, |
| 50 | + mag_address=LSM303_ADDRESS_MAG, i2c=None, **kwargs): |
| 51 | + """Initialize the LSM303 accelerometer & magnetometer. The hires |
| 52 | + boolean indicates if high resolution (12-bit) mode vs. low resolution |
| 53 | + (10-bit, faster and lower power) mode should be used. |
| 54 | + """ |
| 55 | + # Setup I2C interface for accelerometer and magnetometer. |
| 56 | + if i2c is None: |
| 57 | + import Adafruit_GPIO.I2C as I2C |
| 58 | + i2c = I2C |
| 59 | + self._accel = i2c.get_i2c_device(accel_address, **kwargs) |
| 60 | + self._mag = i2c.get_i2c_device(mag_address, **kwargs) |
| 61 | + # Enable the accelerometer |
| 62 | + self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x27) |
| 63 | + # Select hi-res (12-bit) or low-res (10-bit) output mode. |
| 64 | + # Low-res mode uses less power and sustains a higher update rate, |
| 65 | + # output is padded to compatible 12-bit units. |
| 66 | + if hires: |
| 67 | + self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0b00001000) |
| 68 | + else: |
| 69 | + self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0) |
| 70 | + # Enable the magnetometer |
| 71 | + self._mag.write8(LSM303_REGISTER_MAG_MR_REG_M, 0x00) |
| 72 | + |
| 73 | + def read(self): |
| 74 | + """Read the accelerometer and magnetometer value. A tuple of tuples will |
| 75 | + be returned with: |
| 76 | + ((accel X, accel Y, accel Z), (mag X, mag Y, mag Z)) |
| 77 | + """ |
| 78 | + # Read the accelerometer as signed 16-bit little endian values. |
| 79 | + accel_raw = self._accel.readList(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80, 6) |
| 80 | + accel = struct.unpack('<hhh', accel_raw) |
| 81 | + # Convert to 12-bit values by shifting unused bits. |
| 82 | + accel = (accel[0] >> 4, accel[1] >> 4, accel[2] >> 4) |
| 83 | + # Read the magnetometer. |
| 84 | + mag_raw = self._mag.readList(LSM303_REGISTER_MAG_OUT_X_H_M, 6) |
| 85 | + mag = struct.unpack('>hhh', mag_raw) |
| 86 | + return (accel, mag) |
| 87 | + |
| 88 | + def set_mag_gain(gain=LSM303_MAGGAIN_1_3): |
| 89 | + """Set the magnetometer gain. Gain should be one of the following |
| 90 | + constants: |
| 91 | + - LSM303_MAGGAIN_1_3 = +/- 1.3 (default) |
| 92 | + - LSM303_MAGGAIN_1_9 = +/- 1.9 |
| 93 | + - LSM303_MAGGAIN_2_5 = +/- 2.5 |
| 94 | + - LSM303_MAGGAIN_4_0 = +/- 4.0 |
| 95 | + - LSM303_MAGGAIN_4_7 = +/- 4.7 |
| 96 | + - LSM303_MAGGAIN_5_6 = +/- 5.6 |
| 97 | + - LSM303_MAGGAIN_8_1 = +/- 8.1 |
| 98 | + """ |
| 99 | + self._mag.write8(LSM303_REGISTER_MAG_CRB_REG_M, gain) |
0 commit comments