Add MPU9250

This commit is contained in:
Pim van Pelt
2018-04-18 22:54:06 +02:00
parent e4bb9e39cc
commit 1e1c3087cd
4 changed files with 553 additions and 7 deletions

View File

@ -21,6 +21,7 @@
#include "mgos_htu21df.h"
#include "mgos_mcp9808.h"
#include "mgos_ccs811.h"
#include "mgos_mpu9250.h"
#include <fcntl.h>
#include <sys/ioctl.h>
@ -131,6 +132,27 @@ bool do_mcp9808(struct mgos_mcp9808 *sensor) {
return true;
}
bool do_mpu9250(struct mgos_mpu9250 *sensor) {
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
if (!sensor) {
return false;
}
if (mgos_mpu9250_get_accelerometer(sensor, &ax, &ay, &az)) {
LOG(LL_INFO, ("Accel X=%.2f Y=%.2f Z=%.2f", ax, ay, az));
}
if (mgos_mpu9250_get_gyroscope(sensor, &gx, &gy, &gz)) {
LOG(LL_INFO, ("Gyro X=%.2f Y=%.2f Z=%.2f", gx, gy, gz));
}
if (mgos_mpu9250_get_magnetometer(sensor, &mx, &my, &mz)) {
LOG(LL_INFO, ("Mag X=%.2f Y=%.2f Z=%.2f", mx, my, mz));
}
return true;
}
int main() {
struct mgos_i2c * i2c;
struct mgos_si7021 * si7021;
@ -138,6 +160,7 @@ int main() {
struct mgos_htu21df *htu21df;
struct mgos_mcp9808 *mcp9808;
struct mgos_ccs811 * ccs811;
struct mgos_mpu9250 *mpu9250;
if (!mgos_i2c_open(I2CBUSNR)) {
LOG(LL_ERROR, ("Cannot open I2C bus %u", I2CBUSNR));
@ -153,19 +176,23 @@ int main() {
/*
* if (!(sht31 = mgos_sht31_create(i2c, 0x44)))
* LOG(LL_ERROR, ("Cannot create SHT31 device"));
*
* if (!(si7021 = mgos_si7021_create(i2c, 0x40)))
* LOG(LL_ERROR, ("Cannot create SI7021 device"));
*
* if (!(htu21df = mgos_htu21df_create(i2c, 0x40)))
* LOG(LL_ERROR, ("Cannot create HTU21DF device"));
*
* if (!(mcp9808 = mgos_mcp9808_create(i2c, 0x18)))
* LOG(LL_ERROR, ("Cannot create MCP9808 device"));
* if (!(ccs811 = mgos_ccs811_create(i2c, 0x5A)))
* LOG(LL_ERROR, ("Cannot create CCS811 device"));
*/
if (!(ccs811 = mgos_ccs811_create(i2c, 0x5A))) {
LOG(LL_ERROR, ("Cannot create CCS811 device"));
if (!(mpu9250 = mgos_mpu9250_create(i2c, 0x68))) {
LOG(LL_ERROR, ("Cannot create MPU9250 device"));
} else {
mgos_mpu9250_set_accelerometer_range(mpu9250, RANGE_2G);
mgos_mpu9250_set_gyroscope_range(mpu9250, RANGE_GYRO_250);
mgos_mpu9250_set_magnetometer_scale(mpu9250, SCALE_14_BITS);
mgos_mpu9250_set_magnetometer_speed(mpu9250, MAG_100_HZ);
}
for (;;) {
@ -174,9 +201,10 @@ int main() {
* do_si7021(si7021);
* do_htu21df(htu21df);
* do_mcp9808(mcp9808);
* do_ccs811(ccs811);
*/
do_ccs811(ccs811);
sleep(5);
do_mpu9250(mpu9250);
sleep(1);
}
mgos_sht31_destroy(&sht31);
@ -184,6 +212,7 @@ int main() {
mgos_htu21df_destroy(&htu21df);
mgos_mcp9808_destroy(&mcp9808);
mgos_ccs811_destroy(&ccs811);
mgos_mpu9250_destroy(&mpu9250);
return 0;
}