mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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