From 4e70dc99f2fe61235c7e23739f91e3f7e2fff837 Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Thu, 25 Feb 2021 21:49:29 +0100
Subject: move datalog functions from i2cdetect into separate app

---
 src/app/datalogger/Kconfig      |   6 ++
 src/app/datalogger/Makefile.inc |  10 ++
 src/app/datalogger/main.cc      | 217 ++++++++++++++++++++++++++++++++++++++
 src/app/i2cdetect/Kconfig       |   2 +-
 src/app/i2cdetect/Makefile.inc  |   1 -
 src/app/i2cdetect/main.cc       | 225 ++++------------------------------------
 6 files changed, 256 insertions(+), 205 deletions(-)
 create mode 100644 src/app/datalogger/Kconfig
 create mode 100644 src/app/datalogger/Makefile.inc
 create mode 100644 src/app/datalogger/main.cc

diff --git a/src/app/datalogger/Kconfig b/src/app/datalogger/Kconfig
new file mode 100644
index 0000000..5364fac
--- /dev/null
+++ b/src/app/datalogger/Kconfig
@@ -0,0 +1,6 @@
+# Copyright 2020 Daniel Friesel
+#
+# SPDX-License-Identifier: CC0-1.0
+
+prompt "Data Logger for Arbitrary Devices"
+depends on loop && !wakeup
diff --git a/src/app/datalogger/Makefile.inc b/src/app/datalogger/Makefile.inc
new file mode 100644
index 0000000..d2a9de5
--- /dev/null
+++ b/src/app/datalogger/Makefile.inc
@@ -0,0 +1,10 @@
+# vim:ft=make
+#
+# Copyright 2020 Daniel Friesel
+#
+# SPDX-License-Identifier: CC0-1.0
+
+ifdef app
+	CONFIG_loop = y
+	override arch_drivers += ,i2c
+endif
diff --git a/src/app/datalogger/main.cc b/src/app/datalogger/main.cc
new file mode 100644
index 0000000..8312367
--- /dev/null
+++ b/src/app/datalogger/main.cc
@@ -0,0 +1,217 @@
+/*
+ * Copyright 2020 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "arch.h"
+#include "driver/gpio.h"
+#include "driver/stdout.h"
+
+#if defined(DRIVER_I2C)
+#include "driver/i2c.h"
+#elif defined(DRIVER_SOFTI2C)
+#include "driver/soft_i2c.h"
+#endif
+
+#ifdef DRIVER_LM75
+#include "driver/lm75.h"
+#endif
+#ifdef DRIVER_S5851A
+#include "driver/s5851a.h"
+#endif
+#ifdef DRIVER_AM2320
+#include "driver/am2320.h"
+#endif
+#ifdef DRIVER_BME280
+#include "driver/bme280.h"
+#include "driver/bme680_util.h"
+#endif
+#ifdef DRIVER_BME680
+#include "driver/bme680.h"
+#include "driver/bme680_util.h"
+#endif
+#ifdef DRIVER_CCS811
+#include "driver/ccs811.h"
+#endif
+#ifdef DRIVER_MAX44009
+#include "driver/max44009.h"
+#endif
+#ifdef DRIVER_HDC1080
+#include "driver/hdc1080.h"
+#endif
+#ifdef DRIVER_MPU9250
+#include "driver/mpu9250.h"
+#endif
+
+void loop(void)
+{
+#ifdef DRIVER_LM75
+	kout << "temperature_celsius: ";
+	kout.printf_float(lm75.getTemp());
+	kout << endl;
+#endif
+
+#ifdef DRIVER_S5851A
+	kout << "temperature_celsius: ";
+	kout.printf_float(s5851a.getTemp());
+	kout << endl;
+#endif
+
+#ifdef DRIVER_AM2320
+	am2320.read();
+	if (am2320.getStatus() == 0) {
+		kout.printf_float(am2320.getTemp());
+		kout << " degC @ ";
+		kout.printf_float(am2320.getHumidity());
+		kout << " rel%" << endl;
+	} else {
+		kout << "AM2320 error " << dec << am2320.getStatus() << endl;
+	}
+#endif
+
+#ifdef DRIVER_BME280
+	struct bme280_data comp_data;
+	int8_t rslt = bme280.getSensorData(BME280_ALL, &comp_data);
+	kout << "BME280 read " << rslt << endl;
+	kout << "BME280 temperature " << (float)comp_data.temperature / 100 << " degC" << endl;
+	kout << "BME280 humidity " << (float)comp_data.humidity / 1024 << " %" << endl;
+	kout << "BME280 pressure " << (float)comp_data.pressure / 100 << " Pa" << endl;
+#endif
+
+#ifdef DRIVER_BME680
+	struct bme680_field_data data;
+	bme680.setSensorMode();
+	arch.delay_ms(250);
+	bme680.getSensorData(&data);
+	kout << "BME680 temperature " << (float)data.temperature / 100 << " degC" << endl;
+	kout << "BME680 humidity " << (float)data.humidity / 1000  << " %" << endl;
+	kout << "BME680 pressure " << (float)data.pressure / 100 << " hPa" << endl;
+	kout << "BME680 gas resistance " << data.gas_resistance << endl;
+#endif
+
+#ifdef DRIVER_CCS811
+	ccs811.read();
+	kout << bin;
+	kout << "CCS811 status / error: " << ccs811.status << " / " << ccs811.error_id << endl;
+	kout << dec;
+	kout << "CCS811 tVOC / eCO₂ : " << ccs811.tvoc << " ppb / " << ccs811.eco2 << " ppm" << endl;
+#endif
+
+#ifdef DRIVER_HDC1080
+	/*
+	hdc1080.heater(1);
+	for (unsigned char i = 0; i < 50; i++) {
+		hdc1080.getTemp();
+	}
+	*/
+	kout << "HDC1080 temperature " << hdc1080.getTemp() << " degC" << endl;
+	kout << "HDC1080 humidity " << hdc1080.getRH() << " %H" << endl;
+#endif
+
+#ifdef DRIVER_MPU9250
+	int mx, my, mz;
+	kout << "Temperature: " << mpu9250.getTemperature() << endl;
+	kout << "Accel X " << mpu9250.getAccelX() << endl;
+	kout << "Accel Y " << mpu9250.getAccelY() << endl;
+	kout << "Accel Z " << mpu9250.getAccelZ() << endl;
+	kout << "Gyro X " << mpu9250.getGyroX() << endl;
+	kout << "Gyro Y " << mpu9250.getGyroY() << endl;
+	kout << "Gyro Z " << mpu9250.getGyroZ() << endl;
+	mpu9250.getRawMagnet(&mx, &my, &mz);
+	kout << "Magnet X " << mx << endl;
+	kout << "Magnet Y " << my << endl;
+	kout << "Magnet Z " << mz << endl;
+#endif
+
+#ifdef DRIVER_MAX44009
+	kout.printf_float(max44009.getLux());
+	kout << endl;
+#endif
+}
+
+int main(void)
+{
+
+	arch.setup();
+	gpio.setup();
+	kout.setup();
+
+#if defined(DRIVER_I2C) || defined(DRIVER_SOFTI2C)
+	if (i2c.setup() != 0) {
+		kout << "I2C setup FAILED" << endl;
+		return 1;
+	}
+	kout << "I2C setup OK" << endl;
+#endif
+
+#ifdef DRIVER_BME280
+	bme280.intf = BME280_I2C_INTF;
+	bme280.read = bme680_i2c_read;
+	bme280.write = bme680_i2c_write;
+	bme280.delay_ms = bme680_delay_ms;
+
+	int8_t rslt = BME280_OK;
+	rslt = bme280.init();
+	kout << "BME280 init " << rslt << endl;
+
+	bme280.settings.osr_p = BME280_OVERSAMPLING_16X;
+	bme280.settings.osr_t = BME280_OVERSAMPLING_16X;
+	bme280.settings.osr_h = BME280_OVERSAMPLING_16X;
+	bme280.settings.filter = BME280_FILTER_COEFF_OFF;
+	bme280.settings.standby_time = BME280_STANDBY_TIME_500_MS;
+	bme280.setSensorSettings(BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL | BME280_STANDBY_SEL);
+	bme280.enterNormalMode();
+#endif
+
+#ifdef DRIVER_BME680
+	bme680.intf = BME680_I2C_INTF;
+	bme680.read = bme680_i2c_read;
+	bme680.write = bme680_i2c_write;
+	bme680.delay_ms = bme680_delay_ms;
+
+	bme680.amb_temp = 25;
+
+	int8_t rslt = BME680_OK;
+	rslt = bme680.init();
+	kout << "BME680 init " << rslt << endl;
+
+	bme680.power_mode = BME680_FORCED_MODE;
+	bme680.tph_sett.os_hum = BME680_OS_1X;
+	bme680.tph_sett.os_pres = BME680_OS_16X;
+	bme680.tph_sett.os_temp = BME680_OS_2X;
+
+	bme680.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
+	bme680.gas_sett.heatr_dur = 100;
+	bme680.gas_sett.heatr_temp = 300;
+	bme680.setSensorSettings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL);
+#endif
+
+#ifdef DRIVER_CCS811
+	kout << hex;
+	kout << "CCS811 HWID: " << ccs811.getManufacturerID() << endl;
+	arch.delay_ms(65);
+	kout << "CCS811 status: " << ccs811.getStatus() << endl;
+	arch.delay_ms(65);
+	ccs811.init();
+	arch.delay_ms(65);
+	kout << "CCS811 status: " << ccs811.getStatus() << endl;
+	kout << dec;
+	ccs811.setMode(1);
+	arch.delay_ms(50);
+#endif
+
+#ifdef DRIVER_HDC1080
+	hdc1080.init();
+	if (hdc1080.getManufacturerID() != 0x5449) {
+		kout << "[!] invalid HDC1080 manufacturer ID: " << hex << hdc1080.getManufacturerID() << endl;
+	}
+#endif
+
+#ifdef DRIVER_MPU9250
+	mpu9250.init();
+#endif
+
+	arch.idle_loop();
+
+	return 0;
+}
diff --git a/src/app/i2cdetect/Kconfig b/src/app/i2cdetect/Kconfig
index fe4de80..c8c6744 100644
--- a/src/app/i2cdetect/Kconfig
+++ b/src/app/i2cdetect/Kconfig
@@ -3,4 +3,4 @@
 # SPDX-License-Identifier: CC0-1.0
 
 prompt "I2C Bus Scanner"
