Library to access multiple giro - with ability to setup them when starting
Diff: FXAS21002CQ.h
- Revision:
- 0:8aa01dbab64e
- Child:
- 1:816ba6bfbb20
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FXAS21002CQ.h Wed Jul 22 13:23:44 2015 +0000 @@ -0,0 +1,154 @@ +/* FXAS21002CQ sensor driver + * Copyright (c) 2015 WD + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FXAS21002CQ_H +#define FXAS21002CQ_H + +#include "mbed.h" +#include "MotionSensor.h" + +// FXAS21002CQ I2C address +#define FXAS21002CQ_SLAVE_ADDR0 (0x20<<1) // with pins SA0=0, +#define FXAS21002CQ_SLAVE_ADDR1 (0x21<<1) // with pins SA0=1, +// FXAS21002CQ internal register addresses +#define FXAS21002CQ_STATUS 0x00 +#define FXAS21002CQ_OUT_X_MSB 0x01 +#define FXAS21002CQ_OUT_X_LSB 0x02 +#define FXAS21002CQ_OUT_Y_MSB 0x03 +#define FXAS21002CQ_OUT_Y_LSB 0x04 +#define FXAS21002CQ_OUT_Z_MSB 0x05 +#define FXAS21002CQ_OUT_Z_LSB 0x06 +#define FXAS21002CQ_DR_STATUS 0x07 +#define FXAS21002CQ_F_STATUS 0x08 +#define FXAS21002CQ_F_SETUP 0x09 +#define FXAS21002CQ_F_EVENT 0x0A +#define FXAS21002CQ_INT_SRC_FLAG 0x0B +#define FXAS21002CQ_WHO_AM_I 0x0C +#define FXAS21002CQ_CTRL_REG0 0x0D +#define FXAS21002CQ_RT_CFG 0x0E +#define FXAS21002CQ_RT_SRC 0x0F +#define FXAS21002CQ_RT_THS 0x10 +#define FXAS21002CQ_RT_COUNT 0x11 +#define FXAS21002CQ_TEMP 0x12 +#define FXAS21002CQ_CTRL_REG1 0x13 +#define FXAS21002CQ_CTRL_REG2 0x14 +#define FXAS21002CQ_CTRL_REG3 0x15 + +struct FXAS21002CQ_Config_struct +{ + struct FXAS21002CQ_F_SETUP_struct + { + enum {DISABLED=0, CIRCULAR=1, STOP=2} F_MODE; + uint8_t F_WMRK; + } F_SETUP; + struct FXAS21002CQ_CTRL_REG0_struct + { + enum {HIGH_BW=0, MEDIUM_BW=1, LOW_BW=2} BW; + enum {SPI4=0, SPI3=1} SPIW; + enum {HIGH_HP=0, MEDIUM_HIGH_HP=1, MEDIUM_LOW_HP=2, LOW_HP=3} SEL; + enum {DISABLED_HP=0, ENABLED_HP=1} HPF_EN; + enum {RANGE_2000=0, RANGE_1000=1, RANGE_500=2, RANGE_250=3} FS; + } CTRL_REG0; + +}; + +/** FXAS21002CQ giro example + @code + #include "mbed.h" + #include "FXOS8700Q.h" + I2C i2c(PTE25, PTE24); + FXAS21002CQ_Giro giro(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the STTBC-AGM01 + int main(void) + { + motion_data_units_t acc_data, mag_data; + motion_data_counts_t acc_raw, mag_raw; + float faX, faY, faZ, fmX, fmY, fmZ, tmp_float; + int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int; + giro.enable(); + mag.enable(); + while (true) { + // counts based results + acc.getAxis(acc_raw); + mag.getAxis(mag_raw); + acc.getX(raX); + acc.getY(raY); + acc.getZ(raZ); + mag.getX(rmX); + mag.getY(rmY); + mag.getZ(rmZ); + // unit based results + acc.getAxis(acc_data); + mag.getAxis(mag_data); + acc.getX(faX); + acc.getY(faY); + acc.getZ(faZ); + mag.getX(fmX); + mag.getY(fmY); + mag.getZ(fmZ); + wait(0.1f); + } + } + @endcode + */ + +/** FXOS8700Q driver class + */ +class FXAS21002CQ : public MotionSensor +{ +public: + + /** 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 + */ + void readRegs(uint8_t addr, uint8_t *data, uint32_t len) ; + + /** Read the ID from a whoAmI register + @return The device whoAmI register contents + */ + uint8_t whoAmI(void) ; + + virtual void enable(void) const; + virtual void disable(void) const; + virtual uint32_t sampleRate(uint32_t frequency) const; + virtual uint32_t dataReady(void) const; + + 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; + + int8_t getTemperature(void); + + /** FXAS21002CQ constructor + @param i2c a configured i2c object + @param addr addr of the I2C peripheral as wired + */ + FXAS21002CQ(I2C &i2c, uint8_t addr); + + /** FXAS21002CQ deconstructor + */ + ~FXAS21002CQ(); + +protected: + I2C *_i2c; + uint8_t _addr; + + void writeRegs(uint8_t *data, uint32_t len) const; + int16_t getSensorAxis(uint8_t addr) const; +}; + +#endif