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

FXOS8700Q.h

Committer:
JimCarver
Date:
2014-04-19
Revision:
4:be6abf9f2d59
Parent:
3:eb1271ef90bc
Child:
5:c4176a12f7d5

File content as of revision 4:be6abf9f2d59:

/* Copyright (c) 2010-2011 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef FXOS8700Q_H
#define FXOS8700Q_H

#include "mbed.h"
// FXOS8700CQ I2C address
#define FXOS8700CQ_SLAVE_ADDR0 (0x1E<<1) // with pins SA0=0, SA1=0
#define FXOS8700CQ_SLAVE_ADDR1 (0x1D<<1) // with pins SA0=1, SA1=0
#define FXOS8700CQ_SLAVE_ADDR2 (0x1C<<1) // with pins SA0=0, SA1=1
#define FXOS8700CQ_SLAVE_ADDR3 (0x1F<<1) // with pins SA0=1, SA1=1
// FXOS8700CQ internal register addresses
#define FXOS8700Q_STATUS 0x00
#define FXOS8700Q_OUT_X_MSB 0x01
#define FXOS8700Q_OUT_Y_MSB 0x03
#define FXOS8700Q_OUT_Z_MSB 0x05
#define FXOS8700Q_M_OUT_X_MSB 0x33
#define FXOS8700Q_M_OUT_Y_MSB 0x35
#define FXOS8700Q_M_OUT_Z_MSB 0x37
#define FXOS8700Q_WHOAMI 0x0D
#define FXOS8700Q_XYZ_DATA_CFG 0x0E
#define FXOS8700Q_CTRL_REG1 0x2A
#define FXOS8700Q_M_CTRL_REG1 0x5B
#define FXOS8700Q_M_CTRL_REG2 0x5C
#define FXOS8700Q_WHOAMI_VAL 0xC7


/**
* MMA8451Q accelerometer example
*
* @code
* #include "mbed.h"
* #include "FXOS8700Q.h"
* 
* 
* int main(void) {
* 
* FXOS8700Q combo( A4, A5, FXOS8700Q_I2C_ADDRESS0);
* PwmOut rled(LED_RED);
* PwmOut gled(LED_GREEN);
* PwmOut bled(LED_BLUE);
* 
*     while (true) {       
*         rled = 1.0 - combo(acc.getAccX());
*         gled = 1.0 - combo(acc.getAccY());
*         bled = 1.0 - combo(acc.getAccZ());
*         wait(0.1);
*     }
* }
* @endcode
*/

class FXOS8700Q
{
public:
  /**
  * FXOS8700Q constructor
  *
  * @param sda SDA pin
  * @param sdl SCL pin
  * @param addr addr of the I2C peripheral
  */
  
  FXOS8700Q(PinName sda, PinName scl, int addr);

  /**
  * FXOS8700Q destructor
  */
  ~FXOS8700Q();

  /**
   * Get the value of the WHO_AM_I register
   *
   * @returns WHO_AM_I value
   */
  uint8_t getWhoAmI();

  /**
   * Get X axis acceleration
   *
   * @returns X axis acceleration
   */
  float getAccX();

  /**
   * Get Y axis acceleration
   *
   * @returns Y axis acceleration
   */
  float getAccY();

  /**
   * Get Z axis acceleration
   *
   * @returns Z axis acceleration
   */
  float getAccZ();

  /**
   * Get XYZ axis acceleration in G's
   *
   * @param res array where acceleration data will be stored
   */
  void getAccAllAxis(float * res);
  
  /**
   * Get XYZ axis magnetic readings in micro-teslas
   *
   * @param res array where acceleration data will be stored
   */
  void getMagAllAxis(float * res);
  
  /**
   * Get XYZ axis acceleration, signed 16 bit values
   *
   * @param res array where acceleration data will be stored
   */
  void AccXYZraw(int16_t * d);
    
  /**
   * Get XYZ axis magnetometer readings, signed 16 bit values
   *
   * @param res array where acceleration data will be stored
   */
  void MagXYZraw(int16_t * d);
    
  /**
   * Read FXOS8700Q internal registers
   *
   * @param Register Address
   * @param Pointer to whare results will be stored
   * @param Number of registers to read
   */ 
  void readRegs(int addr, uint8_t * data, int len);
  
private:
  I2C m_i2c;
  int m_addr;

  void writeRegs(uint8_t * data, int len);
  int16_t getAccAxis(uint8_t addr);

};

#endif