Reimplemented FXAS21000 library for Freescale Gyro

Fork of FXAS21000 by Jim Carver

Revision:
4:41bd7f360406
Parent:
3:a8f83b52f4df
--- a/FXAS21000.h	Mon Jun 02 17:36:38 2014 +0000
+++ b/FXAS21000.h	Wed Jul 08 13:34:46 2015 +0000
@@ -20,65 +20,84 @@
 #define FXAS21000_H
 
 #include "mbed.h"
-
+#include "MotionSensor.h"
 
 /**
- * MMA8652 Slave Address
+ * FXAS21000 Slave Address
  */
 #define FXAS21000_SLAVE_ADDR 0x40
 
-// MMA8652 internal register addresses
+// FXAS21000 internal register addresses
 #define FXAS21000_STATUS 0x00
 #define FXAS21000_WHOAMI 0x0C
 #define FXAS21000_XYZ_DATA_CFG 0x0E
 #define FXAS21000_CTRL_REG0 0x0D
 #define FXAS21000_CTRL_REG1 0x13
 #define FXAS21000_WHOAMI_VAL 0xD1
+#define FXAS21000_OUT_X_MSB 0x01
+#define FXAS21000_OUT_Y_MSB 0x03
+#define FXAS21000_OUT_Z_MSB 0x05
 
 /**
- * FXAS21000 gyroscope on I2C
+ * FXAS21000 driver class
  */
-class FXAS21000
+class FXAS21000 : public MotionSensor
 {
 public:
-    /**
-     * FXAS21000 constructor
-     *
-     * @param sda SDA pin
-     * @param sdl SCL pin
+
+    /** Read a device register
+        @param addr The address to read from
+        @param data The data to read from it
+        @param len The amount of data to read from it
+        @return 0 if successful, negative number otherwise
      */
-    FXAS21000(PinName sda, PinName scl);
- 
-    /**
-     * Get the Gyro values as floating point degrees / second
-     *
-     * @param floating point array where the results will be placed
+    void readRegs(uint8_t addr, uint8_t *data, uint32_t len) const;
+
+    /** Read the ID from a whoAmI register
+        @return The device whoAmI register contents
      */
-    void ReadXYZ(float * a);
+    uint8_t whoAmI(void) const;
+
+    virtual void enable(void) const;
+    virtual void disable(void) const;
+    virtual uint32_t sampleRate(uint32_t frequency) const;
+    virtual uint32_t dataReady(void) const;
 
-    /**
-     * Get the Gyro values as signed 16 bit value
-     *
-     * @param int16_t point array where the results will be placed
+protected:
+    I2C *_i2c;
+    uint8_t _addr;
+    
+    /** FXAS21000 constructor
+        @param i2c a configured i2c object
+        @param addr addr of the I2C peripheral as wired
      */
-    void ReadXYZraw(int16_t * t);
-   
-    /**
-     * Get the value of the WHO_AM_I register
-     *
-     * @returns DEVICE_ID value == 0xD1
+    FXAS21000(I2C &i2c, uint8_t addr);
+
+    /** FXAS21000 deconstructor
      */
-    char getWhoAmI(void);
+    ~FXAS21000();
     
-private:
-    I2C _i2c;
+    void writeRegs(uint8_t *data, uint32_t len) const;
+    int16_t getSensorAxis(uint8_t addr) const;
+};
+
+/** FXAS21000Gyroscpe interface
+ */
+class FXAS21000Gyroscpe : public FXAS21000
+{
+public:
 
-    /**
-     * Set the device in active mode
-     */
-    void begin( void);
-    
-    void RegRead( char reg, char * d, int len);
+    FXAS21000Gyroscpe(I2C &i2c, uint8_t addr) : FXAS21000(i2c, addr) {}
+
+    virtual int16_t getX(int16_t &x) const;
+    virtual int16_t getY(int16_t &y) const;
+    virtual int16_t getZ(int16_t &z) const;
+    virtual float getX(float &x) const;
+    virtual float getY(float &y) const;
+    virtual float getZ(float &z) const;
+    virtual void getAxis(motion_data_counts_t &xyz) const;
+    virtual void getAxis(motion_data_units_t &xyz) const;
+
 };
 
 #endif