Extended driver to be able to configure the accelerometer for tap detection, orientation detection, and data ready interrupts.
Dependents: FRDM-KL25Z_secret_knock bluetooth_robo01 robo_01
Fork of MMA8451Q by
MMA8451Q.h@8:018aea85c0db, 2013-03-15 (annotated)
- Committer:
- maclobdell
- Date:
- Fri Mar 15 18:49:32 2013 +0000
- Revision:
- 8:018aea85c0db
- Parent:
- 5:c43505b5bc31
updated header information
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 | |
maclobdell | 8:018aea85c0db | 19 | /* Driver extended by Mac Lobdell */ |
maclobdell | 8:018aea85c0db | 20 | /* Added initialization of many features including tap, orientation, and data ready interrupts */ |
maclobdell | 8:018aea85c0db | 21 | /* Added capability to check interrupt source, tap type, and orientation */ |
maclobdell | 8:018aea85c0db | 22 | /* See MMA8451Q.h for configuration to enable/disable different features */ |
maclobdell | 8:018aea85c0db | 23 | |
emilmont | 0:6149091f755d | 24 | #ifndef MMA8451Q_H |
emilmont | 0:6149091f755d | 25 | #define MMA8451Q_H |
emilmont | 0:6149091f755d | 26 | |
emilmont | 0:6149091f755d | 27 | #include "mbed.h" |
emilmont | 0:6149091f755d | 28 | |
maclobdell | 5:c43505b5bc31 | 29 | //just supporting Z taps in the code, not X, Y yet |
maclobdell | 5:c43505b5bc31 | 30 | #define MMA8451Q_ENABLE_DOUBLE_Z_TAP 0 |
maclobdell | 5:c43505b5bc31 | 31 | #define MMA8451Q_ENABLE_SINGLE_Z_TAP 1 |
maclobdell | 5:c43505b5bc31 | 32 | #define MMA8451Q_ENABLE_ORIENTATION 0 |
maclobdell | 5:c43505b5bc31 | 33 | #define MMA8451Q_ENABLE_DATAREADY 0 |
maclobdell | 5:c43505b5bc31 | 34 | |
samux | 1:d2630136d51e | 35 | /** |
samux | 1:d2630136d51e | 36 | * MMA8451Q accelerometer example |
samux | 2:a077541cbadc | 37 | * |
samux | 2:a077541cbadc | 38 | * @code |
samux | 1:d2630136d51e | 39 | * #include "mbed.h" |
samux | 1:d2630136d51e | 40 | * #include "MMA8451Q.h" |
chris | 3:db7126dbd63f | 41 | * |
samux | 1:d2630136d51e | 42 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
chris | 3:db7126dbd63f | 43 | * |
chris | 4:c4d879a39775 | 44 | * int main(void) { |
chris | 4:c4d879a39775 | 45 | * |
chris | 3:db7126dbd63f | 46 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
chris | 3:db7126dbd63f | 47 | * PwmOut rled(LED_RED); |
chris | 3:db7126dbd63f | 48 | * PwmOut gled(LED_GREEN); |
chris | 3:db7126dbd63f | 49 | * PwmOut bled(LED_BLUE); |
chris | 3:db7126dbd63f | 50 | * |
chris | 3:db7126dbd63f | 51 | * while (true) { |
chris | 3:db7126dbd63f | 52 | * rled = 1.0 - abs(acc.getAccX()); |
chris | 3:db7126dbd63f | 53 | * gled = 1.0 - abs(acc.getAccY()); |
chris | 3:db7126dbd63f | 54 | * bled = 1.0 - abs(acc.getAccZ()); |
chris | 3:db7126dbd63f | 55 | * wait(0.1); |
chris | 3:db7126dbd63f | 56 | * } |
samux | 1:d2630136d51e | 57 | * } |
samux | 2:a077541cbadc | 58 | * @endcode |
samux | 1:d2630136d51e | 59 | */ |
emilmont | 0:6149091f755d | 60 | class MMA8451Q |
emilmont | 0:6149091f755d | 61 | { |
emilmont | 0:6149091f755d | 62 | public: |
samux | 1:d2630136d51e | 63 | /** |
samux | 1:d2630136d51e | 64 | * MMA8451Q constructor |
samux | 1:d2630136d51e | 65 | * |
samux | 1:d2630136d51e | 66 | * @param sda SDA pin |
samux | 1:d2630136d51e | 67 | * @param sdl SCL pin |
samux | 1:d2630136d51e | 68 | * @param addr addr of the I2C peripheral |
emilmont | 0:6149091f755d | 69 | */ |
emilmont | 0:6149091f755d | 70 | MMA8451Q(PinName sda, PinName scl, int addr); |
emilmont | 0:6149091f755d | 71 | |
samux | 1:d2630136d51e | 72 | /** |
samux | 1:d2630136d51e | 73 | * MMA8451Q destructor |
emilmont | 0:6149091f755d | 74 | */ |
emilmont | 0:6149091f755d | 75 | ~MMA8451Q(); |
emilmont | 0:6149091f755d | 76 | |
samux | 1:d2630136d51e | 77 | /** |
emilmont | 0:6149091f755d | 78 | * Get the value of the WHO_AM_I register |
emilmont | 0:6149091f755d | 79 | * |
samux | 1:d2630136d51e | 80 | * @returns WHO_AM_I value |
emilmont | 0:6149091f755d | 81 | */ |
emilmont | 0:6149091f755d | 82 | uint8_t getWhoAmI(); |
emilmont | 0:6149091f755d | 83 | |
samux | 1:d2630136d51e | 84 | /** |
emilmont | 0:6149091f755d | 85 | * Get X axis acceleration |
emilmont | 0:6149091f755d | 86 | * |
samux | 1:d2630136d51e | 87 | * @returns X axis acceleration |
emilmont | 0:6149091f755d | 88 | */ |
chris | 3:db7126dbd63f | 89 | float getAccX(); |
emilmont | 0:6149091f755d | 90 | |
samux | 1:d2630136d51e | 91 | /** |
emilmont | 0:6149091f755d | 92 | * Get Y axis acceleration |
emilmont | 0:6149091f755d | 93 | * |
samux | 1:d2630136d51e | 94 | * @returns Y axis acceleration |
emilmont | 0:6149091f755d | 95 | */ |
chris | 3:db7126dbd63f | 96 | float getAccY(); |
emilmont | 0:6149091f755d | 97 | |
samux | 1:d2630136d51e | 98 | /** |
emilmont | 0:6149091f755d | 99 | * Get Z axis acceleration |
emilmont | 0:6149091f755d | 100 | * |
samux | 1:d2630136d51e | 101 | * @returns Z axis acceleration |
emilmont | 0:6149091f755d | 102 | */ |
chris | 3:db7126dbd63f | 103 | float getAccZ(); |
emilmont | 0:6149091f755d | 104 | |
samux | 1:d2630136d51e | 105 | /** |
emilmont | 0:6149091f755d | 106 | * Get XYZ axis acceleration |
emilmont | 0:6149091f755d | 107 | * |
samux | 1:d2630136d51e | 108 | * @param res array where acceleration data will be stored |
emilmont | 0:6149091f755d | 109 | */ |
chris | 3:db7126dbd63f | 110 | void getAccAllAxis(float * res); |
emilmont | 0:6149091f755d | 111 | |
maclobdell | 5:c43505b5bc31 | 112 | uint8_t direction(void); |
maclobdell | 5:c43505b5bc31 | 113 | uint8_t tapSource(void); |
maclobdell | 5:c43505b5bc31 | 114 | uint8_t intSource(void); |
maclobdell | 5:c43505b5bc31 | 115 | |
emilmont | 0:6149091f755d | 116 | private: |
samux | 1:d2630136d51e | 117 | I2C m_i2c; |
emilmont | 0:6149091f755d | 118 | int m_addr; |
samux | 1:d2630136d51e | 119 | void readRegs(int addr, uint8_t * data, int len); |
samux | 1:d2630136d51e | 120 | void writeRegs(uint8_t * data, int len); |
emilmont | 0:6149091f755d | 121 | int16_t getAccAxis(uint8_t addr); |
maclobdell | 5:c43505b5bc31 | 122 | void writeRegister(uint8_t reg, uint8_t data); |
maclobdell | 5:c43505b5bc31 | 123 | uint8_t readRegister(uint8_t reg); |
maclobdell | 5:c43505b5bc31 | 124 | void modeStandby(void); |
maclobdell | 5:c43505b5bc31 | 125 | void modeActive(void); |
emilmont | 0:6149091f755d | 126 | }; |
emilmont | 0:6149091f755d | 127 | #endif |