Maxim Integrated's IoT development kit

Dependencies:   MAX30101 MAX30003 MAX113XX_Pixi MAX30205 max32630fthr USBDevice

Files at this revision

API Documentation at this revision

Comitter:
Mahir Ozturk
Date:
Thu Mar 15 17:13:40 2018 +0300
Parent:
1:efe9cad8942f
Child:
3:7e9a93b6b82b
Commit message:
Add BLE Notification period setting, code cleaning

Change-Id: Ib49ed44fe0fb2ccf03f2379e1cd086e5a36e7e49

Changed in this revision

.cproject Show annotated file Show diff for this revision Revisions of this file
Kconfig Show annotated file Show diff for this revision Revisions of this file
Makefile Show annotated file Show diff for this revision Revisions of this file
configs/pegasus.config Show diff for this revision Revisions of this file
configs/pegasus_iot.config Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/.cproject	Tue Mar 13 14:52:59 2018 +0300
+++ b/.cproject	Thu Mar 15 17:13:40 2018 +0300
@@ -503,6 +503,11 @@
 									<listOptionValue builtIn="false" value="LIB_MAX113XX_PIXI"/>
 									<listOptionValue builtIn="false" value="LIB_MAX30101"/>
 									<listOptionValue builtIn="false" value="LIB_MAX30205"/>
+									<listOptionValue builtIn="false" value="MAX30205_BLE_NOTIFY_PERIOD_SEC=5"/>
+									<listOptionValue builtIn="false" value="MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC=5"/>
+									<listOptionValue builtIn="false" value="MAX30003_BLE_NOTIFY_PERIOD_SEC=5"/>
+									<listOptionValue builtIn="false" value="MAX30101_BLE_NOTIFY_PERIOD_SEC=5"/>
+									<listOptionValue builtIn="false" value="BLE_SUPPORT"/>
 								</option>
 								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.include.files.758850743" name="Include files (-include)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.include.files" useByScannerDiscovery="true" valueType="includeFiles">
 									<listOptionValue builtIn="false" value="&quot;${ProjDirPath}/mbed_config.h&quot;"/>
--- a/Kconfig	Tue Mar 13 14:52:59 2018 +0300
+++ b/Kconfig	Thu Mar 15 17:13:40 2018 +0300
@@ -35,15 +35,63 @@
 
 endmenu
 
+config BLE_SUPPORT
+	bool "Enable Bluetooth Low Energy Support"
+
 menu "Maxim IoT Devices"
+
 config LIBRARY_MAX30205
 	bool "MAX30205 - Human Body Temperature Sensor"
+
+if LIBRARY_MAX30205
+
+config MAX30205_BLE_NOTIFY_PERIOD_SEC	
+	int "BLE Notification period (sec)"
+	range 1 60 
+	default "5"
+	depends on BLE_SUPPORT
+
+endif
+	
 config LIBRARY_MAX113XX_PIXI
 	bool "MAX11300/01/11/12 - Programmable Mixed-Signal I/O"
+	
+if LIBRARY_MAX113XX_PIXI
+
+config MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC	
+	int "BLE Notification period (sec)"
+	range 1 60 
+	default "5"
+	depends on BLE_SUPPORT
+
+endif
+		
 config LIBRARY_MAX30003
 	bool "MAX30003 - Single-Channel Integrated Biopotential (ECG, R to R Detection) AFE"
+	
+if LIBRARY_MAX30003
+
+config MAX30003_BLE_NOTIFY_PERIOD_SEC	
+	int "BLE Notification period (sec)"
+	range 1 60 
+	default "5"
+	depends on BLE_SUPPORT
+
+endif
+	
 config LIBRARY_MAX30101
 	bool "MAX30101 - High-Sensitivity Pulse Oximeter and Heart-Rate Sensor"
+	
+if LIBRARY_MAX30101
+
+config MAX30101_BLE_NOTIFY_PERIOD_SEC	
+	int "BLE Notification period (sec)"
+	range 1 60 
+	default "5"
+	depends on BLE_SUPPORT
+
+endif	
+	
 endmenu
 
 menu "Memory debug setup"
