Code to log MAX30102 sensor data to phone application (Based on Nexpaq)

Dependencies:   ds3231 maxim_dev nexpaq_mdk MAX30102

Fork of IO_Demo by nexpaq

Committer:
shettypavank
Date:
Sun Dec 18 09:43:33 2016 +0000
Revision:
2:9057c54ae6e9
Parent:
1:5fb525e83780
Code to log max30102 sensor data to phone app

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:088641a122ff 1 #include "mbed.h"
switches 0:088641a122ff 2 #include "nexpaq_mdk.h"
shettypavank 2:9057c54ae6e9 3 #include "ds3231.h"
shettypavank 2:9057c54ae6e9 4 #include "algorithm.h"
shettypavank 2:9057c54ae6e9 5 #include "MAX30102.h"
switches 0:088641a122ff 6
shettypavank 2:9057c54ae6e9 7 #define I2C_SDA P1_6
shettypavank 2:9057c54ae6e9 8 #define I2C_SCL P1_7
shettypavank 2:9057c54ae6e9 9
shettypavank 2:9057c54ae6e9 10 #define FUNCTION_TABLE_NUM 2
shettypavank 2:9057c54ae6e9 11 #define UUID_NUM 16 // UUID number is 16, don't change it
shettypavank 2:9057c54ae6e9 12 #define LOOP_DELAY 1000
shettypavank 2:9057c54ae6e9 13
shettypavank 2:9057c54ae6e9 14 DigitalOut ledR(P2_4, LED_OFF);
shettypavank 2:9057c54ae6e9 15 DigitalOut ledG(P2_5, LED_OFF);
shettypavank 2:9057c54ae6e9 16 DigitalOut ledB(P2_6, LED_OFF);
shettypavank 2:9057c54ae6e9 17
shettypavank 2:9057c54ae6e9 18 uint32_t aun_ir_buffer[500]; //IR LED sensor data
shettypavank 2:9057c54ae6e9 19 int32_t n_ir_buffer_length; //data length
shettypavank 2:9057c54ae6e9 20 uint32_t aun_red_buffer[500]; //Red LED sensor data
shettypavank 2:9057c54ae6e9 21 int32_t n_sp02; //SPO2 value
shettypavank 2:9057c54ae6e9 22 int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
shettypavank 2:9057c54ae6e9 23 int32_t n_heart_rate; //heart rate value
shettypavank 2:9057c54ae6e9 24 int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
shettypavank 2:9057c54ae6e9 25 uint8_t uch_dummy;
shettypavank 2:9057c54ae6e9 26
shettypavank 2:9057c54ae6e9 27 DigitalIn INT(P4_1);
shettypavank 2:9057c54ae6e9 28
switches 0:088641a122ff 29 AnalogIn aIn0(AIN_0);
switches 0:088641a122ff 30 AnalogIn aIn1(AIN_1);
switches 0:088641a122ff 31 AnalogIn aIn2(AIN_2);
switches 0:088641a122ff 32 AnalogIn aIn3(AIN_3);
switches 0:088641a122ff 33 AnalogIn aIn4(AIN_4);
switches 0:088641a122ff 34 AnalogIn aIn5(AIN_5);
switches 0:088641a122ff 35 DigitalInOut digPin[] = {DigitalInOut(P0_1),DigitalInOut(P0_0),DigitalInOut(P1_2),DigitalInOut(P1_1),
switches 0:088641a122ff 36 DigitalInOut(P1_0),DigitalInOut(P1_3),DigitalInOut(P1_7),DigitalInOut(P1_6),
switches 0:088641a122ff 37 DigitalInOut(P3_0),DigitalInOut(P3_1),DigitalInOut(P3_4),DigitalInOut(P3_5),
switches 0:088641a122ff 38 DigitalInOut(P0_2),DigitalInOut(P0_3),DigitalInOut(P4_0),DigitalInOut(P4_1)
switches 0:088641a122ff 39 };
switches 0:088641a122ff 40
switches 0:088641a122ff 41 /***** Definitions *****/
switches 0:088641a122ff 42 #define FUNCTION_TABLE_NUM 2
switches 0:088641a122ff 43 #define UUID_NUM 16 // UUID number is 16, don't change it
switches 0:088641a122ff 44 #define LOOP_DELAY 1000
switches 0:088641a122ff 45
switches 0:088641a122ff 46 /***** Globals *****/
switches 0:088641a122ff 47 void my_function_CMD_2700(unsigned char *pData, unsigned char len);
switches 0:088641a122ff 48 void my_function_CMD_2702(unsigned char *pData, unsigned char len);
switches 0:088641a122ff 49 const MDK_REGISTER_CMD my_cmd_func_table[FUNCTION_TABLE_NUM] = {
switches 0:088641a122ff 50 {0x2700, my_function_CMD_2700}, // Command -> function
switches 0:088641a122ff 51 {0x2702, my_function_CMD_2702}, // Command -> function
switches 0:088641a122ff 52 };
switches 0:088641a122ff 53
switches 0:088641a122ff 54 unsigned char dataOut[10];
shettypavank 2:9057c54ae6e9 55 Ds3231 temp_sense(P3_4, P3_5);
shettypavank 2:9057c54ae6e9 56 DigitalOut cs(P1_3);
shettypavank 2:9057c54ae6e9 57 //vitals
shettypavank 2:9057c54ae6e9 58 int spo2=50,heart_rate=120,Resp_rate=12,tmp=25;
switches 0:088641a122ff 59 /***** Functions *****/
switches 0:088641a122ff 60 void my_function_CMD_2700(unsigned char *pData, unsigned char len)
switches 0:088641a122ff 61 {
switches 0:088641a122ff 62 unsigned char response = 0x00;
shettypavank 2:9057c54ae6e9 63 /* ledR = 1.0f - (pData[0] / 255.0f);
switches 0:088641a122ff 64 ledG = 1.0f - (pData[1] / 255.0f);
shettypavank 2:9057c54ae6e9 65 ledB = 1.0f - (pData[2] / 255.0f);*/
switches 0:088641a122ff 66 np_api_upload(0x2701, &response, 1);
switches 0:088641a122ff 67 }
switches 0:088641a122ff 68
switches 0:088641a122ff 69 void my_function_CMD_2702(unsigned char *pData, unsigned char len)
switches 0:088641a122ff 70 {
switches 0:088641a122ff 71 unsigned char response = 0x00;
switches 0:088641a122ff 72 int pinIndex = pData[0];
switches 0:088641a122ff 73 int pinMode = pData[1];
switches 0:088641a122ff 74 switch(pinMode) {
switches 0:088641a122ff 75 case 0:
switches 0:088641a122ff 76 case 1:
switches 0:088641a122ff 77 digPin[pinIndex].mode(PullNone);
switches 0:088641a122ff 78 digPin[pinIndex].input();
switches 0:088641a122ff 79 break;
switches 0:088641a122ff 80 case 2:
switches 0:088641a122ff 81 digPin[pinIndex].mode(PullDown);
switches 0:088641a122ff 82 digPin[pinIndex].input();
switches 0:088641a122ff 83 break;
switches 0:088641a122ff 84 case 3:
switches 0:088641a122ff 85 digPin[pinIndex].mode(PullUp);
switches 0:088641a122ff 86 digPin[pinIndex].input();
switches 0:088641a122ff 87 break;
switches 0:088641a122ff 88 case 4:
switches 0:088641a122ff 89 digPin[pinIndex]=0;
switches 0:088641a122ff 90 digPin[pinIndex].output();
switches 0:088641a122ff 91 break;
switches 0:088641a122ff 92 case 5:
switches 0:088641a122ff 93 digPin[pinIndex]=1;
switches 0:088641a122ff 94 digPin[pinIndex].output();
switches 0:088641a122ff 95 break;
switches 0:088641a122ff 96 }
switches 0:088641a122ff 97 np_api_upload(0x2703, &response, 1);
switches 0:088641a122ff 98 }
switches 0:088641a122ff 99
switches 0:088641a122ff 100 void sendStatus()
switches 0:088641a122ff 101 {
switches 0:088641a122ff 102 np_api_upload(0x2800, dataOut, 10);
switches 0:088641a122ff 103 }
switches 0:088641a122ff 104
switches 0:088641a122ff 105 /******************************************************************************/
switches 0:088641a122ff 106 void app_setup()
switches 0:088641a122ff 107 {
switches 0:088641a122ff 108 int i = 0;
switches 0:088641a122ff 109 // np_api_set_app_version(0, 0, 3);
switches 0:088641a122ff 110 if ( np_api_register((MDK_REGISTER_CMD*)my_cmd_func_table, FUNCTION_TABLE_NUM) == MDK_REGISTER_FAILD ) {
switches 0:088641a122ff 111 // Register failed handle code
switches 0:088641a122ff 112 }
shettypavank 2:9057c54ae6e9 113 /* ledR = 1.0f;
switches 0:088641a122ff 114 ledG = 1.0f;
switches 0:088641a122ff 115 ledB = 1.0f;
switches 0:088641a122ff 116 for (i = 0; i < 16; i++) {
switches 0:088641a122ff 117 digPin[i].mode(PullUp);
switches 0:088641a122ff 118 digPin[i].input();
switches 0:088641a122ff 119 }
switches 0:088641a122ff 120 for (i = 0; i < 10; i++) {
switches 0:088641a122ff 121 dataOut[i] = 0xFF;
shettypavank 2:9057c54ae6e9 122 }*/
shettypavank 2:9057c54ae6e9 123
shettypavank 2:9057c54ae6e9 124 //instantiate rtc object
shettypavank 2:9057c54ae6e9 125
shettypavank 2:9057c54ae6e9 126
switches 0:088641a122ff 127 }
switches 0:088641a122ff 128
switches 0:088641a122ff 129 void app_loop()
shettypavank 2:9057c54ae6e9 130 {
shettypavank 2:9057c54ae6e9 131 if((n_heart_rate>20)&&(n_heart_rate<220))
shettypavank 2:9057c54ae6e9 132 heart_rate=n_heart_rate;
shettypavank 2:9057c54ae6e9 133 if((n_sp02>60)&&(n_sp02<120))
shettypavank 2:9057c54ae6e9 134 spo2=n_sp02;
shettypavank 2:9057c54ae6e9 135
shettypavank 2:9057c54ae6e9 136 tmp = temp_sense.get_temperature();
shettypavank 2:9057c54ae6e9 137 dataOut[0] = heart_rate;
shettypavank 2:9057c54ae6e9 138 dataOut[1] = heart_rate>>8;
shettypavank 2:9057c54ae6e9 139 dataOut[2] = tmp;
shettypavank 2:9057c54ae6e9 140 dataOut[3] = tmp>>8;
shettypavank 2:9057c54ae6e9 141 dataOut[4] = spo2;
shettypavank 2:9057c54ae6e9 142 dataOut[5] = spo2>>8;
shettypavank 2:9057c54ae6e9 143 dataOut[6] = Resp_rate;
shettypavank 2:9057c54ae6e9 144 dataOut[7] = Resp_rate>>8;
switches 0:088641a122ff 145
switches 0:088641a122ff 146 dataOut[8] = (digPin[7].read()<<7) +
switches 0:088641a122ff 147 (digPin[6].read()<<6) +
switches 0:088641a122ff 148 (digPin[5].read()<<5) +
switches 0:088641a122ff 149 (digPin[4].read()<<4) +
switches 0:088641a122ff 150 (digPin[3].read()<<3) +
switches 0:088641a122ff 151 (digPin[2].read()<<2) +
switches 0:088641a122ff 152 (digPin[1].read()<<1) +
switches 0:088641a122ff 153 (digPin[0].read());
switches 0:088641a122ff 154 dataOut[9] = (digPin[15].read()<<7) +
switches 0:088641a122ff 155 (digPin[14].read()<<6) +
switches 0:088641a122ff 156 (digPin[13].read()<<5) +
switches 0:088641a122ff 157 (digPin[12].read()<<4) +
switches 0:088641a122ff 158 (digPin[11].read()<<3) +
switches 0:088641a122ff 159 (digPin[10].read()<<2) +
switches 0:088641a122ff 160 (digPin[9].read()<<1) +
switches 0:088641a122ff 161 (digPin[8].read());
shettypavank 2:9057c54ae6e9 162
switches 0:088641a122ff 163 sendStatus();
switches 0:088641a122ff 164 }
switches 0:088641a122ff 165
switches 0:088641a122ff 166 int main(void)
switches 0:088641a122ff 167 {
shettypavank 2:9057c54ae6e9 168 uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats
shettypavank 2:9057c54ae6e9 169 int i;
shettypavank 2:9057c54ae6e9 170 maxim_max30102_reset();
shettypavank 2:9057c54ae6e9 171 maxim_max30102_read_reg(0,&uch_dummy);
shettypavank 2:9057c54ae6e9 172 maxim_max30102_init();
shettypavank 2:9057c54ae6e9 173 un_min=0x3FFFF;
shettypavank 2:9057c54ae6e9 174 un_max=0;
shettypavank 2:9057c54ae6e9 175 n_ir_buffer_length=500;
switches 0:088641a122ff 176 np_api_init();
switches 0:088641a122ff 177 app_setup();
switches 0:088641a122ff 178 np_api_start();
shettypavank 2:9057c54ae6e9 179
shettypavank 2:9057c54ae6e9 180 for(i=0;i<n_ir_buffer_length;i++)
shettypavank 2:9057c54ae6e9 181 {
shettypavank 2:9057c54ae6e9 182 while(INT.read()==1); //wait until the interrupt pin asserts
shettypavank 2:9057c54ae6e9 183
shettypavank 2:9057c54ae6e9 184 maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO
shettypavank 2:9057c54ae6e9 185
shettypavank 2:9057c54ae6e9 186 if(un_min>aun_red_buffer[i])
shettypavank 2:9057c54ae6e9 187 un_min=aun_red_buffer[i]; //update signal min
shettypavank 2:9057c54ae6e9 188 if(un_max<aun_red_buffer[i])
shettypavank 2:9057c54ae6e9 189 un_max=aun_red_buffer[i];
shettypavank 2:9057c54ae6e9 190 }
shettypavank 2:9057c54ae6e9 191 un_prev_data=aun_red_buffer[i];
shettypavank 2:9057c54ae6e9 192 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);
shettypavank 2:9057c54ae6e9 193
switches 0:088641a122ff 194 while(1) {
shettypavank 2:9057c54ae6e9 195 i=0;
shettypavank 2:9057c54ae6e9 196 un_min=0x3FFFF;
shettypavank 2:9057c54ae6e9 197 un_max=0;
shettypavank 2:9057c54ae6e9 198
shettypavank 2:9057c54ae6e9 199 //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
shettypavank 2:9057c54ae6e9 200 for(i=100;i<500;i++)
shettypavank 2:9057c54ae6e9 201 {
shettypavank 2:9057c54ae6e9 202 aun_red_buffer[i-100]=aun_red_buffer[i];
shettypavank 2:9057c54ae6e9 203 aun_ir_buffer[i-100]=aun_ir_buffer[i];
shettypavank 2:9057c54ae6e9 204
shettypavank 2:9057c54ae6e9 205 //update the signal min and max
shettypavank 2:9057c54ae6e9 206 if(un_min>aun_red_buffer[i])
shettypavank 2:9057c54ae6e9 207 un_min=aun_red_buffer[i];
shettypavank 2:9057c54ae6e9 208 if(un_max<aun_red_buffer[i])
shettypavank 2:9057c54ae6e9 209 un_max=aun_red_buffer[i];
shettypavank 2:9057c54ae6e9 210 }
shettypavank 2:9057c54ae6e9 211
shettypavank 2:9057c54ae6e9 212 for(i=400;i<500;i++)
shettypavank 2:9057c54ae6e9 213 {
shettypavank 2:9057c54ae6e9 214 un_prev_data=aun_red_buffer[i-1];
shettypavank 2:9057c54ae6e9 215 while(INT.read()==1);
shettypavank 2:9057c54ae6e9 216 maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));
shettypavank 2:9057c54ae6e9 217
shettypavank 2:9057c54ae6e9 218 /* if(aun_red_buffer[i]>un_prev_data)
shettypavank 2:9057c54ae6e9 219 {
shettypavank 2:9057c54ae6e9 220 f_temp=aun_red_buffer[i]-un_prev_data;
shettypavank 2:9057c54ae6e9 221 f_temp/=(un_max-un_min);
shettypavank 2:9057c54ae6e9 222 f_temp*=MAX_BRIGHTNESS;
shettypavank 2:9057c54ae6e9 223 n_brightness-=(int)f_temp;
shettypavank 2:9057c54ae6e9 224 if(n_brightness<0)
shettypavank 2:9057c54ae6e9 225 n_brightness=0;
shettypavank 2:9057c54ae6e9 226 }
shettypavank 2:9057c54ae6e9 227 else
shettypavank 2:9057c54ae6e9 228 {
shettypavank 2:9057c54ae6e9 229 f_temp=un_prev_data-aun_red_buffer[i];
shettypavank 2:9057c54ae6e9 230 f_temp/=(un_max-un_min);
shettypavank 2:9057c54ae6e9 231 f_temp*=MAX_BRIGHTNESS;
shettypavank 2:9057c54ae6e9 232 n_brightness+=(int)f_temp;
shettypavank 2:9057c54ae6e9 233 if(n_brightness>MAX_BRIGHTNESS)
shettypavank 2:9057c54ae6e9 234 n_brightness=MAX_BRIGHTNESS;
shettypavank 2:9057c54ae6e9 235 }*/
shettypavank 2:9057c54ae6e9 236
shettypavank 2:9057c54ae6e9 237 //led.write(1-(float)n_brightness/256);
shettypavank 2:9057c54ae6e9 238
shettypavank 2:9057c54ae6e9 239 //send samples and calculation result to terminal program through UART
shettypavank 2:9057c54ae6e9 240
shettypavank 2:9057c54ae6e9 241 }
shettypavank 2:9057c54ae6e9 242 // ledR=LED_OFF;
shettypavank 2:9057c54ae6e9 243 // ledG=LED_ON;
shettypavank 2:9057c54ae6e9 244 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);
shettypavank 2:9057c54ae6e9 245
shettypavank 2:9057c54ae6e9 246 ledR = LED_ON;
switches 0:088641a122ff 247 app_loop();
switches 0:088641a122ff 248 np_api_bsl_chk();
switches 0:088641a122ff 249 Thread::wait(LOOP_DELAY);
switches 0:088641a122ff 250 }
switches 0:088641a122ff 251 return 0;
switches 0:088641a122ff 252 }