From 1082a82fabd84fb2f23f182fa7f9ed76db9071d6 Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Fri, 1 Nov 2019 21:19:25 +0100
Subject: MPU9250: Add SparkFun constants and non-raw readout of Accel and Gyro

---
 src/app/mpu9250_motionlog/Makefile.inc |  2 ++
 src/app/mpu9250_motionlog/main.cc      | 66 ++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 src/app/mpu9250_motionlog/Makefile.inc
 create mode 100644 src/app/mpu9250_motionlog/main.cc

(limited to 'src/app')

diff --git a/src/app/mpu9250_motionlog/Makefile.inc b/src/app/mpu9250_motionlog/Makefile.inc
new file mode 100644
index 0000000..e273aaa
--- /dev/null
+++ b/src/app/mpu9250_motionlog/Makefile.inc
@@ -0,0 +1,2 @@
+arch_drivers += ,i2c
+drivers += ,mpu9250
diff --git a/src/app/mpu9250_motionlog/main.cc b/src/app/mpu9250_motionlog/main.cc
new file mode 100644
index 0000000..fc9ac1d
--- /dev/null
+++ b/src/app/mpu9250_motionlog/main.cc
@@ -0,0 +1,66 @@
+#include "arch.h"
+#include "driver/gpio.h"
+#include "driver/stdout.h"
+#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(DRIVER_SOFTI2C)
+#include "driver/i2c.h"
+#else
+#include "driver/soft_i2c.h"
+#endif
+#include "driver/mpu9250.h"
+
+#define UPDATE_MIN(min_val, val) if ((val) < (min_val)) { (min_val) = (val); }
+#define UPDATE_MAX(max_val, val) if ((val) > (max_val)) { (max_val) = (val); }
+
+int main(void)
+{
+	int ax, ay, az, gx, gy, gz;
+	int max_ax, max_ay, max_az, max_gx, max_gy, max_gz;
+	int min_ax, min_ay, min_az, min_gx, min_gy, min_gz;
+	unsigned char i = 0;
+
+	arch.setup();
+	gpio.setup();
+	kout.setup();
+
+	if (i2c.setup() != 0) {
+		kout << "I2C setup failed" << endl;
+		return 1;
+	}
+
+	kout << "I2C setup OK" << endl;
+
+	mpu9250.init();
+
+	while (1) {
+		mpu9250.getRawAccel(&ax, &ay, &az);
+		mpu9250.getRawGyro(&gx, &gy, &gz);
+
+		UPDATE_MIN(min_ax, ax);
+		UPDATE_MIN(min_ay, ay);
+		UPDATE_MIN(min_az, az);
+		UPDATE_MIN(min_gx, gx);
+		UPDATE_MIN(min_gy, gy);
+		UPDATE_MIN(min_gz, gz);
+		UPDATE_MAX(max_ax, ax);
+		UPDATE_MAX(max_ay, ay);
+		UPDATE_MAX(max_az, az);
+		UPDATE_MAX(max_gx, gx);
+		UPDATE_MAX(max_gy, gy);
+		UPDATE_MAX(max_gz, gz);
+
+		if (i++ == 200) {
+			kout << "Temp: " << mpu9250.getTemperature() << endl;
+			kout << "Min Accel: " << min_ax << " / " << min_ay << " / " << min_az << endl;
+			kout << "Max Accel: " << max_ax << " / " << max_ay << " / " << max_az << endl;
+			kout << "Min Gyro: " << min_gx << " / " << min_gy << " / " << min_gz << endl;
+			kout << "Max Gyro: " << max_gx << " / " << max_gy << " / " << max_gz << endl;
+			min_ax = min_ay = min_az = 0;
+			min_gx = min_gy = min_gz = 0;
+			max_ax = max_ay = max_az = 0;
+			max_gx = max_gy = max_gz = 0;
+			i = 0;
+		}
+	}
+
+	return 0;
+}
-- 
cgit v1.2.3