A basic library for the FXOS8700Q combination accelerometer / magnetometer

Dependencies:   MotionSensor

Dependents:   K64F_eCompass_LCD Hello_FXOS8700Q rtos_compass K64F_eCompass ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FXOS8700Q.h Source File

FXOS8700Q.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef FXOS8700Q_H
00020 #define FXOS8700Q_H
00021 
00022 #include "mbed.h"
00023 #include "MotionSensor.h"
00024 // FXOS8700CQ I2C address
00025 #define FXOS8700CQ_SLAVE_ADDR0 (0x1E<<1) // with pins SA0=0, SA1=0
00026 #define FXOS8700CQ_SLAVE_ADDR1 (0x1D<<1) // with pins SA0=1, SA1=0
00027 #define FXOS8700CQ_SLAVE_ADDR2 (0x1C<<1) // with pins SA0=0, SA1=1
00028 #define FXOS8700CQ_SLAVE_ADDR3 (0x1F<<1) // with pins SA0=1, SA1=1
00029 // FXOS8700CQ internal register addresses
00030 #define FXOS8700Q_STATUS 0x00
00031 #define FXOS8700Q_OUT_X_MSB 0x01
00032 #define FXOS8700Q_OUT_Y_MSB 0x03
00033 #define FXOS8700Q_OUT_Z_MSB 0x05
00034 #define FXOS8700Q_M_OUT_X_MSB 0x33
00035 #define FXOS8700Q_M_OUT_Y_MSB 0x35
00036 #define FXOS8700Q_M_OUT_Z_MSB 0x37
00037 #define FXOS8700Q_WHOAMI 0x0D
00038 #define FXOS8700Q_XYZ_DATA_CFG 0x0E
00039 #define FXOS8700Q_CTRL_REG1 0x2A
00040 #define FXOS8700Q_M_CTRL_REG1 0x5B
00041 #define FXOS8700Q_M_CTRL_REG2 0x5C
00042 #define FXOS8700Q_WHOAMI_VAL 0xC7
00043 
00044 
00045 /**
00046 * MMA8451Q accelerometer example
00047 *
00048 * @code
00049 * #include "mbed.h"
00050 * #include "FXOS8700Q.h"
00051 * 
00052 * 
00053 * int main(void) {
00054 * 
00055 * FXOS8700Q combo( A4, A5, FXOS8700Q_I2C_ADDRESS0);
00056 * PwmOut rled(LED_RED);
00057 * PwmOut gled(LED_GREEN);
00058 * PwmOut bled(LED_BLUE);
00059 * 
00060 *     while (true) {       
00061 *         rled1.0 - combo(acc.getAccX());
00062 *         gled1.0 - combo(acc.getAccY());
00063 *         bled1.0 - combo(acc.getAccZ());
00064 *         wait(0.1);
00065 *     }
00066 * }
00067 * @endcode
00068 */
00069 
00070 class FXOS8700Q_acc : public MotionSensor
00071 {
00072 public:
00073   /**
00074   * FXOS8700Q constructor
00075   *
00076   * @param sda SDA pin
00077   * @param sdl SCL pin
00078   * @param addr addr of the I2C peripheral
00079   */
00080   
00081   FXOS8700Q_acc(PinName sda, PinName scl, int addr);
00082 
00083   /**
00084   * FXOS8700Q destructor
00085   */
00086   ~FXOS8700Q_acc();
00087 
00088     void enable(void);
00089     void disable(void);
00090     uint32_t sampleRate(uint32_t frequency);
00091     uint32_t whoAmI(void);
00092     uint32_t dataReady(void);
00093     void getX(int16_t * x);
00094     void getY(int16_t * y);
00095     void getZ(int16_t * z);
00096     void getX(float * x);
00097     void getY(float * y);
00098     void getZ(float * z);
00099     void getAxis(MotionSensorDataCounts &data);
00100     void getAxis(MotionSensorDataUnits &data);
00101   
00102   void readRegs(int addr, uint8_t * data, int len);
00103   
00104 private:
00105   I2C m_i2c;
00106   int m_addr;
00107 
00108   void writeRegs(uint8_t * data, int len);
00109   int16_t getAccAxis(uint8_t addr);
00110 
00111 };
00112 
00113 class FXOS8700Q_mag : public MotionSensor
00114 {
00115 public:
00116   FXOS8700Q_mag(PinName sda, PinName scl, int addr);
00117 
00118   /**
00119   * FXOS8700Q destructor
00120   */
00121   ~FXOS8700Q_mag();
00122 
00123     void enable(void);
00124     void disable(void);
00125     uint32_t sampleRate(uint32_t fequency);
00126     uint32_t whoAmI(void);
00127     uint32_t dataReady(void);
00128     void getX(int16_t * x);
00129     void getY(int16_t * y);
00130     void getZ(int16_t * z);
00131     void getX(float * x);
00132     void getY(float * y);
00133     void getZ(float * z);
00134     void getAxis(MotionSensorDataCounts &data);
00135     void getAxis(MotionSensorDataUnits &data);
00136   
00137   void readRegs(int addr, uint8_t * data, int len);
00138   
00139 private:
00140   I2C m_i2c;
00141   int m_addr;
00142   char sbuf[12];
00143   int sstatus;
00144   
00145   void writeRegs(uint8_t * data, int len);
00146   int16_t getAccAxis(uint8_t addr);
00147 
00148 };
00149 
00150 #endif