--- a/Makefile	Tue Mar 13 14:52:59 2018 +0300
+++ b/Makefile	Thu Mar 15 17:13:40 2018 +0300
@@ -74,23 +74,39 @@
 ## END PLATFORM ##
 
 ## BEGIN Maxim IoT Platform ##
+ifeq ($(CONFIG_BLE_SUPPORT), y)
+CONFIG_FLAGS += BLE_SUPPORT
+endif
+
 ifeq ($(CONFIG_LIBRARY_MAX30205), y)
 CONFIG_FLAGS += LIB_MAX30205
+ifeq ($(CONFIG_BLE_SUPPORT), y)
+CONFIG_FLAGS += MAX30205_BLE_NOTIFY_PERIOD_SEC=$(CONFIG_MAX30205_BLE_NOTIFY_PERIOD_SEC)
+endif
 PROJECT_SOURCES += MAX30205
 endif
 
 ifeq ($(CONFIG_LIBRARY_MAX113XX_PIXI), y)
 CONFIG_FLAGS += LIB_MAX113XX_PIXI
+ifeq ($(CONFIG_BLE_SUPPORT), y)
+CONFIG_FLAGS += MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC=$(CONFIG_MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC)
+endif
 PROJECT_SOURCES += MAX113XX_Pixi
 endif
 
 ifeq ($(CONFIG_LIBRARY_MAX30003), y)
 CONFIG_FLAGS += LIB_MAX30003
+ifeq ($(CONFIG_BLE_SUPPORT), y)
+CONFIG_FLAGS += MAX30003_BLE_NOTIFY_PERIOD_SEC=$(CONFIG_MAX30003_BLE_NOTIFY_PERIOD_SEC)
+endif
 PROJECT_SOURCES += MAX30003
 endif
 
 ifeq ($(CONFIG_LIBRARY_MAX30101), y)
 CONFIG_FLAGS += LIB_MAX30101
+ifeq ($(CONFIG_BLE_SUPPORT), y)
+CONFIG_FLAGS += MAX30101_BLE_NOTIFY_PERIOD_SEC=$(CONFIG_MAX30101_BLE_NOTIFY_PERIOD_SEC)
+endif
 PROJECT_SOURCES += MAX30101
 endif
 ## END Maxim IoT Platform ##
--- a/configs/pegasus.config	Tue Mar 13 14:52:59 2018 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-#
-# Automatically generated file; DO NOT EDIT.
-# MAXIM MAX326XXX mBED EVKit platform configuration
-#
-CONFIG_CONFIG_NAME="pegasus_os58"
-
-#
-# Platform setup
-#
-# CONFIG_PLATFORM_MAX32600MBED is not set
-# CONFIG_PLATFORM_MAX32625MBED is not set
-CONFIG_PLATFORM_MAX32630FTHR=y
-CONFIG_MAXIM_PLATFORM_NAME="pegasus"
-
-#
-# PPG Sensor setup
-#
-CONFIG_ENABLE_PPG=y
-# CONFIG_PPG_MAX3011X is not set
-CONFIG_PPG_MAX8614X=y
-
-#
-# Accelerometer setup
-#
-CONFIG_ENABLE_ACCEL=y
-# CONFIG_ACCEL_BMI160 is not set
-CONFIG_ACCEL_BMA280=y
-# CONFIG_ACCEL_LIS2DH is not set
-
-#
-# OLED setup
-#
-CONFIG_ENABLE_OLED=y
-CONFIG_OLED_ADAFRUIT_SSD1306=y
-
-BLE_SUPPORT=y
-ENABLE_ACCEL=y
-#
-# Memory debug setup
-#
-# CONFIG_ENABLE_MEMORY_DEBUG is not set
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/configs/pegasus_iot.config	Thu Mar 15 17:13:40 2018 +0300
@@ -0,0 +1,32 @@
+#
+# Automatically generated file; DO NOT EDIT.
+# MAXIM MAX326XXX mBED EVKit platform configuration
+#
+CONFIG_CONFIG_NAME="pegasus_iot"
+
+#
+# Platform setup
+#
+# CONFIG_PLATFORM_MAX32600MBED is not set
+# CONFIG_PLATFORM_MAX32625MBED is not set
+CONFIG_PLATFORM_MAX32630FTHR=y
+# CONFIG_PLATFORM_MAX32625PICO is not set
+CONFIG_MAXIM_PLATFORM_NAME="Pegasus-IoT"
+CONFIG_BLE_SUPPORT=y
+
+#
+# Maxim IoT Devices
+#
+CONFIG_LIBRARY_MAX30205=y
+CONFIG_MAX30205_BLE_NOTIFY_PERIOD_SEC=5
+CONFIG_LIBRARY_MAX113XX_PIXI=y
+CONFIG_MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC=5
+CONFIG_LIBRARY_MAX30003=y
+CONFIG_MAX30003_BLE_NOTIFY_PERIOD_SEC=5
+CONFIG_LIBRARY_MAX30101=y
+CONFIG_MAX30101_BLE_NOTIFY_PERIOD_SEC=5
+
+#
+# Memory debug setup
+#
+# CONFIG_ENABLE_MEMORY_DEBUG is not set
--- a/main.cpp	Tue Mar 13 14:52:59 2018 +0300
+++ b/main.cpp	Thu Mar 15 17:13:40 2018 +0300
@@ -74,6 +74,8 @@
 /* Hardware serial port over DAPLink */
 Serial daplink(USBTX, USBRX, 115200);
 
