Project 1 V1 Submission
Fork of MMA8451Q8_v1 by
MMA8451Q8.h@9:84011f06d1ea, 2015-02-09 (annotated)
- Committer:
- bomalley
- Date:
- Mon Feb 09 06:57:21 2015 +0000
- Revision:
- 9:84011f06d1ea
- Parent:
- 8:993bb9e96a4b
HW 4.2.1 submission
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(); |
scohennm | 7:ed7e11d269f8 | 79 | |
scohennm | 7:ed7e11d269f8 | 80 | uint16_t igetAccX(); |
emilmont | 0:6149091f755d | 81 | |
samux | 1:d2630136d51e | 82 | /** |
emilmont | 0:6149091f755d | 83 | * Get Y axis acceleration |
emilmont | 0:6149091f755d | 84 | * |
samux | 1:d2630136d51e | 85 | * @returns Y axis acceleration |
emilmont | 0:6149091f755d | 86 | */ |
chris | 3:db7126dbd63f | 87 | float getAccY(); |
emilmont | 0:6149091f755d | 88 | |
samux | 1:d2630136d51e | 89 | /** |
emilmont | 0:6149091f755d | 90 | * Get Z axis acceleration |
emilmont | 0:6149091f755d | 91 | * |
samux | 1:d2630136d51e | 92 | * @returns Z axis acceleration |
emilmont | 0:6149091f755d | 93 | */ |
chris | 3:db7126dbd63f | 94 | float getAccZ(); |
emilmont | 0:6149091f755d | 95 | |
samux | 1:d2630136d51e | 96 | /** |
emilmont | 0:6149091f755d | 97 | * Get XYZ axis acceleration |
emilmont | 0:6149091f755d | 98 | * |
samux | 1:d2630136d51e | 99 | * @param res array where acceleration data will be stored |
emilmont | 0:6149091f755d | 100 | */ |
chris | 3:db7126dbd63f | 101 | void getAccAllAxis(float * res); |
emilmont | 0:6149091f755d | 102 | |
scohennm | 6:e0dc3cd59991 | 103 | int16_t getAccAxis(uint8_t addr); |
scohennm | 6:e0dc3cd59991 | 104 | |
samux | 1:d2630136d51e | 105 | I2C m_i2c; |
emilmont | 0:6149091f755d | 106 | int m_addr; |
scohennm | 5:5a7293378401 | 107 | int gChosen; |
scohennm | 5:5a7293378401 | 108 | |
scohennm | 5:5a7293378401 | 109 | /** |
scohennm | 5:5a7293378401 | 110 | Allow user to set max g's |
scohennm | 5:5a7293378401 | 111 | **/ |
scohennm | 5:5a7293378401 | 112 | void setGLimit(int gSelect); |
scohennm | 8:993bb9e96a4b | 113 | // added in .cpp file for tap configuration |
bomalley | 9:84011f06d1ea | 114 | void setPulseConfiguration(uint8_t latch, uint8_t axisSet); |
bomalley | 9:84011f06d1ea | 115 | void setRegisterInStandby(uint8_t regAddress,uint8_t regData); |
samux | 1:d2630136d51e | 116 | void readRegs(int addr, uint8_t * data, int len); |
samux | 1:d2630136d51e | 117 | void writeRegs(uint8_t * data, int len); |
scohennm | 5:5a7293378401 | 118 | |
scohennm | 5:5a7293378401 | 119 | private: |
scohennm | 6:e0dc3cd59991 | 120 | |
scohennm | 5:5a7293378401 | 121 | |
emilmont | 0:6149091f755d | 122 | }; |
emilmont | 0:6149091f755d | 123 | |
emilmont | 0:6149091f755d | 124 | #endif |