HW 4.2.1 submission
Fork of MMA8451Q8b by
MMA8451Q.h@1:d2630136d51e, 2012-10-11 (annotated)
- Committer:
- samux
- Date:
- Thu Oct 11 14:26:22 2012 +0000
- Revision:
- 1:d2630136d51e
- Parent:
- 0:6149091f755d
- Child:
- 2:a077541cbadc
modify doxygen comments/add license
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 | 1:d2630136d51e | 26 | * #include "mbed.h" |
samux | 1:d2630136d51e | 27 | * #include "MMA8451Q.h" |
samux | 1:d2630136d51e | 28 | * |
samux | 1:d2630136d51e | 29 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
samux | 1:d2630136d51e | 30 | * |
samux | 1:d2630136d51e | 31 | * int main(void) { |
samux | 1:d2630136d51e | 32 | * DigitalOut led(LED_GREEN); |
samux | 1:d2630136d51e | 33 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
samux | 1:d2630136d51e | 34 | * printf("WHO AM I: 0x%2X\r\n", acc.getWhoAmI()); |
samux | 1:d2630136d51e | 35 | * |
samux | 1:d2630136d51e | 36 | * while (true) { |
samux | 1:d2630136d51e | 37 | * printf("-----------\r\n"); |
samux | 1:d2630136d51e | 38 | * printf("acc_x: %d\r\n", acc.getAccX()); |
samux | 1:d2630136d51e | 39 | * printf("acc_y: %d\r\n", acc.getAccY()); |
samux | 1:d2630136d51e | 40 | * printf("acc_z: %d\r\n", acc.getAccZ()); |
samux | 1:d2630136d51e | 41 | * |
samux | 1:d2630136d51e | 42 | * wait(1); |
samux | 1:d2630136d51e | 43 | * led = !led; |
samux | 1:d2630136d51e | 44 | * } |
samux | 1:d2630136d51e | 45 | * } |
samux | 1:d2630136d51e | 46 | */ |
emilmont | 0:6149091f755d | 47 | class MMA8451Q |
emilmont | 0:6149091f755d | 48 | { |
emilmont | 0:6149091f755d | 49 | public: |
samux | 1:d2630136d51e | 50 | /** |
samux | 1:d2630136d51e | 51 | * MMA8451Q constructor |
samux | 1:d2630136d51e | 52 | * |
samux | 1:d2630136d51e | 53 | * @param sda SDA pin |
samux | 1:d2630136d51e | 54 | * @param sdl SCL pin |
samux | 1:d2630136d51e | 55 | * @param addr addr of the I2C peripheral |
emilmont | 0:6149091f755d | 56 | */ |
emilmont | 0:6149091f755d | 57 | MMA8451Q(PinName sda, PinName scl, int addr); |
emilmont | 0:6149091f755d | 58 | |
samux | 1:d2630136d51e | 59 | /** |
samux | 1:d2630136d51e | 60 | * MMA8451Q destructor |
emilmont | 0:6149091f755d | 61 | */ |
emilmont | 0:6149091f755d | 62 | ~MMA8451Q(); |
emilmont | 0:6149091f755d | 63 | |
samux | 1:d2630136d51e | 64 | /** |
emilmont | 0:6149091f755d | 65 | * Get the value of the WHO_AM_I register |
emilmont | 0:6149091f755d | 66 | * |
samux | 1:d2630136d51e | 67 | * @returns WHO_AM_I value |
emilmont | 0:6149091f755d | 68 | */ |
emilmont | 0:6149091f755d | 69 | uint8_t getWhoAmI(); |
emilmont | 0:6149091f755d | 70 | |
samux | 1:d2630136d51e | 71 | /** |
emilmont | 0:6149091f755d | 72 | * Get X axis acceleration |
emilmont | 0:6149091f755d | 73 | * |
samux | 1:d2630136d51e | 74 | * @returns X axis acceleration |
emilmont | 0:6149091f755d | 75 | */ |
emilmont | 0:6149091f755d | 76 | int16_t getAccX(); |
emilmont | 0:6149091f755d | 77 | |
samux | 1:d2630136d51e | 78 | /** |
emilmont | 0:6149091f755d | 79 | * Get Y axis acceleration |
emilmont | 0:6149091f755d | 80 | * |
samux | 1:d2630136d51e | 81 | * @returns Y axis acceleration |
emilmont | 0:6149091f755d | 82 | */ |
emilmont | 0:6149091f755d | 83 | int16_t getAccY(); |
emilmont | 0:6149091f755d | 84 | |
samux | 1:d2630136d51e | 85 | /** |
emilmont | 0:6149091f755d | 86 | * Get Z axis acceleration |
emilmont | 0:6149091f755d | 87 | * |
samux | 1:d2630136d51e | 88 | * @returns Z axis acceleration |
emilmont | 0:6149091f755d | 89 | */ |
emilmont | 0:6149091f755d | 90 | int16_t getAccZ(); |
emilmont | 0:6149091f755d | 91 | |
samux | 1:d2630136d51e | 92 | /** |
emilmont | 0:6149091f755d | 93 | * Get XYZ axis acceleration |
emilmont | 0:6149091f755d | 94 | * |
samux | 1:d2630136d51e | 95 | * @param res array where acceleration data will be stored |
emilmont | 0:6149091f755d | 96 | */ |
samux | 1:d2630136d51e | 97 | void getAccAllAxis(int16_t * res); |
emilmont | 0:6149091f755d | 98 | |
emilmont | 0:6149091f755d | 99 | private: |
samux | 1:d2630136d51e | 100 | I2C m_i2c; |
emilmont | 0:6149091f755d | 101 | int m_addr; |
samux | 1:d2630136d51e | 102 | void readRegs(int addr, uint8_t * data, int len); |
samux | 1:d2630136d51e | 103 | void writeRegs(uint8_t * data, int len); |
emilmont | 0:6149091f755d | 104 | int16_t getAccAxis(uint8_t addr); |
emilmont | 0:6149091f755d | 105 | |
emilmont | 0:6149091f755d | 106 | }; |
emilmont | 0:6149091f755d | 107 | |
emilmont | 0:6149091f755d | 108 | #endif |