A basic library for the FXOS8700Q combination accelerometer / magnetometer
Fork of FXOS8700Q by
FXOS8700Q.h@10:1efb0564e0a4, 2015-10-29 (annotated)
- Committer:
- co657_kk351
- Date:
- Thu Oct 29 17:38:41 2015 +0000
- Revision:
- 10:1efb0564e0a4
- Parent:
- 9:5553a64d0762
.
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:cdc362f08339 | 61 | * rled1.0 - combo(acc.getAccX()); |
JimCarver | 6:cdc362f08339 | 62 | * gled1.0 - combo(acc.getAccY()); |
JimCarver | 6:cdc362f08339 | 63 | * bled1.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 | 6:cdc362f08339 | 70 | class FXOS8700Q_acc : 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 | 6:cdc362f08339 | 81 | FXOS8700Q_acc(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 | 6:cdc362f08339 | 86 | ~FXOS8700Q_acc(); |
JimCarver | 1:8b53edef272f | 87 | |
co657_kk351 | 10:1efb0564e0a4 | 88 | virtual void enable(void); |
co657_kk351 | 10:1efb0564e0a4 | 89 | virtual void disable(void); |
co657_kk351 | 10:1efb0564e0a4 | 90 | virtual uint32_t sampleRate(uint32_t frequency); |
co657_kk351 | 10:1efb0564e0a4 | 91 | virtual uint32_t whoAmI(void); |
co657_kk351 | 10:1efb0564e0a4 | 92 | virtual uint32_t dataReady(void); |
co657_kk351 | 10:1efb0564e0a4 | 93 | virtual void getX(int16_t * x); |
co657_kk351 | 10:1efb0564e0a4 | 94 | virtual void getY(int16_t * y); |
co657_kk351 | 10:1efb0564e0a4 | 95 | virtual void getZ(int16_t * z); |
co657_kk351 | 10:1efb0564e0a4 | 96 | virtual void getX(float * x); |
co657_kk351 | 10:1efb0564e0a4 | 97 | virtual void getY(float * y); |
co657_kk351 | 10:1efb0564e0a4 | 98 | virtual void getZ(float * z); |
co657_kk351 | 10:1efb0564e0a4 | 99 | virtual void getAxis(MotionSensorDataCounts &data); |
co657_kk351 | 10:1efb0564e0a4 | 100 | virtual void getAxis(MotionSensorDataUnits &data); |
JimCarver | 6:cdc362f08339 | 101 | |
JimCarver | 6:cdc362f08339 | 102 | void readRegs(int addr, uint8_t * data, int len); |
JimCarver | 6:cdc362f08339 | 103 | |
JimCarver | 6:cdc362f08339 | 104 | private: |
JimCarver | 6:cdc362f08339 | 105 | I2C m_i2c; |
JimCarver | 6:cdc362f08339 | 106 | int m_addr; |
JimCarver | 0:2562215f5bc0 | 107 | |
JimCarver | 6:cdc362f08339 | 108 | void writeRegs(uint8_t * data, int len); |
JimCarver | 6:cdc362f08339 | 109 | int16_t getAccAxis(uint8_t addr); |
JimCarver | 6:cdc362f08339 | 110 | |
JimCarver | 6:cdc362f08339 | 111 | }; |
JimCarver | 1:8b53edef272f | 112 | |
JimCarver | 6:cdc362f08339 | 113 | class FXOS8700Q_mag : public MotionSensor |
JimCarver | 6:cdc362f08339 | 114 | { |
JimCarver | 6:cdc362f08339 | 115 | public: |
JimCarver | 6:cdc362f08339 | 116 | FXOS8700Q_mag(PinName sda, PinName scl, int addr); |
JimCarver | 0:2562215f5bc0 | 117 | |
JimCarver | 1:8b53edef272f | 118 | /** |
JimCarver | 6:cdc362f08339 | 119 | * FXOS8700Q destructor |
JimCarver | 6:cdc362f08339 | 120 | */ |
JimCarver | 6:cdc362f08339 | 121 | ~FXOS8700Q_mag(); |
JimCarver | 6:cdc362f08339 | 122 | |
co657_kk351 | 10:1efb0564e0a4 | 123 | virtual void enable(void); |
co657_kk351 | 10:1efb0564e0a4 | 124 | virtual void disable(void); |
co657_kk351 | 10:1efb0564e0a4 | 125 | virtual uint32_t sampleRate(uint32_t fequency); |
co657_kk351 | 10:1efb0564e0a4 | 126 | virtual uint32_t whoAmI(void); |
co657_kk351 | 10:1efb0564e0a4 | 127 | virtual uint32_t dataReady(void); |
co657_kk351 | 10:1efb0564e0a4 | 128 | virtual void getX(int16_t * x); |
co657_kk351 | 10:1efb0564e0a4 | 129 | virtual void getY(int16_t * y); |
co657_kk351 | 10:1efb0564e0a4 | 130 | virtual void getZ(int16_t * z); |
co657_kk351 | 10:1efb0564e0a4 | 131 | virtual void getX(float * x); |
co657_kk351 | 10:1efb0564e0a4 | 132 | virtual void getY(float * y); |
co657_kk351 | 10:1efb0564e0a4 | 133 | virtual void getZ(float * z); |
co657_kk351 | 10:1efb0564e0a4 | 134 | virtual void getAxis(MotionSensorDataCounts &data); |
co657_kk351 | 10:1efb0564e0a4 | 135 | virtual void getAxis(MotionSensorDataUnits &data); |
JimCarver | 4:be6abf9f2d59 | 136 | |
JimCarver | 1:8b53edef272f | 137 | void readRegs(int addr, uint8_t * data, int len); |
JimCarver | 1:8b53edef272f | 138 | |
JimCarver | 1:8b53edef272f | 139 | private: |
JimCarver | 1:8b53edef272f | 140 | I2C m_i2c; |
JimCarver | 1:8b53edef272f | 141 | int m_addr; |
JimCarver | 9:5553a64d0762 | 142 | char sbuf[12]; |
JimCarver | 9:5553a64d0762 | 143 | int sstatus; |
JimCarver | 9:5553a64d0762 | 144 | |
JimCarver | 1:8b53edef272f | 145 | void writeRegs(uint8_t * data, int len); |
JimCarver | 1:8b53edef272f | 146 | int16_t getAccAxis(uint8_t addr); |
JimCarver | 0:2562215f5bc0 | 147 | |
JimCarver | 0:2562215f5bc0 | 148 | }; |
JimCarver | 0:2562215f5bc0 | 149 | |
JimCarver | 0:2562215f5bc0 | 150 | #endif |