Preliminary main mbed library for nexpaq development
libraries/tests/peripherals/MMA8451Q/MMA8451Q.h@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
nexpaq | 0:6c56fb4bc5f0 | 2 | * |
nexpaq | 0:6c56fb4bc5f0 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
nexpaq | 0:6c56fb4bc5f0 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
nexpaq | 0:6c56fb4bc5f0 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
nexpaq | 0:6c56fb4bc5f0 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
nexpaq | 0:6c56fb4bc5f0 | 7 | * Software is furnished to do so, subject to the following conditions: |
nexpaq | 0:6c56fb4bc5f0 | 8 | * |
nexpaq | 0:6c56fb4bc5f0 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
nexpaq | 0:6c56fb4bc5f0 | 10 | * substantial portions of the Software. |
nexpaq | 0:6c56fb4bc5f0 | 11 | * |
nexpaq | 0:6c56fb4bc5f0 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
nexpaq | 0:6c56fb4bc5f0 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
nexpaq | 0:6c56fb4bc5f0 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
nexpaq | 0:6c56fb4bc5f0 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
nexpaq | 0:6c56fb4bc5f0 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
nexpaq | 0:6c56fb4bc5f0 | 17 | */ |
nexpaq | 0:6c56fb4bc5f0 | 18 | |
nexpaq | 0:6c56fb4bc5f0 | 19 | #ifndef MMA8451Q_H |
nexpaq | 0:6c56fb4bc5f0 | 20 | #define MMA8451Q_H |
nexpaq | 0:6c56fb4bc5f0 | 21 | |
nexpaq | 0:6c56fb4bc5f0 | 22 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 23 | |
nexpaq | 0:6c56fb4bc5f0 | 24 | /** |
nexpaq | 0:6c56fb4bc5f0 | 25 | * MMA8451Q accelerometer example |
nexpaq | 0:6c56fb4bc5f0 | 26 | * #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 27 | * #include "MMA8451Q.h" |
nexpaq | 0:6c56fb4bc5f0 | 28 | * |
nexpaq | 0:6c56fb4bc5f0 | 29 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
nexpaq | 0:6c56fb4bc5f0 | 30 | * |
nexpaq | 0:6c56fb4bc5f0 | 31 | * int main(void) { |
nexpaq | 0:6c56fb4bc5f0 | 32 | * DigitalOut led(LED_GREEN); |
nexpaq | 0:6c56fb4bc5f0 | 33 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
nexpaq | 0:6c56fb4bc5f0 | 34 | * printf("WHO AM I: 0x%2X\r\n", acc.getWhoAmI()); |
nexpaq | 0:6c56fb4bc5f0 | 35 | * |
nexpaq | 0:6c56fb4bc5f0 | 36 | * while (true) { |
nexpaq | 0:6c56fb4bc5f0 | 37 | * printf("-----------\r\n"); |
nexpaq | 0:6c56fb4bc5f0 | 38 | * printf("acc_x: %d\r\n", acc.getAccX()); |
nexpaq | 0:6c56fb4bc5f0 | 39 | * printf("acc_y: %d\r\n", acc.getAccY()); |
nexpaq | 0:6c56fb4bc5f0 | 40 | * printf("acc_z: %d\r\n", acc.getAccZ()); |
nexpaq | 0:6c56fb4bc5f0 | 41 | * |
nexpaq | 0:6c56fb4bc5f0 | 42 | * wait(1); |
nexpaq | 0:6c56fb4bc5f0 | 43 | * led = !led; |
nexpaq | 0:6c56fb4bc5f0 | 44 | * } |
nexpaq | 0:6c56fb4bc5f0 | 45 | * } |
nexpaq | 0:6c56fb4bc5f0 | 46 | */ |
nexpaq | 0:6c56fb4bc5f0 | 47 | class MMA8451Q |
nexpaq | 0:6c56fb4bc5f0 | 48 | { |
nexpaq | 0:6c56fb4bc5f0 | 49 | public: |
nexpaq | 0:6c56fb4bc5f0 | 50 | /** |
nexpaq | 0:6c56fb4bc5f0 | 51 | * MMA8451Q constructor |
nexpaq | 0:6c56fb4bc5f0 | 52 | * |
nexpaq | 0:6c56fb4bc5f0 | 53 | * @param sda SDA pin |
nexpaq | 0:6c56fb4bc5f0 | 54 | * @param sdl SCL pin |
nexpaq | 0:6c56fb4bc5f0 | 55 | * @param addr addr of the I2C peripheral |
nexpaq | 0:6c56fb4bc5f0 | 56 | */ |
nexpaq | 0:6c56fb4bc5f0 | 57 | MMA8451Q(PinName sda, PinName scl, int addr); |
nexpaq | 0:6c56fb4bc5f0 | 58 | |
nexpaq | 0:6c56fb4bc5f0 | 59 | /** |
nexpaq | 0:6c56fb4bc5f0 | 60 | * MMA8451Q destructor |
nexpaq | 0:6c56fb4bc5f0 | 61 | */ |
nexpaq | 0:6c56fb4bc5f0 | 62 | ~MMA8451Q(); |
nexpaq | 0:6c56fb4bc5f0 | 63 | |
nexpaq | 0:6c56fb4bc5f0 | 64 | /** |
nexpaq | 0:6c56fb4bc5f0 | 65 | * Get the value of the WHO_AM_I register |
nexpaq | 0:6c56fb4bc5f0 | 66 | * |
nexpaq | 0:6c56fb4bc5f0 | 67 | * @returns WHO_AM_I value |
nexpaq | 0:6c56fb4bc5f0 | 68 | */ |
nexpaq | 0:6c56fb4bc5f0 | 69 | uint8_t getWhoAmI(); |
nexpaq | 0:6c56fb4bc5f0 | 70 | |
nexpaq | 0:6c56fb4bc5f0 | 71 | /** |
nexpaq | 0:6c56fb4bc5f0 | 72 | * Get X axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 73 | * |
nexpaq | 0:6c56fb4bc5f0 | 74 | * @returns X axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 75 | */ |
nexpaq | 0:6c56fb4bc5f0 | 76 | int16_t getAccX(); |
nexpaq | 0:6c56fb4bc5f0 | 77 | |
nexpaq | 0:6c56fb4bc5f0 | 78 | /** |
nexpaq | 0:6c56fb4bc5f0 | 79 | * Get Y axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 80 | * |
nexpaq | 0:6c56fb4bc5f0 | 81 | * @returns Y axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 82 | */ |
nexpaq | 0:6c56fb4bc5f0 | 83 | int16_t getAccY(); |
nexpaq | 0:6c56fb4bc5f0 | 84 | |
nexpaq | 0:6c56fb4bc5f0 | 85 | /** |
nexpaq | 0:6c56fb4bc5f0 | 86 | * Get Z axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 87 | * |
nexpaq | 0:6c56fb4bc5f0 | 88 | * @returns Z axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 89 | */ |
nexpaq | 0:6c56fb4bc5f0 | 90 | int16_t getAccZ(); |
nexpaq | 0:6c56fb4bc5f0 | 91 | |
nexpaq | 0:6c56fb4bc5f0 | 92 | /** |
nexpaq | 0:6c56fb4bc5f0 | 93 | * Get XYZ axis acceleration |
nexpaq | 0:6c56fb4bc5f0 | 94 | * |
nexpaq | 0:6c56fb4bc5f0 | 95 | * @param res array where acceleration data will be stored |
nexpaq | 0:6c56fb4bc5f0 | 96 | */ |
nexpaq | 0:6c56fb4bc5f0 | 97 | void getAccAllAxis(int16_t * res); |
nexpaq | 0:6c56fb4bc5f0 | 98 | |
nexpaq | 0:6c56fb4bc5f0 | 99 | private: |
nexpaq | 0:6c56fb4bc5f0 | 100 | I2C m_i2c; |
nexpaq | 0:6c56fb4bc5f0 | 101 | int m_addr; |
nexpaq | 0:6c56fb4bc5f0 | 102 | void readRegs(int addr, uint8_t * data, int len); |
nexpaq | 0:6c56fb4bc5f0 | 103 | void writeRegs(uint8_t * data, int len); |
nexpaq | 0:6c56fb4bc5f0 | 104 | int16_t getAccAxis(uint8_t addr); |
nexpaq | 0:6c56fb4bc5f0 | 105 | |
nexpaq | 0:6c56fb4bc5f0 | 106 | }; |
nexpaq | 0:6c56fb4bc5f0 | 107 | |
nexpaq | 0:6c56fb4bc5f0 | 108 | #endif |