James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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