+int aliveLedEventId;
+
 /******************************************************************************/
 const static char     DEVICE_NAME[] = MAXIM_PLATFORM_NAME;
 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
@@ -193,7 +195,7 @@
 
 void blinkCallback(void)
 {
-    //led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
+	gLED = !gLED;
 }
 
 void onBleInitError(BLE &ble, ble_error_t error)
@@ -260,7 +262,7 @@
  ******************************************************************************/
 #if defined(LIB_MAX30205)
 
-#define MAX30205_DATA_READ_PERIOD_MSEC	2000
+#define MAX30205_DATA_READ_PERIOD_MSEC	5000
 
 MAX30205 max30205_temp_sensor(i2c1, 0x48);	/* New MAX30205 on i2cBus */
 
@@ -314,15 +316,12 @@
 			bleGattAttrWrite(gattCharTemp.getValueHandle(), (uint8_t *)&temperature, sizeof(temperature));
 			daplink.printf("Temperature is %2.3f deg. C\r\n", temperature);
 
-			Thread::wait(MAX30205_DATA_READ_PERIOD_MSEC);
+			Thread::wait(MAX30205_BLE_NOTIFY_PERIOD_SEC * 1000);
 
 		} else {
 			daplink.printf("Something went wrong, check the I2C bus and power connections...\r\n");
 
-			while (1) {
-				rLED = !rLED;
-				Thread::wait(500);
-			}
+			return;
 		}
 	}
 }
@@ -478,6 +477,9 @@
 	uint16_t HRTemp;
 	uint16_t spo2Temp;
 
+	uint16_t lastValidHR = 0;
+	uint16_t lastValidSPO2 = 0;
+
 	int r=0; //counter for redData position
 	int ir=0; //counter for irData position
 	int g =0; //counter for greenData position
@@ -486,6 +488,10 @@
 	daplink.printf("Starting MAX30101 HeartRate / SPO2 Demo Application...\r\n");
 	daplink.printf("Please wait a few seconds while data is being collected.\r\n");
 
