Glimit
Fork of MMA8451Q8 by
MMA8451Q8.cpp@8:a515da97b396, 2017-03-08 (annotated)
- Committer:
- mireyarod23
- Date:
- Wed Mar 08 05:15:45 2017 +0000
- Revision:
- 8:a515da97b396
- Parent:
- 7:7812354ef684
Attempt;
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 | */ |
scohennm | 5:be042e8c1756 | 18 | // Modify to change full scale gravity range 141207 sc |
samux | 1:d2630136d51e | 19 | |
scohennm | 5:be042e8c1756 | 20 | |
scohennm | 5:be042e8c1756 | 21 | #include "MMA8451Q8.h" |
emilmont | 0:6149091f755d | 22 | |
samux | 1:d2630136d51e | 23 | #define REG_WHO_AM_I 0x0D |
samux | 1:d2630136d51e | 24 | #define REG_CTRL_REG_1 0x2A |
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 |
scohennm | 5:be042e8c1756 | 28 | #define XYZ_DATA_CFG 0x0E |
emilmont | 0:6149091f755d | 29 | |
samux | 1:d2630136d51e | 30 | #define UINT14_MAX 16383 |
emilmont | 0:6149091f755d | 31 | |
scohennm | 5:be042e8c1756 | 32 | #define MAX_2G 0x00 |
scohennm | 5:be042e8c1756 | 33 | #define MAX_4G 0x01 |
scohennm | 6:c29386367dcf | 34 | #define MAX_8G 0x02 |
scohennm | 5:be042e8c1756 | 35 | |
mireyarod23 | 8:a515da97b396 | 36 | #define NUM_DATA 2 |
scohennm | 5:be042e8c1756 | 37 | #define GSCALING 1024.0 |
mireyarod23 | 8:a515da97b396 | 38 | #define ADDRESS_INDEX 0 |
mireyarod23 | 8:a515da97b396 | 39 | #define DATA_INDEX 1 |
mireyarod23 | 8:a515da97b396 | 40 | |
mireyarod23 | 8:a515da97b396 | 41 | float gScaling[3] = {4095.0,2048.0,1024.0}; //scaling for acceleration of gravity values |
scohennm | 5:be042e8c1756 | 42 | |
emilmont | 0:6149091f755d | 43 | MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
emilmont | 0:6149091f755d | 44 | // activate the peripheral |
emilmont | 0:6149091f755d | 45 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
samux | 1:d2630136d51e | 46 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 47 | } |
emilmont | 0:6149091f755d | 48 | |
emilmont | 0:6149091f755d | 49 | MMA8451Q::~MMA8451Q() { } |
emilmont | 0:6149091f755d | 50 | |
emilmont | 0:6149091f755d | 51 | uint8_t MMA8451Q::getWhoAmI() { |
emilmont | 0:6149091f755d | 52 | uint8_t who_am_i = 0; |
samux | 1:d2630136d51e | 53 | readRegs(REG_WHO_AM_I, &who_am_i, 1); |
emilmont | 0:6149091f755d | 54 | return who_am_i; |
emilmont | 0:6149091f755d | 55 | } |
mireyarod23 | 8:a515da97b396 | 56 | |
mireyarod23 | 8:a515da97b396 | 57 | void MMA8451Q::setStandbyMode() |
mireyarod23 | 8:a515da97b396 | 58 | { |
mireyarod23 | 8:a515da97b396 | 59 | #define ACTIVEMASK 0x01 |
mireyarod23 | 8:a515da97b396 | 60 | uint8_t registerData[1]; |
mireyarod23 | 8:a515da97b396 | 61 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; |
mireyarod23 | 8:a515da97b396 | 62 | |
mireyarod23 | 8:a515da97b396 | 63 | readRegs(REG_CTRL_REG_1, registerData, 1); |
mireyarod23 | 8:a515da97b396 | 64 | data[1] = registerData[0] & ~ACTIVEMASK; |
mireyarod23 | 8:a515da97b396 | 65 | writeRegs(data, NUM_DATA); //put in standby |
mireyarod23 | 8:a515da97b396 | 66 | } |
mireyarod23 | 8:a515da97b396 | 67 | |
mireyarod23 | 8:a515da97b396 | 68 | void MMA8451Q::setActiveMode() |
mireyarod23 | 8:a515da97b396 | 69 | { |
mireyarod23 | 8:a515da97b396 | 70 | #define ACTIVEMASK 0x01 |
mireyarod23 | 8:a515da97b396 | 71 | uint8_t registerData[1]; |
mireyarod23 | 8:a515da97b396 | 72 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; |
mireyarod23 | 8:a515da97b396 | 73 | |
mireyarod23 | 8:a515da97b396 | 74 | readRegs(REG_CTRL_REG_1, registerData, 1); |
mireyarod23 | 8:a515da97b396 | 75 | data[1] = registerData[0] | ACTIVEMASK; |
mireyarod23 | 8:a515da97b396 | 76 | writeRegs(data, NUM_DATA); |
mireyarod23 | 8:a515da97b396 | 77 | } |
mireyarod23 | 8:a515da97b396 | 78 | |
mireyarod23 | 8:a515da97b396 | 79 | //seGLimit obtains index of the bit that needs to be changed |
mireyarod23 | 8:a515da97b396 | 80 | void MMA8451Q::setGLimit(int gSelect) { |
scohennm | 5:be042e8c1756 | 81 | uint8_t data[2] = {REG_CTRL_REG_1, 0x00}; |
mireyarod23 | 8:a515da97b396 | 82 | gChosen = gSelect; |
mireyarod23 | 8:a515da97b396 | 83 | setStandbyMode(); |
mireyarod23 | 8:a515da97b396 | 84 | |
mireyarod23 | 8:a515da97b396 | 85 | data[ADDRESS_INDEX ] = XYZ_DATA_CFG; |
mireyarod23 | 8:a515da97b396 | 86 | data[DATA_INDEX] = gChosen; |
mireyarod23 | 8:a515da97b396 | 87 | writeRegs(data, 2); |
mireyarod23 | 8:a515da97b396 | 88 | setActiveMode(); |
scohennm | 5:be042e8c1756 | 89 | } |
emilmont | 0:6149091f755d | 90 | |
chris | 3:db7126dbd63f | 91 | float MMA8451Q::getAccX() { |
mireyarod23 | 8:a515da97b396 | 92 | return (float(getAccAxis(REG_OUT_X_MSB))/gScaling[gChosen]); |
emilmont | 0:6149091f755d | 93 | } |
emilmont | 0:6149091f755d | 94 | |
chris | 3:db7126dbd63f | 95 | float MMA8451Q::getAccY() { |
mireyarod23 | 8:a515da97b396 | 96 | return (float(getAccAxis(REG_OUT_Y_MSB))/gScaling[gChosen]); |
emilmont | 0:6149091f755d | 97 | } |
emilmont | 0:6149091f755d | 98 | |
chris | 3:db7126dbd63f | 99 | float MMA8451Q::getAccZ() { |
mireyarod23 | 8:a515da97b396 | 100 | return (float(getAccAxis(REG_OUT_Z_MSB))/gScaling[gChosen]); |
emilmont | 0:6149091f755d | 101 | } |
emilmont | 0:6149091f755d | 102 | |
chris | 3:db7126dbd63f | 103 | void MMA8451Q::getAccAllAxis(float * res) { |
emilmont | 0:6149091f755d | 104 | res[0] = getAccX(); |
emilmont | 0:6149091f755d | 105 | res[1] = getAccY(); |
emilmont | 0:6149091f755d | 106 | res[2] = getAccZ(); |
emilmont | 0:6149091f755d | 107 | } |
emilmont | 0:6149091f755d | 108 | |
emilmont | 0:6149091f755d | 109 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
emilmont | 0:6149091f755d | 110 | int16_t acc; |
emilmont | 0:6149091f755d | 111 | uint8_t res[2]; |
samux | 1:d2630136d51e | 112 | readRegs(addr, res, 2); |
emilmont | 0:6149091f755d | 113 | |
emilmont | 0:6149091f755d | 114 | acc = (res[0] << 6) | (res[1] >> 2); |
emilmont | 0:6149091f755d | 115 | if (acc > UINT14_MAX/2) |
emilmont | 0:6149091f755d | 116 | acc -= UINT14_MAX; |
emilmont | 0:6149091f755d | 117 | |
emilmont | 0:6149091f755d | 118 | return acc; |
emilmont | 0:6149091f755d | 119 | } |
emilmont | 0:6149091f755d | 120 | |
samux | 1:d2630136d51e | 121 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 122 | char t[1] = {addr}; |
emilmont | 0:6149091f755d | 123 | m_i2c.write(m_addr, t, 1, true); |
emilmont | 0:6149091f755d | 124 | m_i2c.read(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 125 | } |
emilmont | 0:6149091f755d | 126 | |
samux | 1:d2630136d51e | 127 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 128 | m_i2c.write(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 129 | } |