A basic library for the FXOS8700Q combination accelerometer / magnetometer

Dependencies:   MotionSensor

Dependents:   K64F_eCompass_LCD Hello_FXOS8700Q rtos_compass K64F_eCompass ... more

This library supports the 6 axis combination Accelerometer / Magnetometer. Functions are provided to retrieve data in raw 16 bit signed integers or unit converted G's and micro-teslas

Committer:
JimCarver
Date:
Sat Apr 19 00:16:49 2014 +0000
Revision:
4:be6abf9f2d59
Parent:
3:eb1271ef90bc
Child:
5:c4176a12f7d5
Added unit converted magnetometer support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 3:eb1271ef90bc 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
JimCarver 3:eb1271ef90bc 2 *
JimCarver 3:eb1271ef90bc 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
JimCarver 3:eb1271ef90bc 4 * and associated documentation files (the "Software"), to deal in the Software without
JimCarver 3:eb1271ef90bc 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
JimCarver 3:eb1271ef90bc 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
JimCarver 3:eb1271ef90bc 7 * Software is furnished to do so, subject to the following conditions:
JimCarver 3:eb1271ef90bc 8 *
JimCarver 3:eb1271ef90bc 9 * The above copyright notice and this permission notice shall be included in all copies or
JimCarver 3:eb1271ef90bc 10 * substantial portions of the Software.
JimCarver 3:eb1271ef90bc 11 *
JimCarver 3:eb1271ef90bc 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
JimCarver 3:eb1271ef90bc 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
JimCarver 3:eb1271ef90bc 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
JimCarver 3:eb1271ef90bc 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
JimCarver 3:eb1271ef90bc 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
JimCarver 3:eb1271ef90bc 17 */
JimCarver 3:eb1271ef90bc 18
JimCarver 0:2562215f5bc0 19 #ifndef FXOS8700Q_H
JimCarver 0:2562215f5bc0 20 #define FXOS8700Q_H
JimCarver 0:2562215f5bc0 21
JimCarver 0:2562215f5bc0 22 #include "mbed.h"
JimCarver 0:2562215f5bc0 23 // FXOS8700CQ I2C address
JimCarver 1:8b53edef272f 24 #define FXOS8700CQ_SLAVE_ADDR0 (0x1E<<1) // with pins SA0=0, SA1=0
JimCarver 1:8b53edef272f 25 #define FXOS8700CQ_SLAVE_ADDR1 (0x1D<<1) // with pins SA0=1, SA1=0
JimCarver 1:8b53edef272f 26 #define FXOS8700CQ_SLAVE_ADDR2 (0x1C<<1) // with pins SA0=0, SA1=1
JimCarver 1:8b53edef272f 27 #define FXOS8700CQ_SLAVE_ADDR3 (0x1F<<1) // with pins SA0=1, SA1=1
JimCarver 0:2562215f5bc0 28 // FXOS8700CQ internal register addresses
JimCarver 1:8b53edef272f 29 #define FXOS8700Q_STATUS 0x00
JimCarver 1:8b53edef272f 30 #define FXOS8700Q_OUT_X_MSB 0x01
JimCarver 1:8b53edef272f 31 #define FXOS8700Q_OUT_Y_MSB 0x03
JimCarver 1:8b53edef272f 32 #define FXOS8700Q_OUT_Z_MSB 0x05
JimCarver 1:8b53edef272f 33 #define FXOS8700Q_M_OUT_X_MSB 0x33
JimCarver 1:8b53edef272f 34 #define FXOS8700Q_M_OUT_Y_MSB 0x35
JimCarver 1:8b53edef272f 35 #define FXOS8700Q_M_OUT_Z_MSB 0x37
JimCarver 1:8b53edef272f 36 #define FXOS8700Q_WHOAMI 0x0D
JimCarver 1:8b53edef272f 37 #define FXOS8700Q_XYZ_DATA_CFG 0x0E
JimCarver 1:8b53edef272f 38 #define FXOS8700Q_CTRL_REG1 0x2A
JimCarver 1:8b53edef272f 39 #define FXOS8700Q_M_CTRL_REG1 0x5B
JimCarver 1:8b53edef272f 40 #define FXOS8700Q_M_CTRL_REG2 0x5C
JimCarver 1:8b53edef272f 41 #define FXOS8700Q_WHOAMI_VAL 0xC7
JimCarver 0:2562215f5bc0 42
JimCarver 3:eb1271ef90bc 43
JimCarver 3:eb1271ef90bc 44 /**
JimCarver 3:eb1271ef90bc 45 * MMA8451Q accelerometer example
JimCarver 3:eb1271ef90bc 46 *
JimCarver 3:eb1271ef90bc 47 * @code
JimCarver 3:eb1271ef90bc 48 * #include "mbed.h"
JimCarver 3:eb1271ef90bc 49 * #include "FXOS8700Q.h"
JimCarver 3:eb1271ef90bc 50 *
JimCarver 3:eb1271ef90bc 51 *
JimCarver 3:eb1271ef90bc 52 * int main(void) {
JimCarver 3:eb1271ef90bc 53 *
JimCarver 3:eb1271ef90bc 54 * FXOS8700Q combo( A4, A5, FXOS8700Q_I2C_ADDRESS0);
JimCarver 3:eb1271ef90bc 55 * PwmOut rled(LED_RED);
JimCarver 3:eb1271ef90bc 56 * PwmOut gled(LED_GREEN);
JimCarver 3:eb1271ef90bc 57 * PwmOut bled(LED_BLUE);
JimCarver 3:eb1271ef90bc 58 *
JimCarver 3:eb1271ef90bc 59 * while (true) {
JimCarver 3:eb1271ef90bc 60 * rled = 1.0 - combo(acc.getAccX());
JimCarver 3:eb1271ef90bc 61 * gled = 1.0 - combo(acc.getAccY());
JimCarver 3:eb1271ef90bc 62 * bled = 1.0 - combo(acc.getAccZ());
JimCarver 3:eb1271ef90bc 63 * wait(0.1);
JimCarver 3:eb1271ef90bc 64 * }
JimCarver 3:eb1271ef90bc 65 * }
JimCarver 3:eb1271ef90bc 66 * @endcode
JimCarver 3:eb1271ef90bc 67 */
JimCarver 3:eb1271ef90bc 68
JimCarver 0:2562215f5bc0 69 class FXOS8700Q
JimCarver 0:2562215f5bc0 70 {
JimCarver 0:2562215f5bc0 71 public:
JimCarver 1:8b53edef272f 72 /**
JimCarver 1:8b53edef272f 73 * FXOS8700Q constructor
JimCarver 1:8b53edef272f 74 *
JimCarver 1:8b53edef272f 75 * @param sda SDA pin
JimCarver 1:8b53edef272f 76 * @param sdl SCL pin
JimCarver 1:8b53edef272f 77 * @param addr addr of the I2C peripheral
JimCarver 1:8b53edef272f 78 */
JimCarver 1:8b53edef272f 79
JimCarver 1:8b53edef272f 80 FXOS8700Q(PinName sda, PinName scl, int addr);
JimCarver 1:8b53edef272f 81
JimCarver 1:8b53edef272f 82 /**
JimCarver 1:8b53edef272f 83 * FXOS8700Q destructor
JimCarver 1:8b53edef272f 84 */
JimCarver 1:8b53edef272f 85 ~FXOS8700Q();
JimCarver 1:8b53edef272f 86
JimCarver 1:8b53edef272f 87 /**
JimCarver 1:8b53edef272f 88 * Get the value of the WHO_AM_I register
JimCarver 1:8b53edef272f 89 *
JimCarver 1:8b53edef272f 90 * @returns WHO_AM_I value
JimCarver 1:8b53edef272f 91 */
JimCarver 1:8b53edef272f 92 uint8_t getWhoAmI();
JimCarver 1:8b53edef272f 93
JimCarver 1:8b53edef272f 94 /**
JimCarver 1:8b53edef272f 95 * Get X axis acceleration
JimCarver 1:8b53edef272f 96 *
JimCarver 1:8b53edef272f 97 * @returns X axis acceleration
JimCarver 1:8b53edef272f 98 */
JimCarver 1:8b53edef272f 99 float getAccX();
JimCarver 0:2562215f5bc0 100
JimCarver 1:8b53edef272f 101 /**
JimCarver 1:8b53edef272f 102 * Get Y axis acceleration
JimCarver 1:8b53edef272f 103 *
JimCarver 1:8b53edef272f 104 * @returns Y axis acceleration
JimCarver 1:8b53edef272f 105 */
JimCarver 1:8b53edef272f 106 float getAccY();
JimCarver 1:8b53edef272f 107
JimCarver 1:8b53edef272f 108 /**
JimCarver 1:8b53edef272f 109 * Get Z axis acceleration
JimCarver 1:8b53edef272f 110 *
JimCarver 1:8b53edef272f 111 * @returns Z axis acceleration
JimCarver 1:8b53edef272f 112 */
JimCarver 1:8b53edef272f 113 float getAccZ();
JimCarver 0:2562215f5bc0 114
JimCarver 1:8b53edef272f 115 /**
JimCarver 4:be6abf9f2d59 116 * Get XYZ axis acceleration in G's
JimCarver 1:8b53edef272f 117 *
JimCarver 1:8b53edef272f 118 * @param res array where acceleration data will be stored
JimCarver 1:8b53edef272f 119 */
JimCarver 1:8b53edef272f 120 void getAccAllAxis(float * res);
JimCarver 3:eb1271ef90bc 121
JimCarver 3:eb1271ef90bc 122 /**
JimCarver 4:be6abf9f2d59 123 * Get XYZ axis magnetic readings in micro-teslas
JimCarver 4:be6abf9f2d59 124 *
JimCarver 4:be6abf9f2d59 125 * @param res array where acceleration data will be stored
JimCarver 4:be6abf9f2d59 126 */
JimCarver 4:be6abf9f2d59 127 void getMagAllAxis(float * res);
JimCarver 4:be6abf9f2d59 128
JimCarver 4:be6abf9f2d59 129 /**
JimCarver 3:eb1271ef90bc 130 * Get XYZ axis acceleration, signed 16 bit values
JimCarver 3:eb1271ef90bc 131 *
JimCarver 3:eb1271ef90bc 132 * @param res array where acceleration data will be stored
JimCarver 3:eb1271ef90bc 133 */
JimCarver 1:8b53edef272f 134 void AccXYZraw(int16_t * d);
JimCarver 3:eb1271ef90bc 135
JimCarver 3:eb1271ef90bc 136 /**
JimCarver 3:eb1271ef90bc 137 * Get XYZ axis magnetometer readings, signed 16 bit values
JimCarver 3:eb1271ef90bc 138 *
JimCarver 3:eb1271ef90bc 139 * @param res array where acceleration data will be stored
JimCarver 3:eb1271ef90bc 140 */
JimCarver 1:8b53edef272f 141 void MagXYZraw(int16_t * d);
JimCarver 3:eb1271ef90bc 142
JimCarver 3:eb1271ef90bc 143 /**
JimCarver 3:eb1271ef90bc 144 * Read FXOS8700Q internal registers
JimCarver 3:eb1271ef90bc 145 *
JimCarver 3:eb1271ef90bc 146 * @param Register Address
JimCarver 3:eb1271ef90bc 147 * @param Pointer to whare results will be stored
JimCarver 3:eb1271ef90bc 148 * @param Number of registers to read
JimCarver 3:eb1271ef90bc 149 */
JimCarver 1:8b53edef272f 150 void readRegs(int addr, uint8_t * data, int len);
JimCarver 1:8b53edef272f 151
JimCarver 1:8b53edef272f 152 private:
JimCarver 1:8b53edef272f 153 I2C m_i2c;
JimCarver 1:8b53edef272f 154 int m_addr;
JimCarver 0:2562215f5bc0 155
JimCarver 1:8b53edef272f 156 void writeRegs(uint8_t * data, int len);
JimCarver 1:8b53edef272f 157 int16_t getAccAxis(uint8_t addr);
JimCarver 0:2562215f5bc0 158
JimCarver 0:2562215f5bc0 159 };
JimCarver 0:2562215f5bc0 160
JimCarver 0:2562215f5bc0 161 #endif