-depends on loop && !wakeup && meta_driver_i2c
+depends on !loop && !wakeup && meta_driver_i2c
diff --git a/src/app/i2cdetect/Makefile.inc b/src/app/i2cdetect/Makefile.inc
index d2a9de5..501e914 100644
--- a/src/app/i2cdetect/Makefile.inc
+++ b/src/app/i2cdetect/Makefile.inc
@@ -5,6 +5,5 @@
 # SPDX-License-Identifier: CC0-1.0
 
 ifdef app
-	CONFIG_loop = y
 	override arch_drivers += ,i2c
 endif
diff --git a/src/app/i2cdetect/main.cc b/src/app/i2cdetect/main.cc
index 27e35bf..fb6f54c 100644
--- a/src/app/i2cdetect/main.cc
+++ b/src/app/i2cdetect/main.cc
@@ -11,168 +11,6 @@
 #else
 #include "driver/soft_i2c.h"
 #endif
-#ifdef DRIVER_LM75
-#include "driver/lm75.h"
-#endif
-#ifdef DRIVER_S5851A
-#include "driver/s5851a.h"
-#endif
-#ifdef DRIVER_AM2320
-#include "driver/am2320.h"
-#endif
-#ifdef DRIVER_BME280
-#include "driver/bme280.h"
-#include "driver/bme680_util.h"
-#endif
-#ifdef DRIVER_BME680
-#include "driver/bme680.h"
-#include "driver/bme680_util.h"
-#endif
-#ifdef DRIVER_CCS811
-#include "driver/ccs811.h"
-#endif
-#ifdef DRIVER_EEPROM24LC64
-#include "driver/eeprom24lc64.h"
-#endif
-#ifdef DRIVER_MAX44009
-#include "driver/max44009.h"
-#endif
-#ifdef DRIVER_HDC1080
-#include "driver/hdc1080.h"
-#endif
-#ifdef DRIVER_MPU9250
-#include "driver/mpu9250.h"
-#endif
-#ifdef DRIVER_MMSIMPLE
-#include "driver/mmsimple.h"
-#endif
-
-void loop(void)
-{
-#ifdef DRIVER_LM75
-	kout.printf_float(lm75.getTemp());
-	kout << endl;
-#endif
-#ifdef DRIVER_S5851A
-	kout.printf_float(s5851a.getTemp());
-	kout << endl;
-#endif
-#ifdef DRIVER_AM2320
-	am2320.read();
-	if (am2320.getStatus() == 0) {
-		kout.printf_float(am2320.getTemp());
-		kout << " degC @ ";
-		kout.printf_float(am2320.getHumidity());
-		kout << " rel%" << endl;
-	} else {
-		kout << "AM2320 error " << dec << am2320.getStatus() << endl;
-	}
-#endif
-#ifdef DRIVER_BME280
-	bme280.intf = BME280_I2C_INTF;
-	bme280.read = bme680_i2c_read;
-	bme280.write = bme680_i2c_write;
-	bme280.delay_ms = bme680_delay_ms;
-
-	int8_t rslt = BME280_OK;
-	struct bme280_data comp_data;
-	rslt = bme280.init();
-	kout << "BME280 init " << rslt << endl;
-	bme280.settings.osr_p = BME280_OVERSAMPLING_16X;
-	bme280.settings.osr_t = BME280_OVERSAMPLING_16X;
-	bme280.settings.osr_h = BME280_OVERSAMPLING_16X;
-	bme280.settings.filter = BME280_FILTER_COEFF_OFF;
-	bme280.settings.standby_time = BME280_STANDBY_TIME_500_MS;
-	bme280.setSensorSettings(BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL | BME280_STANDBY_SEL);
-	bme280.enterNormalMode();
-	arch.delay_ms(100);
-	rslt = bme280.getSensorData(BME280_ALL, &comp_data);
-	kout << "BME280 read " << rslt << endl;
-	kout << "BME280 temperature " << (float)comp_data.temperature / 100 << " degC" << endl;
-	kout << "BME280 humidity " << (float)comp_data.humidity / 1024 << " %" << endl;
-	kout << "BME280 pressure " << (float)comp_data.pressure / 100 << " Pa" << endl;
-#endif
-#ifdef DRIVER_BME680
-	bme680.intf = BME680_I2C_INTF;
-	bme680.read = bme680_i2c_read;
-	bme680.write = bme680_i2c_write;
-	bme680.delay_ms = bme680_delay_ms;
-	/* amb_temp can be set to 25 prior to configuring the gas sensor 
-	* or by performing a few temperature readings without operating the gas sensor.
-	*/
-	bme680.amb_temp = 25;
-
-	int8_t rslt = BME680_OK;
-	rslt = bme680.init();
-	kout << "BME680 init " << rslt << endl;
-
-	bme680.power_mode = BME680_FORCED_MODE;
-	bme680.tph_sett.os_hum = BME680_OS_1X;
-	bme680.tph_sett.os_pres = BME680_OS_16X;
-	bme680.tph_sett.os_temp = BME680_OS_2X;
-
-	bme680.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
-	bme680.gas_sett.heatr_dur = 30;
-	bme680.gas_sett.heatr_temp = 300;
-	bme680.setSensorSettings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL);
-	bme680.setSensorMode();
-	arch.delay_ms(200);
-	struct bme680_field_data data;
-	bme680.getSensorData(&data);
-	kout << "BME680 temperature " << (float)data.temperature / 100 << " degC" << endl;
-	kout << "BME680 humidity " << (float)data.humidity / 1000  << " %" << endl;
-	kout << "BME680 pressure " << (float)data.pressure / 100 << " hPa" << endl;
-	kout << "BME680 gas resistance " << data.gas_resistance << endl;
-#endif
-#ifdef DRIVER_CCS811
-	ccs811.read();
-	kout << bin;
-	kout << "CCS811 status / error: " << ccs811.status << " / " << ccs811.error_id << endl;
-	kout << dec;
-	kout << "CCS811 tVOC / eCO₂ : " << ccs811.tvoc << " ppb / " << ccs811.eco2 << " ppm" << endl;
-#endif
-#ifdef DRIVER_HDC1080
-	/*
-	hdc1080.heater(1);
-	for (unsigned char i = 0; i < 50; i++) {
-		hdc1080.getTemp();
-	}
-	*/
-	kout << "HDC1080 temperature " << hdc1080.getTemp() << " degC" << endl;
-	kout << "HDC1080 humidity " << hdc1080.getRH() << " %H" << endl;
-#endif
-#ifdef DRIVER_MPU9250
-	int mx, my, mz;
-	kout << "Temperature: " << mpu9250.getTemperature() << endl;
-	kout << "Accel X " << mpu9250.getAccelX() << endl;
-	kout << "Accel Y " << mpu9250.getAccelY() << endl;
-	kout << "Accel Z " << mpu9250.getAccelZ() << endl;
-	kout << "Gyro X " << mpu9250.getGyroX() << endl;
-	kout << "Gyro Y " << mpu9250.getGyroY() << endl;
-	kout << "Gyro Z " << mpu9250.getGyroZ() << endl;
-	mpu9250.getRawMagnet(&mx, &my, &mz);
-	kout << "Magnet X " << mx << endl;
-	kout << "Magnet Y " << my << endl;
-	kout << "Magnet Z " << mz << endl;
-#endif
-#ifdef DRIVER_MAX44009
-	kout.printf_float(max44009.getLux());
-	kout << endl;
-#endif
-#ifdef DRIVER_EEPROM24LC64
-	char buf[33];
-	static unsigned char page = 0;
-	eeprom24lc64.writePage(page, "Hello, World! Und so weiter, lol");
-	arch.delay_ms(10);
-	eeprom24lc64.readPage(page, buf);
-	buf[32] = '\0';
-	kout << "Address " << page << ": " << buf << endl;
-	page++;
-#endif
-#ifdef DRIVER_MMSIMPLE
-	moody.toggleBlue();
-#endif
-}
 
 unsigned int i2c_status[128 / (8 * sizeof(unsigned int)) + 1];
 