+    Timer bleNotifyTimer;
+
+    bleNotifyTimer.start();
+
 	while (1) {
 		if (rc == 0) {
 			/* Check if op_sensor interrupt asserted */
@@ -537,13 +543,13 @@
 						if (DRdy == 1) {
 							daplink.printf("Heart Rate = %i\r\n",HRbpm2);
 							daplink.printf("SPO2 = %i\r\n",SpO2B);
-							bleGattAttrWrite(gattCharHeartRate.getValueHandle(), (uint8_t *)&HRbpm2, sizeof(HRbpm2));
-							bleGattAttrWrite(gattCharSPO2.getValueHandle(), (uint8_t *)&SpO2B, sizeof(SpO2B));
+							lastValidHR = HRbpm2;
+							lastValidSPO2 = SpO2B;
 						} else if (HRTemp != 0) { /* if a valid heart was calculated at all, it is printed */
 							daplink.printf("Heart Rate = %i\r\n",HRTemp);
 							daplink.printf("SPO2 = %i\r\n",spo2Temp);
-							bleGattAttrWrite(gattCharHeartRate.getValueHandle(), (uint8_t *)&HRTemp, sizeof(HRTemp));
-							bleGattAttrWrite(gattCharSPO2.getValueHandle(), (uint8_t *)&spo2Temp, sizeof(spo2Temp));
+							lastValidHR = HRTemp;
+							lastValidSPO2 = spo2Temp;
 						} else {
 							daplink.printf("Calculation failed...waiting for more samples...\r\n");
 							daplink.printf("Please keep your finger on the MAX30101 sensor with minimal movement.\r\n");
@@ -561,6 +567,12 @@
 						ir = 400;
 						g = 400;
 					}
+
+					if (bleNotifyTimer.read_ms() >= (MAX30101_BLE_NOTIFY_PERIOD_SEC * 1000)) {
+						bleGattAttrWrite(gattCharHeartRate.getValueHandle(), (uint8_t *)&lastValidHR, sizeof(lastValidHR));
+						bleGattAttrWrite(gattCharSPO2.getValueHandle(), (uint8_t *)&lastValidSPO2, sizeof(lastValidSPO2));
+						bleNotifyTimer.reset();
+					}
 				}
 			}
 		} else { // If rc != 0, a communication error has occurred
@@ -568,7 +580,7 @@
 			daplink.printf("Something went wrong, "
 					  "check the I2C bus or power connections... \r\n");
 
-			Thread::wait(3000);
+			return;
 		}
 
 	}
@@ -825,17 +837,15 @@
     uint32_t ecgFIFO, RtoR, readECGSamples, idx, ETAG[32], status;
     int16_t ecgSample[32];
     float BPM;
+    Timer bleNotifyTimer;
 
+    bleNotifyTimer.start();
     while (1) {
 		// Read back ECG samples from the FIFO
 		thread_max30003_reader.signal_wait(MAX30003_IRQ_ASSERTED_SIGNAL_ID);
 
         /* Read back ECG samples from the FIFO */
 		status = max30003.readRegister( MAX30003::STATUS );      // Read the STATUS register
-#if __DEBUG__
-		daplink.printf("Status : 0x%x\r\n"
-				  "Current BPM is %3.2f\r\n\r\n", status, BPM);
-#endif
 
 		// Check if R-to-R interrupt asserted
 		if ((status & RTOR_STATUS) == RTOR_STATUS) {
@@ -848,50 +858,33 @@
 			// Convert to BPM
 			BPM = 1.0f / ( RtoR * RTOR_LSB_RES / 60.0f );
 
-			// Print RtoR
-#if __DEBUG__
-			daplink.printf("RtoR : %d\r\n", RtoR);
-#endif
 			daplink.printf("BPM: %.2f\r\n", BPM);
 
-			bleGattAttrWrite(gattCharBPM.getValueHandle(), (uint8_t *)&BPM, sizeof(BPM));
+			if (bleNotifyTimer.read_ms() >=  (MAX30003_BLE_NOTIFY_PERIOD_SEC * 1000)) {
+				bleGattAttrWrite(gattCharBPM.getValueHandle(), (uint8_t *)&BPM, sizeof(BPM));
+				bleNotifyTimer.reset();
+			}
 		}
 
 		// Check if EINT interrupt asserted
 		if ((status & EINT_STATUS) == EINT_STATUS) {
-
-#if __DEBUG__
-			daplink.printf("FIFO Interrupt \r\n");
-#endif
 			readECGSamples = 0;                        // Reset sample counter
 
 			do {
-				ecgFIFO = max30003.readRegister( MAX30003::ECG_FIFO );       // Read FIFO
-				ecgSample[readECGSamples] = ecgFIFO >> 8;                  // Isolate voltage data
-				ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS;  // Isolate ETAG
-				readECGSamples++;                                          // Increment sample counter
+				ecgFIFO = max30003.readRegister( MAX30003::ECG_FIFO );	// Read FIFO
+				ecgSample[readECGSamples] = ecgFIFO >> 8;				// Isolate voltage data
+				ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS;	// Isolate ETAG
+				readECGSamples++;										// Increment sample counter
 
 			// Check that sample is not last sample in FIFO
 			} while (ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE ||
 					  ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE);
 
-#if __DEBUG__
-			daplink.printf("%d samples read from FIFO \r\n", readECGSamples);
-#endif
-
 			// Check if FIFO has overflowed
 			if (ETAG[readECGSamples - 1] == FIFO_OVF){
 				max30003.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO
 				rLED = 1;
 			}
-
-#if __DEBUG__
-			// Print results
-			for (idx = 0; idx < readECGSamples; idx++) {
-				daplink.printf("Sample : %6d, \tETAG : 0x%x\r\n", ecgSample[idx], ETAG[idx]);
-			}
-			daplink.printf("\r\n\r\n\r\n");
-#endif
 		}
     }
 }
