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:
Wed Apr 23 17:12:15 2014 +0000
Revision:
5:c4176a12f7d5
Parent:
4:be6abf9f2d59
Child:
6:cdc362f08339
experiment

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 5:c4176a12f7d5 23 #include "MotionSensor.h"
JimCarver 0:2562215f5bc0 24 // FXOS8700CQ I2C address
JimCarver 1:8b53edef272f 25 #define FXOS8700CQ_SLAVE_ADDR0 (0x1E<<1) // with pins SA0=0, SA1=0
JimCarver 1:8b53edef272f 26 #define FXOS8700CQ_SLAVE_ADDR1 (0x1D<<1) // with pins SA0=1, SA1=0
JimCarver 1:8b53edef272f 27 #define FXOS8700CQ_SLAVE_ADDR2 (0x1C<<1) // with pins SA0=0, SA1=1
JimCarver 1:8b53edef272f 28 #define FXOS8700CQ_SLAVE_ADDR3 (0x1F<<1) // with pins SA0=1, SA1=1
JimCarver 0:2562215f5bc0 29 // FXOS8700CQ internal register addresses
JimCarver 1:8b53edef272f 30 #define FXOS8700Q_STATUS 0x00
JimCarver 1:8b53edef272f 31 #define FXOS8700Q_OUT_X_MSB 0x01
JimCarver 1:8b53edef272f 32 #define FXOS8700Q_OUT_Y_MSB 0x03
JimCarver 1:8b53edef272f 33 #define FXOS8700Q_OUT_Z_MSB 0x05
JimCarver 1:8b53edef272f 34 #define FXOS8700Q_M_OUT_X_MSB 0x33
JimCarver 1:8b53edef272f 35 #define FXOS8700Q_M_OUT_Y_MSB 0x35
JimCarver 1:8b53edef272f 36 #define FXOS8700Q_M_OUT_Z_MSB 0x37
JimCarver 1:8b53edef272f 37 #define FXOS8700Q_WHOAMI 0x0D
JimCarver 1:8b53edef272f 38 #define FXOS8700Q_XYZ_DATA_CFG 0x0E
JimCarver 1:8b53edef272f 39 #define FXOS8700Q_CTRL_REG1 0x2A
JimCarver 1:8b53edef272f 40 #define FXOS8700Q_M_CTRL_REG1 0x5B
JimCarver 1:8b53edef272f 41 #define FXOS8700Q_M_CTRL_REG2 0x5C
JimCarver 1:8b53edef272f 42 #define FXOS8700Q_WHOAMI_VAL 0xC7
JimCarver 0:2562215f5bc0 43
JimCarver 3:eb1271ef90bc 44
JimCarver 3:eb1271ef90bc 45 /**
JimCarver 3:eb1271ef90bc 46 * MMA8451Q accelerometer example
JimCarver 3:eb1271ef90bc 47 *
JimCarver 3:eb1271ef90bc 48 * @code
JimCarver 3:eb1271ef90bc 49 * #include "mbed.h"
JimCarver 3:eb1271ef90bc 50 * #include "FXOS8700Q.h"
JimCarver 3:eb1271ef90bc 51 *
JimCarver 3:eb1271ef90bc 52 *
JimCarver 3:eb1271ef90bc 53 * int main(void) {
JimCarver 3:eb1271ef90bc 54 *
JimCarver 3:eb1271ef90bc 55 * FXOS8700Q combo( A4, A5, FXOS8700Q_I2C_ADDRESS0);
JimCarver 3:eb1271ef90bc 56 * PwmOut rled(LED_RED);
JimCarver 3:eb1271ef90bc 57 * PwmOut gled(LED_GREEN);
JimCarver 3:eb1271ef90bc 58 * PwmOut bled(LED_BLUE);
JimCarver 3:eb1271ef90bc 59 *
JimCarver 3:eb1271ef90bc 60 * while (true) {
JimCarver 3:eb1271ef90bc 61 * rled = 1.0 - combo(acc.getAccX());
JimCarver 3:eb1271ef90bc 62 * gled = 1.0 - combo(acc.getAccY());
JimCarver 3:eb1271ef90bc 63 * bled = 1.0 - combo(acc.getAccZ());
JimCarver 3:eb1271ef90bc 64 * wait(0.1);
JimCarver 3:eb1271ef90bc 65 * }
JimCarver 3:eb1271ef90bc 66 * }
JimCarver 3:eb1271ef90bc 67 * @endcode
JimCarver 3:eb1271ef90bc 68 */
JimCarver 3:eb1271ef90bc 69
JimCarver 5:c4176a12f7d5 70 class FXOS8700Q : public MotionSensor
JimCarver 0:2562215f5bc0 71 {
JimCarver 0:2562215f5bc0 72 public:
JimCarver 1:8b53edef272f 73 /**
JimCarver 1:8b53edef272f 74 * FXOS8700Q constructor
JimCarver 1:8b53edef272f 75 *
JimCarver 1:8b53edef272f 76 * @param sda SDA pin
JimCarver 1:8b53edef272f 77 * @param sdl SCL pin
JimCarver 1:8b53edef272f 78 * @param addr addr of the I2C peripheral
JimCarver 1:8b53edef272f 79 */
JimCarver 1:8b53edef272f 80
JimCarver 1:8b53edef272f 81 FXOS8700Q(PinName sda, PinName scl, int addr);
JimCarver 1:8b53edef272f 82
JimCarver 1:8b53edef272f 83 /**
JimCarver 1:8b53edef272f 84 * FXOS8700Q destructor
JimCarver 1:8b53edef272f 85 */
JimCarver 1:8b53edef272f 86 ~FXOS8700Q();
JimCarver 1:8b53edef272f 87
JimCarver 1:8b53edef272f 88 /**
JimCarver 1:8b53edef272f 89 * Get the value of the WHO_AM_I register
JimCarver 1:8b53edef272f 90 *
JimCarver 1:8b53edef272f 91 * @returns WHO_AM_I value
JimCarver 1:8b53edef272f 92 */
JimCarver 1:8b53edef272f 93 uint8_t getWhoAmI();
JimCarver 1:8b53edef272f 94
JimCarver 1:8b53edef272f 95 /**
JimCarver 1:8b53edef272f 96 * Get X axis acceleration
JimCarver 1:8b53edef272f 97 *
JimCarver 1:8b53edef272f 98 * @returns X axis acceleration
JimCarver 1:8b53edef272f 99 */
JimCarver 1:8b53edef272f 100 float getAccX();
JimCarver 0:2562215f5bc0 101
JimCarver 1:8b53edef272f 102 /**
JimCarver 1:8b53edef272f 103 * Get Y axis acceleration
JimCarver 1:8b53edef272f 104 *
JimCarver 1:8b53edef272f 105 * @returns Y axis acceleration
JimCarver 1:8b53edef272f 106 */
JimCarver 1:8b53edef272f 107 float getAccY();
JimCarver 1:8b53edef272f 108
JimCarver 1:8b53edef272f 109 /**
JimCarver 1:8b53edef272f 110 * Get Z axis acceleration
JimCarver 1:8b53edef272f 111 *
JimCarver 1:8b53edef272f 112 * @returns Z axis acceleration
JimCarver 1:8b53edef272f 113 */
JimCarver 1:8b53edef272f 114 float getAccZ();
JimCarver 0:2562215f5bc0 115
JimCarver 1:8b53edef272f 116 /**
JimCarver 4:be6abf9f2d59 117 * Get XYZ axis acceleration in G's
JimCarver 1:8b53edef272f 118 *
JimCarver 1:8b53edef272f 119 * @param res array where acceleration data will be stored
JimCarver 1:8b53edef272f 120 */
JimCarver 1:8b53edef272f 121 void getAccAllAxis(float * res);
JimCarver 3:eb1271ef90bc 122
JimCarver 3:eb1271ef90bc 123 /**
JimCarver 4:be6abf9f2d59 124 * Get XYZ axis magnetic readings in micro-teslas
JimCarver 4:be6abf9f2d59 125 *
JimCarver 4:be6abf9f2d59 126 * @param res array where acceleration data will be stored
JimCarver 4:be6abf9f2d59 127 */
JimCarver 4:be6abf9f2d59 128 void getMagAllAxis(float * res);
JimCarver 4:be6abf9f2d59 129
JimCarver 4:be6abf9f2d59 130 /**
JimCarver 3:eb1271ef90bc 131 * Get XYZ axis acceleration, signed 16 bit values
JimCarver 3:eb1271ef90bc 132 *
JimCarver 3:eb1271ef90bc 133 * @param res array where acceleration data will be stored
JimCarver 3:eb1271ef90bc 134 */
JimCarver 1:8b53edef272f 135 void AccXYZraw(int16_t * d);
JimCarver 3:eb1271ef90bc 136
JimCarver 3:eb1271ef90bc 137 /**
JimCarver 3:eb1271ef90bc 138 * Get XYZ axis magnetometer readings, signed 16 bit values
JimCarver 3:eb1271ef90bc 139 *
JimCarver 3:eb1271ef90bc 140 * @param res array where acceleration data will be stored
JimCarver 3:eb1271ef90bc 141 */
JimCarver 1:8b53edef272f 142 void MagXYZraw(int16_t * d);
JimCarver 3:eb1271ef90bc 143
JimCarver 3:eb1271ef90bc 144 /**
JimCarver 3:eb1271ef90bc 145 * Read FXOS8700Q internal registers
JimCarver 3:eb1271ef90bc 146 *
JimCarver 3:eb1271ef90bc 147 * @param Register Address
JimCarver 3:eb1271ef90bc 148 * @param Pointer to whare results will be stored
JimCarver 3:eb1271ef90bc 149 * @param Number of registers to read
JimCarver 3:eb1271ef90bc 150 */
JimCarver 1:8b53edef272f 151 void readRegs(int addr, uint8_t * data, int len);
JimCarver 1:8b53edef272f 152
JimCarver 1:8b53edef272f 153 private:
JimCarver 1:8b53edef272f 154 I2C m_i2c;
JimCarver 1:8b53edef272f 155 int m_addr;
JimCarver 0:2562215f5bc0 156
JimCarver 1:8b53edef272f 157 void writeRegs(uint8_t * data, int len);
JimCarver 1:8b53edef272f 158 int16_t getAccAxis(uint8_t addr);
JimCarver 0:2562215f5bc0 159
JimCarver 0:2562215f5bc0 160 };
JimCarver 0:2562215f5bc0 161
JimCarver 0:2562215f5bc0 162 #endif