Skip to content

Commit

Permalink
MPU6050: Added fcn to directly get acc/gyro data
Browse files Browse the repository at this point in the history
This allows to skip the callback and receive data
with getter functions.
  • Loading branch information
jrotkiewicz committed May 18, 2023
1 parent f8f96ea commit 9913036
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions hw/drivers/sensors/mpu6050/include/mpu6050/mpu6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ int mpu6050_set_z_gyro_offset(struct sensor_itf *itf, int16_t offset);
int mpu6050_get_z_gyro_offset(struct sensor_itf *itf, int16_t *offset);
int mpu6050_set_offsets(struct mpu6050 *mpu, struct mpu6050_cfg *cfg);

int mpu6050_get_accel(struct mpu6050 *mpu6050, struct sensor_accel_data *sad);
int mpu6050_get_gyro(struct mpu6050 *mpu6050, struct sensor_gyro_data *sgd);

int mpu6050_init(struct os_dev *, void *);
int mpu6050_config(struct mpu6050 *, struct mpu6050_cfg *);

Expand Down
42 changes: 35 additions & 7 deletions hw/drivers/sensors/mpu6050/src/mpu6050.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include "modlog/modlog.h"
#include "stats/stats.h"
#include <syscfg/syscfg.h>
#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
#include <bus/drivers/i2c_common.h>
#endif

/* Define the stats section and records */
STATS_SECT_START(mpu6050_stat_section)
Expand Down Expand Up @@ -865,10 +868,16 @@ mpu6050_sensor_read(struct sensor *sensor, sensor_type_t type,
databuf.sad.sad_z = (z / lsb) * STANDARD_ACCEL_GRAVITY;
databuf.sad.sad_z_is_valid = 1;

rc = data_func(sensor, data_arg, &databuf.sad,
SENSOR_TYPE_ACCELEROMETER);
if (rc) {
return rc;
if (data_func != NULL) {
rc = data_func(sensor, data_arg, &databuf.sad,
SENSOR_TYPE_ACCELEROMETER);
if (rc) {
return rc;
}
} else {
struct sensor_accel_data* sad = data_arg;
memcpy(sad, &databuf.sad, sizeof(struct sensor_accel_data));
return 0;
}
}

Expand Down Expand Up @@ -907,15 +916,34 @@ mpu6050_sensor_read(struct sensor *sensor, sensor_type_t type,
databuf.sgd.sgd_z = z / lsb;
databuf.sgd.sgd_z_is_valid = 1;

rc = data_func(sensor, data_arg, &databuf.sgd, SENSOR_TYPE_GYROSCOPE);
if (rc) {
return rc;
if (data_func != NULL) {
rc = data_func(sensor, data_arg, &databuf.sgd, SENSOR_TYPE_GYROSCOPE);
if (rc) {
return rc;
}
} else {
struct sensor_gyro_data* sgd = data_arg;
memcpy(sgd, &databuf.sgd, sizeof(struct sensor_gyro_data));
}
}

return 0;
}

int
mpu6050_get_accel(struct mpu6050 *mpu6050, struct sensor_accel_data *sad)
{
return mpu6050_sensor_read(&mpu6050->sensor, SENSOR_TYPE_ACCELEROMETER,
NULL, sad, OS_TIMEOUT_NEVER);
}

int
mpu6050_get_gyro(struct mpu6050 *mpu6050, struct sensor_gyro_data *sgd)
{
return mpu6050_sensor_read(&mpu6050->sensor, SENSOR_TYPE_GYROSCOPE,
NULL, sgd, OS_TIMEOUT_NEVER);
}

static int
mpu6050_sensor_get_config(struct sensor *sensor, sensor_type_t type,
struct sensor_cfg *cfg)
Expand Down

0 comments on commit 9913036

Please sign in to comment.