samux MMA8451Q accelerometer library
Dependents: AirMouse FRDM_LCD4884 TextLCD_HelloWorld FRDM_MMA8451Q ... more
MMA8451Q.cpp@4:c4d879a39775, 2012-10-12 (annotated)
- Committer:
- chris
- Date:
- Fri Oct 12 11:35:07 2012 +0000
- Revision:
- 4:c4d879a39775
- Parent:
- 3:db7126dbd63f
Pushed objec declaration into main
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" |
emilmont | 0:6149091f755d | 20 | |
samux | 1:d2630136d51e | 21 | #define REG_WHO_AM_I 0x0D |
samux | 1:d2630136d51e | 22 | #define REG_CTRL_REG_1 0x2A |
emilmont | 0:6149091f755d | 23 | #define REG_OUT_X_MSB 0x01 |
emilmont | 0:6149091f755d | 24 | #define REG_OUT_Y_MSB 0x03 |
emilmont | 0:6149091f755d | 25 | #define REG_OUT_Z_MSB 0x05 |
emilmont | 0:6149091f755d | 26 | |
samux | 1:d2630136d51e | 27 | #define UINT14_MAX 16383 |
emilmont | 0:6149091f755d | 28 | |
emilmont | 0:6149091f755d | 29 | MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
emilmont | 0:6149091f755d | 30 | // activate the peripheral |
emilmont | 0:6149091f755d | 31 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
samux | 1:d2630136d51e | 32 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 33 | } |
emilmont | 0:6149091f755d | 34 | |
emilmont | 0:6149091f755d | 35 | MMA8451Q::~MMA8451Q() { } |
emilmont | 0:6149091f755d | 36 | |
emilmont | 0:6149091f755d | 37 | uint8_t MMA8451Q::getWhoAmI() { |
emilmont | 0:6149091f755d | 38 | uint8_t who_am_i = 0; |
samux | 1:d2630136d51e | 39 | readRegs(REG_WHO_AM_I, &who_am_i, 1); |
emilmont | 0:6149091f755d | 40 | return who_am_i; |
emilmont | 0:6149091f755d | 41 | } |
emilmont | 0:6149091f755d | 42 | |
chris | 3:db7126dbd63f | 43 | float MMA8451Q::getAccX() { |
chris | 3:db7126dbd63f | 44 | return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); |
emilmont | 0:6149091f755d | 45 | } |
emilmont | 0:6149091f755d | 46 | |
chris | 3:db7126dbd63f | 47 | float MMA8451Q::getAccY() { |
chris | 3:db7126dbd63f | 48 | return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); |
emilmont | 0:6149091f755d | 49 | } |
emilmont | 0:6149091f755d | 50 | |
chris | 3:db7126dbd63f | 51 | float MMA8451Q::getAccZ() { |
chris | 3:db7126dbd63f | 52 | return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); |
emilmont | 0:6149091f755d | 53 | } |
emilmont | 0:6149091f755d | 54 | |
chris | 3:db7126dbd63f | 55 | void MMA8451Q::getAccAllAxis(float * res) { |
emilmont | 0:6149091f755d | 56 | res[0] = getAccX(); |
emilmont | 0:6149091f755d | 57 | res[1] = getAccY(); |
emilmont | 0:6149091f755d | 58 | res[2] = getAccZ(); |
emilmont | 0:6149091f755d | 59 | } |
emilmont | 0:6149091f755d | 60 | |
emilmont | 0:6149091f755d | 61 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
emilmont | 0:6149091f755d | 62 | int16_t acc; |
emilmont | 0:6149091f755d | 63 | uint8_t res[2]; |
samux | 1:d2630136d51e | 64 | readRegs(addr, res, 2); |
emilmont | 0:6149091f755d | 65 | |
emilmont | 0:6149091f755d | 66 | acc = (res[0] << 6) | (res[1] >> 2); |
emilmont | 0:6149091f755d | 67 | if (acc > UINT14_MAX/2) |
emilmont | 0:6149091f755d | 68 | acc -= UINT14_MAX; |
emilmont | 0:6149091f755d | 69 | |
emilmont | 0:6149091f755d | 70 | return acc; |
emilmont | 0:6149091f755d | 71 | } |
emilmont | 0:6149091f755d | 72 | |
samux | 1:d2630136d51e | 73 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 74 | char t[1] = {addr}; |
emilmont | 0:6149091f755d | 75 | m_i2c.write(m_addr, t, 1, true); |
emilmont | 0:6149091f755d | 76 | m_i2c.read(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 77 | } |
emilmont | 0:6149091f755d | 78 | |
samux | 1:d2630136d51e | 79 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 80 | m_i2c.write(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 81 | } |