![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
FXOS8700CQ/FXOS8700CQ.h@0:4b15c2d4aa58, 2019-03-07 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu Mar 07 19:05:10 2019 +0000
- Revision:
- 0:4b15c2d4aa58
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 0:4b15c2d4aa58 | 1 | /** @file FXOS8700CQ.h |
AhmedPlaymaker | 0:4b15c2d4aa58 | 2 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 3 | @ brief FXOS8700CQ Library |
AhmedPlaymaker | 0:4b15c2d4aa58 | 4 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 5 | @author Dr Craig A. Evans |
AhmedPlaymaker | 0:4b15c2d4aa58 | 6 | @brief (c) University of Leeds, Jan 2017 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 7 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 8 | @code |
AhmedPlaymaker | 0:4b15c2d4aa58 | 9 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 10 | #include "mbed.h" |
AhmedPlaymaker | 0:4b15c2d4aa58 | 11 | #include "FXOS8700CQ.h" |
AhmedPlaymaker | 0:4b15c2d4aa58 | 12 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 13 | // create object and specifiy pins |
AhmedPlaymaker | 0:4b15c2d4aa58 | 14 | FXOS8700CQ device(I2C_SDA,I2C_SCL); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 15 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 16 | int main() |
AhmedPlaymaker | 0:4b15c2d4aa58 | 17 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 18 | // call initialisation method |
AhmedPlaymaker | 0:4b15c2d4aa58 | 19 | device.init(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 20 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 21 | while (1) { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 22 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 23 | // poll the sensor and get the values, storing in a struct |
AhmedPlaymaker | 0:4b15c2d4aa58 | 24 | Data values = device.get_values(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 25 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 26 | // print each struct member over serial |
AhmedPlaymaker | 0:4b15c2d4aa58 | 27 | printf("ax = %f ay = %f az = %f | mx = %f my = %f mz = %f\n" |
AhmedPlaymaker | 0:4b15c2d4aa58 | 28 | ,values.ax, values.ay, values.az |
AhmedPlaymaker | 0:4b15c2d4aa58 | 29 | ,values.mx, values.my, values.mz); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 30 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 31 | wait(0.5); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 32 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 33 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 34 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 35 | @endcode |
AhmedPlaymaker | 0:4b15c2d4aa58 | 36 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 37 | */ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 38 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 39 | #ifndef FXOS8700CQ_H |
AhmedPlaymaker | 0:4b15c2d4aa58 | 40 | #define FXOS8700CQ_H |
AhmedPlaymaker | 0:4b15c2d4aa58 | 41 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 42 | #include "mbed.h" |
AhmedPlaymaker | 0:4b15c2d4aa58 | 43 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 44 | // mbed API uses 8-bit addresses so need to left-shift 7-bit addresses by 1 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 45 | #define FXOS8700CQ_ADDR (0x1D << 1) // for K64F board |
AhmedPlaymaker | 0:4b15c2d4aa58 | 46 | // values from 13.2 datasheet |
AhmedPlaymaker | 0:4b15c2d4aa58 | 47 | #define FXOS8700CQ_STATUS 0x00 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 48 | #define FXOS8700CQ_WHO_AM_I 0x0D |
AhmedPlaymaker | 0:4b15c2d4aa58 | 49 | #define FXOS8700CQ_XYZ_DATA_CFG 0x0E |
AhmedPlaymaker | 0:4b15c2d4aa58 | 50 | #define FXOS8700CQ_CTRL_REG1 0x2A |
AhmedPlaymaker | 0:4b15c2d4aa58 | 51 | #define FXOS8700CQ_M_CTRL_REG1 0x5B |
AhmedPlaymaker | 0:4b15c2d4aa58 | 52 | #define FXOS8700CQ_M_CTRL_REG2 0x5C |
AhmedPlaymaker | 0:4b15c2d4aa58 | 53 | #define FXOS8700CQ_WHO_AM_I_VAL 0xC7 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 54 | #define FXOS8700CQ_READ_LEN 13 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 55 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 56 | #define PI 3.14159265359f |
AhmedPlaymaker | 0:4b15c2d4aa58 | 57 | #define RAD2DEG 57.2957795131f |
AhmedPlaymaker | 0:4b15c2d4aa58 | 58 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 59 | struct Data { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 60 | float ax; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 61 | float ay; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 62 | float az; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 63 | float mx; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 64 | float my; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 65 | float mz; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 66 | }; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 67 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 68 | class FXOS8700CQ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 69 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 70 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 71 | public: |
AhmedPlaymaker | 0:4b15c2d4aa58 | 72 | FXOS8700CQ(PinName sda, PinName scl); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 73 | ~FXOS8700CQ(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 74 | void init(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 75 | Data get_values(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 76 | float get_pitch_angle(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 77 | float get_roll_angle(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 78 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 79 | private: |
AhmedPlaymaker | 0:4b15c2d4aa58 | 80 | I2C* i2c; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 81 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 82 | void send_byte_to_reg(char byte,char reg); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 83 | char read_byte_from_reg(char reg); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 84 | void read_bytes_from_reg(char reg,int number_of_bytes,char bytes[]); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 85 | float _PitchAngle; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 86 | float _RollAngle; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 87 | }; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 88 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 89 | #endif |