Added function to retrieve raw data from sensor
Dependents: KL46_eCompass KL46_eCompass_TiltCompensed_Acel-Mag Ragnarok_2ejes compass_acc ... more
Fork of MMA8451Q by
MMA8451Q.cpp@6:d3f7851ff32e, 2014-05-16 (annotated)
- Committer:
- JimCarver
- Date:
- Fri May 16 18:21:49 2014 +0000
- Revision:
- 6:d3f7851ff32e
- Parent:
- 5:b8512e0de86b
Implements new virtual MotionSensor class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:d2630136d51e | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
samux | 1:d2630136d51e | 2 | * |
samux | 1:d2630136d51e | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:d2630136d51e | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
samux | 1:d2630136d51e | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
samux | 1:d2630136d51e | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
samux | 1:d2630136d51e | 7 | * Software is furnished to do so, subject to the following conditions: |
samux | 1:d2630136d51e | 8 | * |
samux | 1:d2630136d51e | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:d2630136d51e | 10 | * substantial portions of the Software. |
samux | 1:d2630136d51e | 11 | * |
samux | 1:d2630136d51e | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:d2630136d51e | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:d2630136d51e | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:d2630136d51e | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:d2630136d51e | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:d2630136d51e | 17 | */ |
samux | 1:d2630136d51e | 18 | |
emilmont | 0:6149091f755d | 19 | #include "MMA8451Q.h" |
JimCarver | 6:d3f7851ff32e | 20 | #include "MotionSensor.h" |
emilmont | 0:6149091f755d | 21 | |
JimCarver | 6:d3f7851ff32e | 22 | #define REG_WHOAMI 0x0D |
samux | 1:d2630136d51e | 23 | #define REG_CTRL_REG_1 0x2A |
JimCarver | 6:d3f7851ff32e | 24 | #define REG_DR_STATUS 0x00 |
emilmont | 0:6149091f755d | 25 | #define REG_OUT_X_MSB 0x01 |
emilmont | 0:6149091f755d | 26 | #define REG_OUT_Y_MSB 0x03 |
emilmont | 0:6149091f755d | 27 | #define REG_OUT_Z_MSB 0x05 |
emilmont | 0:6149091f755d | 28 | |
samux | 1:d2630136d51e | 29 | #define UINT14_MAX 16383 |
emilmont | 0:6149091f755d | 30 | |
JimCarver | 6:d3f7851ff32e | 31 | float countsperG = 4096.0f; // default 2G range |
JimCarver | 6:d3f7851ff32e | 32 | |
emilmont | 0:6149091f755d | 33 | MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
emilmont | 0:6149091f755d | 34 | // activate the peripheral |
JimCarver | 6:d3f7851ff32e | 35 | uint8_t data[2] = {REG_CTRL_REG_1, 0x00}; |
samux | 1:d2630136d51e | 36 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 37 | } |
emilmont | 0:6149091f755d | 38 | |
emilmont | 0:6149091f755d | 39 | MMA8451Q::~MMA8451Q() { } |
emilmont | 0:6149091f755d | 40 | |
JimCarver | 6:d3f7851ff32e | 41 | void MMA8451Q::enable(void) { |
JimCarver | 6:d3f7851ff32e | 42 | uint8_t data[2]; |
JimCarver | 6:d3f7851ff32e | 43 | readRegs( REG_CTRL_REG_1, &data[1], 1); |
JimCarver | 6:d3f7851ff32e | 44 | data[1] |= 0x01; |
JimCarver | 6:d3f7851ff32e | 45 | data[0] = REG_CTRL_REG_1; |
JimCarver | 6:d3f7851ff32e | 46 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 47 | } |
emilmont | 0:6149091f755d | 48 | |
JimCarver | 6:d3f7851ff32e | 49 | void MMA8451Q::disable(void) { |
JimCarver | 6:d3f7851ff32e | 50 | uint8_t data[2]; |
JimCarver | 6:d3f7851ff32e | 51 | readRegs( REG_CTRL_REG_1, &data[1], 1); |
JimCarver | 6:d3f7851ff32e | 52 | data[1] &= 0xFE; |
JimCarver | 6:d3f7851ff32e | 53 | data[0] = REG_CTRL_REG_1; |
JimCarver | 6:d3f7851ff32e | 54 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 55 | } |
emilmont | 0:6149091f755d | 56 | |
JimCarver | 6:d3f7851ff32e | 57 | uint32_t MMA8451Q::whoAmI() { |
JimCarver | 6:d3f7851ff32e | 58 | uint8_t who_am_i = 0; |
JimCarver | 6:d3f7851ff32e | 59 | readRegs(REG_WHOAMI, &who_am_i, 1); |
JimCarver | 6:d3f7851ff32e | 60 | return (uint32_t) who_am_i; |
emilmont | 0:6149091f755d | 61 | } |
emilmont | 0:6149091f755d | 62 | |
JimCarver | 6:d3f7851ff32e | 63 | uint32_t MMA8451Q::dataReady(void) { |
JimCarver | 6:d3f7851ff32e | 64 | uint8_t stat = 0; |
JimCarver | 6:d3f7851ff32e | 65 | readRegs(REG_DR_STATUS, &stat, 1); |
JimCarver | 6:d3f7851ff32e | 66 | return (uint32_t) stat; |
JimCarver | 6:d3f7851ff32e | 67 | } |
JimCarver | 5:b8512e0de86b | 68 | |
JimCarver | 6:d3f7851ff32e | 69 | uint32_t MMA8451Q::sampleRate(uint32_t f) { |
JimCarver | 6:d3f7851ff32e | 70 | return(50); // for now sample rate is fixed at 50Hz |
JimCarver | 6:d3f7851ff32e | 71 | } |
JimCarver | 6:d3f7851ff32e | 72 | |
JimCarver | 6:d3f7851ff32e | 73 | void MMA8451Q::getX(float * x) { |
JimCarver | 6:d3f7851ff32e | 74 | *x = (float(getAccAxis(REG_OUT_X_MSB)) / countsperG); |
emilmont | 0:6149091f755d | 75 | } |
emilmont | 0:6149091f755d | 76 | |
JimCarver | 6:d3f7851ff32e | 77 | void MMA8451Q::getY(float * y) { |
JimCarver | 6:d3f7851ff32e | 78 | *y = (float(getAccAxis(REG_OUT_Y_MSB)) / countsperG); |
JimCarver | 6:d3f7851ff32e | 79 | } |
JimCarver | 6:d3f7851ff32e | 80 | |
JimCarver | 6:d3f7851ff32e | 81 | void MMA8451Q::getZ(float * z) { |
JimCarver | 6:d3f7851ff32e | 82 | *z = (float(getAccAxis(REG_OUT_Z_MSB)) / countsperG); |
JimCarver | 6:d3f7851ff32e | 83 | } |
JimCarver | 5:b8512e0de86b | 84 | |
JimCarver | 6:d3f7851ff32e | 85 | void MMA8451Q::getX(int16_t * d) { |
JimCarver | 6:d3f7851ff32e | 86 | *d = getAccAxis(REG_OUT_X_MSB); |
JimCarver | 6:d3f7851ff32e | 87 | } |
JimCarver | 6:d3f7851ff32e | 88 | |
JimCarver | 6:d3f7851ff32e | 89 | void MMA8451Q::getY(int16_t * d) { |
JimCarver | 6:d3f7851ff32e | 90 | *d = getAccAxis(REG_OUT_Y_MSB); |
JimCarver | 6:d3f7851ff32e | 91 | } |
JimCarver | 6:d3f7851ff32e | 92 | |
JimCarver | 6:d3f7851ff32e | 93 | void MMA8451Q::getZ(int16_t * d) { |
JimCarver | 6:d3f7851ff32e | 94 | *d = getAccAxis(REG_OUT_Z_MSB); |
JimCarver | 5:b8512e0de86b | 95 | } |
JimCarver | 5:b8512e0de86b | 96 | |
emilmont | 0:6149091f755d | 97 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
emilmont | 0:6149091f755d | 98 | int16_t acc; |
emilmont | 0:6149091f755d | 99 | uint8_t res[2]; |
samux | 1:d2630136d51e | 100 | readRegs(addr, res, 2); |
emilmont | 0:6149091f755d | 101 | |
JimCarver | 6:d3f7851ff32e | 102 | acc = (res[0] << 8) | res[1]; |
JimCarver | 6:d3f7851ff32e | 103 | acc = acc >> 2; |
JimCarver | 6:d3f7851ff32e | 104 | return acc; |
JimCarver | 6:d3f7851ff32e | 105 | } |
JimCarver | 6:d3f7851ff32e | 106 | |
JimCarver | 6:d3f7851ff32e | 107 | |
JimCarver | 6:d3f7851ff32e | 108 | void MMA8451Q::getAxis(MotionSensorDataUnits &data) { |
JimCarver | 6:d3f7851ff32e | 109 | int16_t t[3]; |
JimCarver | 6:d3f7851ff32e | 110 | uint8_t res[6]; |
emilmont | 0:6149091f755d | 111 | |
JimCarver | 6:d3f7851ff32e | 112 | readRegs(REG_OUT_X_MSB, res, 6); |
JimCarver | 6:d3f7851ff32e | 113 | t[0] = (res[0] << 8) | res[1]; |
JimCarver | 6:d3f7851ff32e | 114 | t[1] = (res[2] << 8) | res[3]; |
JimCarver | 6:d3f7851ff32e | 115 | t[2] = (res[4] << 8) | res[5]; |
JimCarver | 6:d3f7851ff32e | 116 | data.x = ((float) (t[0] >> 2)) / countsperG; |
JimCarver | 6:d3f7851ff32e | 117 | data.y = ((float) (t[1] >> 2)) / countsperG; |
JimCarver | 6:d3f7851ff32e | 118 | data.z = ((float) (t[2] >> 2)) / countsperG; |
JimCarver | 6:d3f7851ff32e | 119 | } |
JimCarver | 6:d3f7851ff32e | 120 | |
JimCarver | 6:d3f7851ff32e | 121 | |
JimCarver | 6:d3f7851ff32e | 122 | void MMA8451Q::getAxis(MotionSensorDataCounts &data) { |
JimCarver | 6:d3f7851ff32e | 123 | int16_t acc; |
JimCarver | 6:d3f7851ff32e | 124 | uint8_t res[6]; |
JimCarver | 6:d3f7851ff32e | 125 | readRegs(REG_OUT_X_MSB, res, 6); |
JimCarver | 6:d3f7851ff32e | 126 | |
JimCarver | 6:d3f7851ff32e | 127 | |
JimCarver | 6:d3f7851ff32e | 128 | acc = (res[0] << 8) | res[1]; |
JimCarver | 6:d3f7851ff32e | 129 | data.x = acc >> 2; |
JimCarver | 6:d3f7851ff32e | 130 | acc = (res[2] << 8) | res[3]; |
JimCarver | 6:d3f7851ff32e | 131 | data.y = acc >> 2; |
JimCarver | 6:d3f7851ff32e | 132 | acc = (res[4] << 8) | res[5]; |
JimCarver | 6:d3f7851ff32e | 133 | data.z = acc >> 2; |
emilmont | 0:6149091f755d | 134 | } |
emilmont | 0:6149091f755d | 135 | |
samux | 1:d2630136d51e | 136 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 137 | char t[1] = {addr}; |
emilmont | 0:6149091f755d | 138 | m_i2c.write(m_addr, t, 1, true); |
emilmont | 0:6149091f755d | 139 | m_i2c.read(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 140 | } |
emilmont | 0:6149091f755d | 141 | |
samux | 1:d2630136d51e | 142 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 143 | m_i2c.write(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 144 | } |