Fork of trm's FXOS8700CQ library
Fork of FXOS8700CQ by
Revision 5:f27f2c5dc320, committed 2015-11-20
- Comitter:
- co657_frmb
- Date:
- Fri Nov 20 12:31:26 2015 +0000
- Parent:
- 4:e2fe752b881e
- Commit message:
- Changed data-type from SRAWDATA to FXOSData_t, collect accelerometer and magnetometer in one sitting.
Changed in this revision
FXOS8700CQ.cpp | Show annotated file Show diff for this revision Revisions of this file |
FXOS8700CQ.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r e2fe752b881e -r f27f2c5dc320 FXOS8700CQ.cpp --- a/FXOS8700CQ.cpp Tue Jun 03 19:02:19 2014 +0000 +++ b/FXOS8700CQ.cpp Fri Nov 20 12:31:26 2015 +0000 @@ -113,33 +113,31 @@ return databyte; } -uint8_t FXOS8700CQ::get_data(SRAWDATA *accel_data, SRAWDATA *magn_data) +uint8_t FXOS8700CQ::get_data (FXOSData_t *data) { + int i; + if(!enabled) { return 1; } - read_regs(FXOS8700CQ_M_OUT_X_MSB, raw, FXOS8700CQ_READ_LEN); + read_regs (FXOS8700CQ_M_OUT_X_MSB, raw, FXOS8700CQ_READ_LEN); // Pull out 16-bit, 2's complement magnetometer data - magn_data->x = (raw[0] << 8) | raw[1]; - magn_data->y = (raw[2] << 8) | raw[3]; - magn_data->z = (raw[4] << 8) | raw[5]; + data->s.mag_x = (raw[0] << 8) | raw[1]; + data->s.mag_y = (raw[2] << 8) | raw[3]; + data->s.mag_z = (raw[4] << 8) | raw[5]; // Pull out 14-bit, 2's complement, right-justified accelerometer data - accel_data->x = (raw[6] << 8) | raw[7]; - accel_data->y = (raw[8] << 8) | raw[9]; - accel_data->z = (raw[10] << 8) | raw[11]; + data->s.acc_x = (raw[6] << 8) | raw[7]; + data->s.acc_y = (raw[8] << 8) | raw[9]; + data->s.acc_z = (raw[10] << 8) | raw[11]; // Have to apply corrections to make the int16_t correct - if(accel_data->x > UINT14_MAX/2) { - accel_data->x -= UINT14_MAX; - } - if(accel_data->y > UINT14_MAX/2) { - accel_data->y -= UINT14_MAX; - } - if(accel_data->z > UINT14_MAX/2) { - accel_data->z -= UINT14_MAX; + for (i=0; i<3; i++) { + if (data->v[i] > (UINT14_MAX / 2)) { + data->v[i] -= UINT14_MAX; + } } return 0;
diff -r e2fe752b881e -r f27f2c5dc320 FXOS8700CQ.h --- a/FXOS8700CQ.h Tue Jun 03 19:02:19 2014 +0000 +++ b/FXOS8700CQ.h Fri Nov 20 12:31:26 2015 +0000 @@ -100,13 +100,18 @@ // For processing the accelerometer data to right-justified 2's complement #define UINT14_MAX 16383 -// TODO: struct to hold the data out of the sensor -typedef struct { - int16_t x; - int16_t y; - int16_t z; -} SRAWDATA; +typedef union { + struct { + int16_t acc_x; + int16_t acc_y; + int16_t acc_z; + int16_t mag_x; + int16_t mag_y; + int16_t mag_z; + } __attribute__ ((packed)) s; + int16_t v[6]; +} FXOSData_t; /** * A driver on top of mbed-I2C to operate the FXOS8700CQ accelerometer/magnetometer @@ -124,26 +129,26 @@ * @param sdl SCL pin * @param addr address of the I2C peripheral in (7-bit << 1) form */ - FXOS8700CQ(PinName sda, PinName scl, int addr); + FXOS8700CQ (PinName sda, PinName scl, int addr); /** * FXOS8700CQ destructor */ - ~FXOS8700CQ(void); + ~FXOS8700CQ (void); - void enable(void); - void disable(void); + void enable (void); + void disable (void); /** * @return the contents of device register FXOS8700CQ_WHOAMI 0x0D, * should be FXOS8700CQ_WHOAMI_VAL 0xC7 */ - uint8_t get_whoami(void); + uint8_t get_whoami (void); /** * @return the contents of device register FXOS8700CQ_STATUS 0x00 */ - uint8_t status(void); + uint8_t status (void); /** * Data retrieval from the FXOS8700CQ @@ -152,14 +157,14 @@ * @param magn_data destination XYZ magnetometer data struct * @return 0 on success, non-zero on failure */ - uint8_t get_data(SRAWDATA *accel_data, SRAWDATA *magn_data); + uint8_t get_data (FXOSData_t *data); /** * Retrieve the full-range scale value of the accelerometer * * @return 2, 4, or 8, depending on part configuration; 0 on error */ - uint8_t get_accel_scale(void); + uint8_t get_accel_scale (void);