Maxim Integrated's IoT development kit

Dependencies:   MAX30101 MAX30003 MAX113XX_Pixi MAX30205 max32630fthr USBDevice

Revision:
2:68ffd74e3b5c
Parent:
1:efe9cad8942f
Child:
3:7e9a93b6b82b
--- 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;
 }