Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
K64F_PAD_LCDConfiguration/FXOS8700CQ/FXOS8700CQ.cpp@104:17040265b7b4, 2019-05-09 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu May 09 14:52:19 2019 +0000
- Revision:
- 104:17040265b7b4
- Parent:
- 51:387249f9b333
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 0:4b15c2d4aa58 | 1 | /* FXOS8700CQ Library |
AhmedPlaymaker | 0:4b15c2d4aa58 | 2 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 3 | Sample code from ELEC2645 - demonstrates how to create a library |
AhmedPlaymaker | 0:4b15c2d4aa58 | 4 | for the K64F on-board accelerometer and magnetometer |
AhmedPlaymaker | 0:4b15c2d4aa58 | 5 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 6 | (c) Craig A. Evans, University of Leeds, Jan 2017 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 7 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 8 | */ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 9 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 10 | #include "FXOS8700CQ.h" |
AhmedPlaymaker | 0:4b15c2d4aa58 | 11 | #include <math.h> |
AhmedPlaymaker | 0:4b15c2d4aa58 | 12 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 13 | // constructor is called when the object is created - use it to set pins and frequency |
AhmedPlaymaker | 0:4b15c2d4aa58 | 14 | FXOS8700CQ::FXOS8700CQ(PinName sda, PinName scl) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 15 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 16 | i2c = new I2C(sda,scl); // create new I2C instance and initialise |
AhmedPlaymaker | 0:4b15c2d4aa58 | 17 | _PitchAngle = 0.0; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 18 | _RollAngle = 0.0; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 19 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 20 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 21 | // destructor is called when the object goes out of scope |
AhmedPlaymaker | 0:4b15c2d4aa58 | 22 | FXOS8700CQ::~FXOS8700CQ() |
AhmedPlaymaker | 0:4b15c2d4aa58 | 23 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 24 | delete i2c; // free memory |
AhmedPlaymaker | 0:4b15c2d4aa58 | 25 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 26 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 27 | // based on 13.4 in datasheet - 200 Hz hybrid mode (both acc and mag) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 28 | void FXOS8700CQ::init() |
AhmedPlaymaker | 0:4b15c2d4aa58 | 29 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 30 | // i2c fast-mode - 10.1.1 data sheet |
AhmedPlaymaker | 0:4b15c2d4aa58 | 31 | i2c->frequency(400000); // I2C Fast Mode - 400kHz |
AhmedPlaymaker | 0:4b15c2d4aa58 | 32 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 33 | // the device has an ID number so we check the value to ensure the correct |
AhmedPlaymaker | 0:4b15c2d4aa58 | 34 | // drive is on the i2c bus |
AhmedPlaymaker | 0:4b15c2d4aa58 | 35 | char data = read_byte_from_reg(FXOS8700CQ_WHO_AM_I); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 36 | if (data != FXOS8700CQ_WHO_AM_I_VAL) { // if correct ID not found, hang and flash error message |
AhmedPlaymaker | 0:4b15c2d4aa58 | 37 | error("Incorrect ID!"); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 38 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 39 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 40 | // write 0000 0000 = 0x00 to accelerometer control register 1 to place |
AhmedPlaymaker | 0:4b15c2d4aa58 | 41 | // FXOS8700CQ into standby |
AhmedPlaymaker | 0:4b15c2d4aa58 | 42 | // [7-1] = 0000 000 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 43 | // [0]: active=0 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 44 | data = 0x00; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 45 | send_byte_to_reg(data,FXOS8700CQ_CTRL_REG1); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 46 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 47 | // write 0001 1111 = 0x1F to magnetometer control register 1 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 48 | // [7]: m_acal=0: auto calibration disabled |
AhmedPlaymaker | 0:4b15c2d4aa58 | 49 | // [6]: m_rst=0: no one-shot magnetic reset |
AhmedPlaymaker | 0:4b15c2d4aa58 | 50 | // [5]: m_ost=0: no one-shot magnetic measurement |
AhmedPlaymaker | 0:4b15c2d4aa58 | 51 | // [4-2]: m_os=111=7: 8x oversampling (for 200Hz) to reduce magnetometer noise |
AhmedPlaymaker | 0:4b15c2d4aa58 | 52 | // [1-0]: m_hms=11=3: select hybrid mode with accel and magnetometer active |
AhmedPlaymaker | 0:4b15c2d4aa58 | 53 | data = 0x1F; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 54 | send_byte_to_reg(data,FXOS8700CQ_M_CTRL_REG1); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 55 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 56 | // write 0010 0000 = 0x20 to magnetometer control register 2 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 57 | // [7]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 58 | // [6]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 59 | // [5]: hyb_autoinc_mode=1 to map the magnetometer registers to follow |
AhmedPlaymaker | 0:4b15c2d4aa58 | 60 | // the accelerometer registers |
AhmedPlaymaker | 0:4b15c2d4aa58 | 61 | // [4]: m_maxmin_dis=0 to retain default min/max latching even though not used |
AhmedPlaymaker | 0:4b15c2d4aa58 | 62 | // [3]: m_maxmin_dis_ths=0 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 63 | // [2]: m_maxmin_rst=0 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 64 | // [1-0]: m_rst_cnt=00 to enable magnetic reset each cycle |
AhmedPlaymaker | 0:4b15c2d4aa58 | 65 | data = 0x20; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 66 | send_byte_to_reg(data,FXOS8700CQ_M_CTRL_REG2); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 67 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 68 | // write 0000 0001= 0x01 to XYZ_DATA_CFG register |
AhmedPlaymaker | 0:4b15c2d4aa58 | 69 | // [7]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 70 | // [6]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 71 | // [5]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 72 | // [4]: hpf_out=0 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 73 | // [3]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 74 | // [2]: reserved |
AhmedPlaymaker | 0:4b15c2d4aa58 | 75 | // [1-0]: fs=01 for accelerometer range of +/-4g range with 0.488mg/LSB |
AhmedPlaymaker | 0:4b15c2d4aa58 | 76 | data = 0x01; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 77 | send_byte_to_reg(data,FXOS8700CQ_XYZ_DATA_CFG); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 78 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 79 | // write 0000 1101 = 0x0D to accelerometer control register 1 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 80 | // [7-6]: aslp_rate=00 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 81 | // [5-3]: dr=001 for 200Hz data rate (when in hybrid mode) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 82 | // [2]: lnoise=1 for low noise mode |
AhmedPlaymaker | 0:4b15c2d4aa58 | 83 | // [1]: f_read=0 for normal 16 bit reads |
AhmedPlaymaker | 0:4b15c2d4aa58 | 84 | // [0]: active=1 to take the part out of standby and enable sampling |
AhmedPlaymaker | 0:4b15c2d4aa58 | 85 | data = 0x0D; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 86 | send_byte_to_reg(data,FXOS8700CQ_CTRL_REG1); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 87 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 88 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 89 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 90 | Data FXOS8700CQ::get_values() |
AhmedPlaymaker | 0:4b15c2d4aa58 | 91 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 92 | // 13 bytes - status plus 6 channels (2 bytes each) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 93 | // x,y,z for accelerometer and magnetometer |
AhmedPlaymaker | 0:4b15c2d4aa58 | 94 | char data[FXOS8700CQ_READ_LEN]; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 95 | read_bytes_from_reg(FXOS8700CQ_STATUS,FXOS8700CQ_READ_LEN,data); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 96 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 97 | // copy the 14 bit accelerometer byte data into 16 bit words |
AhmedPlaymaker | 0:4b15c2d4aa58 | 98 | int acc_x = (int16_t)(((data[1] << 8) | data[2]))>> 2; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 99 | int acc_y = (int16_t)(((data[3] << 8) | data[4]))>> 2; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 100 | int acc_z = (int16_t)(((data[5] << 8) | data[6]))>> 2; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 101 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 102 | // copy the magnetometer byte data into 16 bit words |
AhmedPlaymaker | 0:4b15c2d4aa58 | 103 | int mag_x = (int16_t) (data[7] << 8) | data[8]; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 104 | int mag_y = (int16_t) (data[9] << 8) | data[10]; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 105 | int mag_z = (int16_t) (data[11] << 8) | data[12]; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 106 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 107 | Data values; // struct to hold values |
AhmedPlaymaker | 0:4b15c2d4aa58 | 108 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 109 | // 0.488 mg/LSB in 4 g mode (8.1 data sheet) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 110 | values.ax = 0.488e-3*acc_x; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 111 | values.ay = 0.488e-3*acc_y; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 112 | values.az = 0.488e-3*acc_z; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 113 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 114 | // the magnetometer sensitivity is fixed at 0.1 μT/LSB |
AhmedPlaymaker | 0:4b15c2d4aa58 | 115 | values.mx = 0.1e-6*mag_x; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 116 | values.my = 0.1e-6*mag_y; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 117 | values.mz = 0.1e-6*mag_z; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 118 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 119 | _PitchAngle = (atan2(-(values.ax),(sqrt(((values.ay)*(values.ay))+((values.az)*(values.az))))) * 180 / PI); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 120 | _RollAngle = (atan2((values.ay),(values.az)) * 180 / PI); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 121 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 122 | return values; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 123 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 124 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 125 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 126 | float FXOS8700CQ::get_pitch_angle() |
AhmedPlaymaker | 0:4b15c2d4aa58 | 127 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 128 | return _PitchAngle; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 129 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 130 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 131 | float FXOS8700CQ::get_roll_angle() |
AhmedPlaymaker | 0:4b15c2d4aa58 | 132 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 133 | return _RollAngle; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 134 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 135 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 136 | void FXOS8700CQ::send_byte_to_reg(char byte,char reg) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 137 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 138 | char data[2]; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 139 | data[0] = reg; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 140 | data[1] = byte; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 141 | // send the register address, followed by the data |
AhmedPlaymaker | 0:4b15c2d4aa58 | 142 | int nack = i2c->write(FXOS8700CQ_ADDR,data,2); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 143 | if (nack) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 144 | error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message |
AhmedPlaymaker | 0:4b15c2d4aa58 | 145 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 146 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 147 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 148 | // reads a byte from a specific register |
AhmedPlaymaker | 0:4b15c2d4aa58 | 149 | char FXOS8700CQ::read_byte_from_reg(char reg) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 150 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 151 | int nack = i2c->write(FXOS8700CQ_ADDR,®,1,true); // send the register address to the slave |
AhmedPlaymaker | 0:4b15c2d4aa58 | 152 | // true as need to send repeated start condition (5.10.1 datasheet) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 153 | // http://www.i2c-bus.org/repeated-start-condition/ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 154 | if (nack) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 155 | error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message |
AhmedPlaymaker | 0:4b15c2d4aa58 | 156 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 157 | char rx; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 158 | nack = i2c->read(FXOS8700CQ_ADDR,&rx,1); // read a byte from the register and store in buffer |
AhmedPlaymaker | 0:4b15c2d4aa58 | 159 | if (nack) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 160 | error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message |
AhmedPlaymaker | 0:4b15c2d4aa58 | 161 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 162 | return rx; |
AhmedPlaymaker | 0:4b15c2d4aa58 | 163 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 164 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 165 | // reads a series of bytes, starting from a specific register |
AhmedPlaymaker | 0:4b15c2d4aa58 | 166 | void FXOS8700CQ::read_bytes_from_reg(char reg,int number_of_bytes,char bytes[]) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 167 | { |
AhmedPlaymaker | 0:4b15c2d4aa58 | 168 | int nack = i2c->write(FXOS8700CQ_ADDR,®,1,true); // send the slave write address and the configuration register address |
AhmedPlaymaker | 0:4b15c2d4aa58 | 169 | // true as need to send repeated start condition (5.10.1 datasheet) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 170 | // http://www.i2c-bus.org/repeated-start-condition/ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 171 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 172 | if (nack) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 173 | error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message |
AhmedPlaymaker | 0:4b15c2d4aa58 | 174 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 175 | nack = i2c->read(FXOS8700CQ_ADDR,bytes,number_of_bytes); // read bytes |
AhmedPlaymaker | 0:4b15c2d4aa58 | 176 | if (nack) |
AhmedPlaymaker | 0:4b15c2d4aa58 | 177 | error("No acknowledgement received!"); // if we don't receive acknowledgement, send error message |
AhmedPlaymaker | 0:4b15c2d4aa58 | 178 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 179 | } |