Demo PAH8011+ HeartRate on nRF52

Dependencies:   mbed pixart_heart_rate_demo pixart_heart_rate_lib_keil_m4

Fork of PixArt_PAH8011_HeartRate_nRF52 by PixArt Imaging

Revision:
0:a6408c845aba
Child:
1:3abb06bb21c5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 27 08:53:49 2017 +0000
@@ -0,0 +1,127 @@
+
+#include "mbed.h"
+#include "system_clock.h"
+
+#include "pixart_pah8011.h"
+#include "pixart_heart_rate.h"
+
+
+static Serial       pc(USBTX, USBRX);
+static I2C          i2c(I2C_SDA0, I2C_SCL0);
+static InterruptIn  pah8011_interrupt(p13);
+
+static volatile bool    has_pah8011_interrupt = false;
+static uint64_t         pah8011_interrupt_timestamp = 0;
+
+
+static void error_handler()
+{
+    pc.printf("error_handler()!\n");
+    
+    while (true)
+    {
+    }
+}
+
+static void debug_print(const char *fmt, ...)
+{
+    char msg[128];
+    va_list marker;
+    va_start(marker, fmt);
+    vsprintf(msg, fmt, marker);
+    va_end(marker);
+    pc.printf(msg);
+}
+
+static void pah8011_interrupt_handle(void)
+{
+    has_pah8011_interrupt = true;
+    pah8011_interrupt_timestamp = system_clock_get_tick();
+}
+
+static uint64_t time_to_millisec(uint64_t time)
+{
+    return system_clock_time_to_milliseconds(time);
+}
+
+int main(void)
+{
+    // system clock
+    system_clock_init();
+    
+    // serial port
+    pc.baud(115200);    
+    pc.printf("Pixart PAH8011 demo\n");
+    
+    // interrupt
+    pah8011_interrupt.rise(pah8011_interrupt_handle);
+    
+    // heart_rate
+    pixart::heart_rate heart_rate(time_to_millisec);
+    heart_rate.enable_debug_print(debug_print);
+    if (!heart_rate.init())
+    {
+        pc.printf("heart_rate.init() failed\n");
+        error_handler();
+    }
+
+    // pah8011
+    pixart::pah8011 pah8011;
+    pah8011.enable_debug_print(debug_print);
+    if (!pah8011.init(i2c))
+    {
+        pc.printf("pah8011.init() failed\n");
+        error_handler();
+    }
+    if (!pah8011.enable_ppg())
+    {
+        pc.printf("pah8011.enable_ppg() failed\n");
+        error_handler();
+    }
+        
+    while(true)
+    {
+        if (has_pah8011_interrupt)
+        {
+            has_pah8011_interrupt = false;
+            
+            if (!pah8011.task())
+            {
+                pc.printf("pah8011.task() failed\n");
+                error_handler();
+            }
+            
+            pixart::pah8011::task_result result;
+            if (pah8011.get_result(result))
+            {
+                if (result.ch_num != 2)
+                    error_handler();
+                
+                for (uint32_t i = 0; i < result.num_per_ch; ++i)
+                {
+                    pixart::heart_rate::ppg_sample ppg_sample;
+                    ppg_sample.ch1 = result.data[i * result.ch_num + 0];
+                    ppg_sample.ch2 = result.data[i * result.ch_num + 1];
+                    ppg_sample.is_touched = result.is_touched;
+                    ppg_sample.timestamp = pah8011_interrupt_timestamp;
+                    heart_rate.set_ppg_sample(ppg_sample);
+                    
+                    pixart::heart_rate::accel_sample accel_sample;
+                    accel_sample.x = 0.0f;
+                    accel_sample.y = 0.0f;
+                    accel_sample.z = 0.0f;
+                    accel_sample.timestamp = pah8011_interrupt_timestamp;
+                    heart_rate.set_accel_sample(accel_sample);
+                    
+                }
+                
+                pixart::heart_rate::result hr_result = heart_rate.get_result();
+                if (hr_result.ret == pixart::heart_rate::ret_success)
+                {
+                    pc.printf("time = %llu, hr = %d, hr_trust_level = %d\n", pah8011_interrupt_timestamp, (int)hr_result.hr, hr_result.hr_trust_level);
+                }
+            }
+        }
+    }
+}
+