I got the structure of the code but did not get every details right

Committer:
WilliamMarshQMUL
Date:
Thu Feb 27 10:07:17 2020 +0000
Revision:
5:392ac6e02d80
Library converted to folder

Who changed what in which revision?

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