心拍・酸素飽和度モニタモジュール MAXREFDES117から取得した心拍の値をシリアルコンソールに表示するプログラムです。

Dependencies:   microbit

Fork of microbit-component-display by BBC

Committer:
JKsoft_main
Date:
Mon Apr 16 16:52:36 2018 +0000
Revision:
1:83ace7df2c63
Parent:
0:0c37474c8541
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonnyA 0:0c37474c8541 1 #include "MicroBit.h"
JKsoft_main 1:83ace7df2c63 2 #include "MAX30102.h"
JKsoft_main 1:83ace7df2c63 3 #include "algorithm.h"
JKsoft_main 1:83ace7df2c63 4
JKsoft_main 1:83ace7df2c63 5 MAX30102 sensor(I2C_SDA0, I2C_SCL0);
JonnyA 0:0c37474c8541 6
JonnyA 0:0c37474c8541 7 MicroBitDisplay display;
JonnyA 0:0c37474c8541 8
JKsoft_main 1:83ace7df2c63 9 DigitalIn INT(P0_16);
JKsoft_main 1:83ace7df2c63 10
JKsoft_main 1:83ace7df2c63 11 Serial pc(USBTX, USBRX);
JKsoft_main 1:83ace7df2c63 12
JKsoft_main 1:83ace7df2c63 13 #define MAX_BRIGHTNESS 255
JKsoft_main 1:83ace7df2c63 14
JKsoft_main 1:83ace7df2c63 15 uint32_t aun_ir_buffer[500]; //IR LED sensor data
JKsoft_main 1:83ace7df2c63 16 int32_t n_ir_buffer_length; //data length
JKsoft_main 1:83ace7df2c63 17 uint32_t aun_red_buffer[500]; //Red LED sensor data
JKsoft_main 1:83ace7df2c63 18 int32_t n_sp02; //SPO2 value
JKsoft_main 1:83ace7df2c63 19 int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
JKsoft_main 1:83ace7df2c63 20 int32_t n_heart_rate; //heart rate value
JKsoft_main 1:83ace7df2c63 21 int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
JKsoft_main 1:83ace7df2c63 22 uint8_t uch_dummy;
JKsoft_main 1:83ace7df2c63 23
JonnyA 0:0c37474c8541 24 int main()
JonnyA 0:0c37474c8541 25 {
JKsoft_main 1:83ace7df2c63 26 uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats
JKsoft_main 1:83ace7df2c63 27 int i;
JKsoft_main 1:83ace7df2c63 28 int32_t n_brightness;
JKsoft_main 1:83ace7df2c63 29 float f_temp;
JKsoft_main 1:83ace7df2c63 30
JKsoft_main 1:83ace7df2c63 31 sensor.reset();
JKsoft_main 1:83ace7df2c63 32 sensor.init();
JKsoft_main 1:83ace7df2c63 33
JKsoft_main 1:83ace7df2c63 34 n_brightness=0;
JKsoft_main 1:83ace7df2c63 35 un_min=0x3FFFF;
JKsoft_main 1:83ace7df2c63 36 un_max=0;
JKsoft_main 1:83ace7df2c63 37
JKsoft_main 1:83ace7df2c63 38 pc.printf("Start\r\n");
JKsoft_main 1:83ace7df2c63 39
JKsoft_main 1:83ace7df2c63 40 n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
JKsoft_main 1:83ace7df2c63 41
JKsoft_main 1:83ace7df2c63 42 //read the first 500 samples, and determine the signal range
JKsoft_main 1:83ace7df2c63 43 //for(i=0;i<n_ir_buffer_length;i++)
JKsoft_main 1:83ace7df2c63 44 i=0;
JonnyA 0:0c37474c8541 45 while(1)
JKsoft_main 1:83ace7df2c63 46 {
JKsoft_main 1:83ace7df2c63 47 while(INT.read()==1); //wait until the interrupt pin asserts
JKsoft_main 1:83ace7df2c63 48
JKsoft_main 1:83ace7df2c63 49 sensor.read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO
JKsoft_main 1:83ace7df2c63 50
JKsoft_main 1:83ace7df2c63 51 if(un_min>aun_red_buffer[i])
JKsoft_main 1:83ace7df2c63 52 un_min=aun_red_buffer[i]; //update signal min
JKsoft_main 1:83ace7df2c63 53 if(un_max<aun_red_buffer[i])
JKsoft_main 1:83ace7df2c63 54 un_max=aun_red_buffer[i]; //update signal max
JKsoft_main 1:83ace7df2c63 55 pc.printf("red=");
JKsoft_main 1:83ace7df2c63 56 pc.printf("%i", aun_red_buffer[i]);
JKsoft_main 1:83ace7df2c63 57 pc.printf(", ir=");
JKsoft_main 1:83ace7df2c63 58 pc.printf("%i\n\r", aun_ir_buffer[i]);
JKsoft_main 1:83ace7df2c63 59 }
JKsoft_main 1:83ace7df2c63 60 un_prev_data=aun_red_buffer[i];
JKsoft_main 1:83ace7df2c63 61
JKsoft_main 1:83ace7df2c63 62
JKsoft_main 1:83ace7df2c63 63 //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
JKsoft_main 1:83ace7df2c63 64 maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);
JKsoft_main 1:83ace7df2c63 65
JKsoft_main 1:83ace7df2c63 66 //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
JKsoft_main 1:83ace7df2c63 67 while(1)
JKsoft_main 1:83ace7df2c63 68 {
JKsoft_main 1:83ace7df2c63 69 i=0;
JKsoft_main 1:83ace7df2c63 70 un_min=0x3FFFF;
JKsoft_main 1:83ace7df2c63 71 un_max=0;
JKsoft_main 1:83ace7df2c63 72
JKsoft_main 1:83ace7df2c63 73 //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
JKsoft_main 1:83ace7df2c63 74 for(i=100;i<500;i++)
JKsoft_main 1:83ace7df2c63 75 {
JKsoft_main 1:83ace7df2c63 76 aun_red_buffer[i-100]=aun_red_buffer[i];
JKsoft_main 1:83ace7df2c63 77 aun_ir_buffer[i-100]=aun_ir_buffer[i];
JKsoft_main 1:83ace7df2c63 78
JKsoft_main 1:83ace7df2c63 79 //update the signal min and max
JKsoft_main 1:83ace7df2c63 80 if(un_min>aun_red_buffer[i])
JKsoft_main 1:83ace7df2c63 81 un_min=aun_red_buffer[i];
JKsoft_main 1:83ace7df2c63 82 if(un_max<aun_red_buffer[i])
JKsoft_main 1:83ace7df2c63 83 un_max=aun_red_buffer[i];
JKsoft_main 1:83ace7df2c63 84 }
JKsoft_main 1:83ace7df2c63 85
JKsoft_main 1:83ace7df2c63 86 //take 100 sets of samples before calculating the heart rate.
JKsoft_main 1:83ace7df2c63 87 for(i=400;i<500;i++)
JKsoft_main 1:83ace7df2c63 88 {
JKsoft_main 1:83ace7df2c63 89 un_prev_data=aun_red_buffer[i-1];
JKsoft_main 1:83ace7df2c63 90 while(INT.read()==1);
JKsoft_main 1:83ace7df2c63 91 sensor.read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));
JKsoft_main 1:83ace7df2c63 92
JKsoft_main 1:83ace7df2c63 93 if(aun_red_buffer[i]>un_prev_data)
JKsoft_main 1:83ace7df2c63 94 {
JKsoft_main 1:83ace7df2c63 95 f_temp=aun_red_buffer[i]-un_prev_data;
JKsoft_main 1:83ace7df2c63 96 f_temp/=(un_max-un_min);
JKsoft_main 1:83ace7df2c63 97 f_temp*=MAX_BRIGHTNESS;
JKsoft_main 1:83ace7df2c63 98 n_brightness-=(int)f_temp;
JKsoft_main 1:83ace7df2c63 99 if(n_brightness<0)
JKsoft_main 1:83ace7df2c63 100 n_brightness=0;
JKsoft_main 1:83ace7df2c63 101 }
JKsoft_main 1:83ace7df2c63 102 else
JKsoft_main 1:83ace7df2c63 103 {
JKsoft_main 1:83ace7df2c63 104 f_temp=un_prev_data-aun_red_buffer[i];
JKsoft_main 1:83ace7df2c63 105 f_temp/=(un_max-un_min);
JKsoft_main 1:83ace7df2c63 106 f_temp*=MAX_BRIGHTNESS;
JKsoft_main 1:83ace7df2c63 107 n_brightness+=(int)f_temp;
JKsoft_main 1:83ace7df2c63 108 if(n_brightness>MAX_BRIGHTNESS)
JKsoft_main 1:83ace7df2c63 109 n_brightness=MAX_BRIGHTNESS;
JKsoft_main 1:83ace7df2c63 110 }
JKsoft_main 1:83ace7df2c63 111
JKsoft_main 1:83ace7df2c63 112 //send samples and calculation result to terminal program through UART
JKsoft_main 1:83ace7df2c63 113 pc.printf("red=");
JKsoft_main 1:83ace7df2c63 114 pc.printf("%i", aun_red_buffer[i]);
JKsoft_main 1:83ace7df2c63 115 pc.printf(", ir=");
JKsoft_main 1:83ace7df2c63 116 pc.printf("%i", aun_ir_buffer[i]);
JKsoft_main 1:83ace7df2c63 117 pc.printf(", HR=%i, ", n_heart_rate);
JKsoft_main 1:83ace7df2c63 118 pc.printf("HRvalid=%i, ", ch_hr_valid);
JKsoft_main 1:83ace7df2c63 119 pc.printf("SpO2=%i, ", n_sp02);
JKsoft_main 1:83ace7df2c63 120 pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
JKsoft_main 1:83ace7df2c63 121 }
JKsoft_main 1:83ace7df2c63 122 maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);
JKsoft_main 1:83ace7df2c63 123 }
JKsoft_main 1:83ace7df2c63 124
JKsoft_main 1:83ace7df2c63 125 //while(1)
JKsoft_main 1:83ace7df2c63 126 // display.scroll(":)");
JonnyA 0:0c37474c8541 127 }