@@ -928,7 +921,7 @@
 
         bleGattAttrWrite(gattCharADC.getValueHandle(), (uint8_t *)&adcVoltage, sizeof(adcVoltage));
 
-        Thread::wait(MAX113XX_DATA_READ_PERIOD_MSEC);
+        Thread::wait(MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC * 1000);
     }
 }
 #endif
@@ -940,46 +933,46 @@
 int main()
 {
 	osStatus status;
-	rLED = 1; gLED = 0; bLED = 0; // red
+	rLED = LED_OFF; gLED = LED_OFF; bLED = LED_OFF; // red
 
-    eventQueue.call_every(500, blinkCallback);
+	aliveLedEventId = eventQueue.call_every(1000, blinkCallback);
 
     daplink.printf("Initializing BLE service...\r\n");
 
-    BLE &ble = BLE::Instance();
-    ble.onEventsToProcess(scheduleBleEventsProcessing);
-    ble.init(bleInitComplete);
+	BLE &ble = BLE::Instance();
+	ble.onEventsToProcess(scheduleBleEventsProcessing);
+	ble.init(bleInitComplete);
 
 #if defined(LIB_MAX30205)
-    status = thread_max30205_reader.start(max30205_reader_task);
-    if (status != osOK) {
-    	daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
-    }
+	status = thread_max30205_reader.start(max30205_reader_task);
+	if (status != osOK) {
+		daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
+	}
 #endif
 
 #if defined(LIB_MAX30101)
-    status = thread_max30101_reader.start(max30101_reader_task);
-    if (status != osOK) {
-    	daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
-    }
+	status = thread_max30101_reader.start(max30101_reader_task);
+	if (status != osOK) {
+		daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
+	}
 #endif
 
 #if defined(LIB_MAX30003)
-    status = thread_max30003_reader.start(max30003_reader_task);
-    if (status != osOK) {
-    	daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
-    }
+	status = thread_max30003_reader.start(max30003_reader_task);
+	if (status != osOK) {
+		daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
+	}
 #endif
 
 #if defined(LIB_MAX113XX_PIXI)
-    status = thread_max11301_reader.start(max11301_reader_task);
-    if (status != osOK) {
-    	daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
-    }
+	status = thread_max11301_reader.start(max11301_reader_task);
+	if (status != osOK) {
+		daplink.printf("Starting thread_max30205_reader thread failed(%d)!\r\n", status);
+	}
 #endif
 
-    eventQueue.dispatch_forever();
+	eventQueue.dispatch_forever();
 
-    return 0;
+	return 0;
 }