@@ -184,56 +22,37 @@ int main(void)
 	kout.setup();
 
 	if (i2c.setup() != 0) {
+		kout << "I2C setup FAILED" << endl;
 		return 1;
 	}
 
 	kout << "I2C setup OK" << endl;
 
-#ifdef DRIVER_CCS811
-	kout << hex;
-	kout << "CCS811 HWID: " << ccs811.getManufacturerID() << endl;
-	arch.delay_ms(65);
-	kout << "CCS811 status: " << ccs811.getStatus() << endl;
-	arch.delay_ms(65);
-	ccs811.init();
-	arch.delay_ms(65);
-	kout << "CCS811 status: " << ccs811.getStatus() << endl;
-	kout << dec;
-	ccs811.setMode(2);
-	//arch.delay_ms(50);
-#endif
-#ifdef DRIVER_HDC1080
-	hdc1080.init();
-	if (hdc1080.getManufacturerID() != 0x5449) {
-		kout << "[!] invalid HDC1080 manufacturer ID: " << hex << hdc1080.getManufacturerID() << endl;
-	}
-#endif
-#ifdef DRIVER_MPU9250
-	mpu9250.init();
-#endif
-
-	for (unsigned char i = 0; i < sizeof(i2c_status)/sizeof(unsigned int); i++) {
-		i2c_status[i] = 0;
-	}
-
-	i2c.scan(i2c_status);
-
-	kout << "     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f";
-	for (unsigned char address = 0; address < 128; address++) {
-		if ((address & 0x0f) == 0) {
-			kout << endl;
-			kout.printf_uint8(address);
-			kout << ":";
+	while (1) {
+		for (unsigned char i = 0; i < sizeof(i2c_status)/sizeof(unsigned int); i++) {
+			i2c_status[i] = 0;
 		}
 
-		if (i2c_status[address / (8 * sizeof(unsigned int))] & (1 << (address % (8 * sizeof(unsigned int))))) {
-			kout << " ";
-			kout.printf_uint8(address);
-		} else {
-			kout << " --";
+		i2c.scan(i2c_status);
+
+		kout << "     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f";
+		for (unsigned char address = 0; address < 128; address++) {
+			if ((address & 0x0f) == 0) {
+				kout << endl;
+				kout.printf_uint8(address);
+				kout << ":";
+			}
+
+			if (i2c_status[address / (8 * sizeof(unsigned int))] & (1 << (address % (8 * sizeof(unsigned int))))) {
+				kout << " ";
+				kout.printf_uint8(address);
+			} else {
+				kout << " --";
+			}
 		}
+		kout << endl << endl;
+		arch.delay_ms(1000);
 	}
-	kout << endl;
 
 	arch.idle_loop();
 
-- 
cgit v1.2.3