Double Tap Enable MMA8451Q library
Dependents: FRDM_MMA8451Q_DTAP_DEMO FRDM_TSI data_logger quadCommand2 ... more
Fork of MMA8451Q by
MMA8451Q.h@5:2d14600116fc, 2013-03-10 (annotated)
- Committer:
- JoKer
- Date:
- Sun Mar 10 04:34:03 2013 +0000
- Revision:
- 5:2d14600116fc
- Parent:
- 4:c4d879a39775
Added Double Tap Detection functionality
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 | #ifndef MMA8451Q_H |
emilmont | 0:6149091f755d | 20 | #define MMA8451Q_H |
emilmont | 0:6149091f755d | 21 | |
emilmont | 0:6149091f755d | 22 | #include "mbed.h" |
emilmont | 0:6149091f755d | 23 | |
samux | 1:d2630136d51e | 24 | /** |
samux | 1:d2630136d51e | 25 | * MMA8451Q accelerometer example |
samux | 2:a077541cbadc | 26 | * |
samux | 2:a077541cbadc | 27 | * @code |
samux | 1:d2630136d51e | 28 | * #include "mbed.h" |
samux | 1:d2630136d51e | 29 | * #include "MMA8451Q.h" |
chris | 3:db7126dbd63f | 30 | * |
samux | 1:d2630136d51e | 31 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
chris | 3:db7126dbd63f | 32 | * |
chris | 4:c4d879a39775 | 33 | * int main(void) { |
chris | 4:c4d879a39775 | 34 | * |
chris | 3:db7126dbd63f | 35 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
chris | 3:db7126dbd63f | 36 | * PwmOut rled(LED_RED); |
chris | 3:db7126dbd63f | 37 | * PwmOut gled(LED_GREEN); |
chris | 3:db7126dbd63f | 38 | * PwmOut bled(LED_BLUE); |
chris | 3:db7126dbd63f | 39 | * |
chris | 3:db7126dbd63f | 40 | * while (true) { |
chris | 3:db7126dbd63f | 41 | * rled = 1.0 - abs(acc.getAccX()); |
chris | 3:db7126dbd63f | 42 | * gled = 1.0 - abs(acc.getAccY()); |
chris | 3:db7126dbd63f | 43 | * bled = 1.0 - abs(acc.getAccZ()); |
chris | 3:db7126dbd63f | 44 | * wait(0.1); |
chris | 3:db7126dbd63f | 45 | * } |
samux | 1:d2630136d51e | 46 | * } |
samux | 2:a077541cbadc | 47 | * @endcode |
samux | 1:d2630136d51e | 48 | */ |
emilmont | 0:6149091f755d | 49 | class MMA8451Q |
emilmont | 0:6149091f755d | 50 | { |
emilmont | 0:6149091f755d | 51 | public: |
samux | 1:d2630136d51e | 52 | /** |
samux | 1:d2630136d51e | 53 | * MMA8451Q constructor |
samux | 1:d2630136d51e | 54 | * |
samux | 1:d2630136d51e | 55 | * @param sda SDA pin |
samux | 1:d2630136d51e | 56 | * @param sdl SCL pin |
samux | 1:d2630136d51e | 57 | * @param addr addr of the I2C peripheral |
emilmont | 0:6149091f755d | 58 | */ |
emilmont | 0:6149091f755d | 59 | MMA8451Q(PinName sda, PinName scl, int addr); |
emilmont | 0:6149091f755d | 60 | |
samux | 1:d2630136d51e | 61 | /** |
samux | 1:d2630136d51e | 62 | * MMA8451Q destructor |
emilmont | 0:6149091f755d | 63 | */ |
emilmont | 0:6149091f755d | 64 | ~MMA8451Q(); |
emilmont | 0:6149091f755d | 65 | |
samux | 1:d2630136d51e | 66 | /** |
emilmont | 0:6149091f755d | 67 | * Get the value of the WHO_AM_I register |
emilmont | 0:6149091f755d | 68 | * |
samux | 1:d2630136d51e | 69 | * @returns WHO_AM_I value |
emilmont | 0:6149091f755d | 70 | */ |
emilmont | 0:6149091f755d | 71 | uint8_t getWhoAmI(); |
emilmont | 0:6149091f755d | 72 | |
samux | 1:d2630136d51e | 73 | /** |
emilmont | 0:6149091f755d | 74 | * Get X axis acceleration |
emilmont | 0:6149091f755d | 75 | * |
samux | 1:d2630136d51e | 76 | * @returns X axis acceleration |
emilmont | 0:6149091f755d | 77 | */ |
chris | 3:db7126dbd63f | 78 | float getAccX(); |
emilmont | 0:6149091f755d | 79 | |
samux | 1:d2630136d51e | 80 | /** |
emilmont | 0:6149091f755d | 81 | * Get Y axis acceleration |
emilmont | 0:6149091f755d | 82 | * |
samux | 1:d2630136d51e | 83 | * @returns Y axis acceleration |
emilmont | 0:6149091f755d | 84 | */ |
chris | 3:db7126dbd63f | 85 | float getAccY(); |
emilmont | 0:6149091f755d | 86 | |
samux | 1:d2630136d51e | 87 | /** |
emilmont | 0:6149091f755d | 88 | * Get Z axis acceleration |
emilmont | 0:6149091f755d | 89 | * |
samux | 1:d2630136d51e | 90 | * @returns Z axis acceleration |
emilmont | 0:6149091f755d | 91 | */ |
chris | 3:db7126dbd63f | 92 | float getAccZ(); |
emilmont | 0:6149091f755d | 93 | |
samux | 1:d2630136d51e | 94 | /** |
emilmont | 0:6149091f755d | 95 | * Get XYZ axis acceleration |
emilmont | 0:6149091f755d | 96 | * |
samux | 1:d2630136d51e | 97 | * @param res array where acceleration data will be stored |
emilmont | 0:6149091f755d | 98 | */ |
chris | 3:db7126dbd63f | 99 | void getAccAllAxis(float * res); |
JoKer | 5:2d14600116fc | 100 | |
JoKer | 5:2d14600116fc | 101 | /** JK |
JoKer | 5:2d14600116fc | 102 | * Setup Double Tap detection |
JoKer | 5:2d14600116fc | 103 | |
JoKer | 5:2d14600116fc | 104 | |
JoKer | 5:2d14600116fc | 105 | Example: |
JoKer | 5:2d14600116fc | 106 | |
JoKer | 5:2d14600116fc | 107 | #include "mbed.h" |
JoKer | 5:2d14600116fc | 108 | #include "MMA8451Q.h" |
JoKer | 5:2d14600116fc | 109 | |
JoKer | 5:2d14600116fc | 110 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
JoKer | 5:2d14600116fc | 111 | #define ON 0 |
JoKer | 5:2d14600116fc | 112 | #define OFF !ON |
JoKer | 5:2d14600116fc | 113 | |
JoKer | 5:2d14600116fc | 114 | //Setup the interrupts for the MMA8451Q |
JoKer | 5:2d14600116fc | 115 | InterruptIn accInt1(PTA14); |
JoKer | 5:2d14600116fc | 116 | InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer |
JoKer | 5:2d14600116fc | 117 | |
JoKer | 5:2d14600116fc | 118 | uint8_t togstat=0;//Led status |
JoKer | 5:2d14600116fc | 119 | DigitalOut bled(LED_BLUE); |
JoKer | 5:2d14600116fc | 120 | |
JoKer | 5:2d14600116fc | 121 | |
JoKer | 5:2d14600116fc | 122 | void tapTrue(void){//ISR |
JoKer | 5:2d14600116fc | 123 | if(togstat == 0){ |
JoKer | 5:2d14600116fc | 124 | togstat = 1; |
JoKer | 5:2d14600116fc | 125 | bled=ON; |
JoKer | 5:2d14600116fc | 126 | } else { |
JoKer | 5:2d14600116fc | 127 | togstat = 0; |
JoKer | 5:2d14600116fc | 128 | bled=OFF; |
JoKer | 5:2d14600116fc | 129 | } |
JoKer | 5:2d14600116fc | 130 | |
JoKer | 5:2d14600116fc | 131 | } |
JoKer | 5:2d14600116fc | 132 | |
JoKer | 5:2d14600116fc | 133 | |
JoKer | 5:2d14600116fc | 134 | int main(void) { |
JoKer | 5:2d14600116fc | 135 | |
JoKer | 5:2d14600116fc | 136 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance |
JoKer | 5:2d14600116fc | 137 | |
JoKer | 5:2d14600116fc | 138 | acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap |
JoKer | 5:2d14600116fc | 139 | accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14 |
JoKer | 5:2d14600116fc | 140 | |
JoKer | 5:2d14600116fc | 141 | while (true) { |
JoKer | 5:2d14600116fc | 142 | //Interrupt driven so nothing in main loop |
JoKer | 5:2d14600116fc | 143 | } |
JoKer | 5:2d14600116fc | 144 | } |
JoKer | 5:2d14600116fc | 145 | |
JoKer | 5:2d14600116fc | 146 | |
JoKer | 5:2d14600116fc | 147 | */ |
JoKer | 5:2d14600116fc | 148 | void setDoubleTap(void); |
emilmont | 0:6149091f755d | 149 | |
emilmont | 0:6149091f755d | 150 | private: |
samux | 1:d2630136d51e | 151 | I2C m_i2c; |
emilmont | 0:6149091f755d | 152 | int m_addr; |
samux | 1:d2630136d51e | 153 | void readRegs(int addr, uint8_t * data, int len); |
samux | 1:d2630136d51e | 154 | void writeRegs(uint8_t * data, int len); |
emilmont | 0:6149091f755d | 155 | int16_t getAccAxis(uint8_t addr); |
emilmont | 0:6149091f755d | 156 | |
emilmont | 0:6149091f755d | 157 | }; |
emilmont | 0:6149091f755d | 158 | |
emilmont | 0:6149091f755d | 159 | #endif |