A03_MAX30102_example1.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "max30102.h"
  19. #include "algorithm.h"
  20. #include "cmsis_os2.h"
  21. #include "ohos_init.h"
  22. #define TASK_STACK_SIZE 1024 * 8
  23. #define TASK_PRIO 25
  24. uint32_t aun_ir_buffer[500]; //IR LED sensor data
  25. int32_t n_ir_buffer_length; //data length
  26. uint32_t aun_red_buffer[500]; //Red LED sensor data
  27. int32_t n_sp02; //SPO2 value
  28. int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
  29. int32_t n_heart_rate; //heart rate value
  30. int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
  31. uint8_t uch_dummy;
  32. #define MAX_BRIGHTNESS 255
  33. static void ExampleTask(void)
  34. {
  35. //variables to calculate the on-board LED brightness that reflects the heartbeats
  36. uint32_t un_min, un_max, un_prev_data;
  37. int i;
  38. int32_t n_brightness;
  39. float f_temp;
  40. uint8_t temp_num=0;
  41. uint8_t temp[6];
  42. uint8_t str[100];
  43. uint8_t dis_hr=0,dis_spo2=0;
  44. BoardInit();
  45. maxim_max30102_init();
  46. printf("\r\n MAX30102 init \r\n");
  47. un_min=0x3FFFF;
  48. un_max=0;
  49. n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
  50. //read the first 500 samples, and determine the signal range
  51. for(i=0;i<n_ir_buffer_length;i++)
  52. {
  53. while(GetInit()==1); //wait until the interrupt pin asserts
  54. maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));
  55. if(un_min>aun_red_buffer[i])
  56. un_min=aun_red_buffer[i]; //update signal min
  57. if(un_max<aun_red_buffer[i])
  58. un_max=aun_red_buffer[i]; //update signal max
  59. printf("red=");
  60. printf("%i", aun_red_buffer[i]);
  61. printf(", ir=");
  62. printf("%i\n\r", aun_ir_buffer[i]);
  63. }
  64. un_prev_data=aun_red_buffer[i];
  65. //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
  66. 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);
  67. while(1)
  68. {
  69. i=0;
  70. un_min=0x3FFFF;
  71. un_max=0;
  72. //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
  73. for(i=100;i<500;i++)
  74. {
  75. aun_red_buffer[i-100]=aun_red_buffer[i];
  76. aun_ir_buffer[i-100]=aun_ir_buffer[i];
  77. //update the signal min and max
  78. if(un_min>aun_red_buffer[i])
  79. un_min=aun_red_buffer[i];
  80. if(un_max<aun_red_buffer[i])
  81. un_max=aun_red_buffer[i];
  82. }
  83. //take 100 sets of samples before calculating the heart rate.
  84. for(i=400;i<500;i++)
  85. {
  86. un_prev_data=aun_red_buffer[i-1];
  87. while(GetInit()==1);
  88. maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));
  89. if(aun_red_buffer[i]>un_prev_data)
  90. {
  91. f_temp=aun_red_buffer[i]-un_prev_data;
  92. f_temp/=(un_max-un_min);
  93. f_temp*=MAX_BRIGHTNESS;
  94. n_brightness-=(int)f_temp;
  95. if(n_brightness<0)
  96. n_brightness=0;
  97. }
  98. else
  99. {
  100. f_temp=un_prev_data-aun_red_buffer[i];
  101. f_temp/=(un_max-un_min);
  102. f_temp*=MAX_BRIGHTNESS;
  103. n_brightness+=(int)f_temp;
  104. if(n_brightness>MAX_BRIGHTNESS)
  105. n_brightness=MAX_BRIGHTNESS;
  106. }
  107. //send samples and calculation result to terminal program through UART
  108. // if(ch_hr_valid == 1 && n_heart_rate<120)//**/ ch_hr_valid == 1 && ch_spo2_valid ==1 && n_heart_rate<120 && n_sp02<101
  109. // {
  110. // dis_hr = n_heart_rate;
  111. // dis_spo2 = n_sp02;
  112. // }
  113. // else
  114. // {
  115. // dis_hr = 0;
  116. // dis_spo2 = 0;
  117. // }
  118. printf("HR=%i, ", n_heart_rate);
  119. printf("HRvalid=%i, ", ch_hr_valid);
  120. printf("SpO2=%i, ", n_sp02);
  121. printf("SPO2Valid=%i\r\n", ch_spo2_valid);
  122. }
  123. 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);
  124. // //显示刷新
  125. // LED0=0;
  126. // if(dis_hr == 0 && dis_spo2 == 0) //**dis_hr == 0 && dis_spo2 == 0
  127. // {
  128. // sprintf((char *)str,"HR:--- SpO2:--- ");//**HR:--- SpO2:---
  129. // }
  130. // else{
  131. // sprintf((char *)str,"HR:%3d SpO2:%3d ",dis_hr,dis_spo2);//**HR:%3d SpO2:%3d
  132. // }
  133. // OLED_ShowString(0,0,str,16);
  134. // OLED_Fill(0,23,127,63,0);
  135. // //红光在上,红外在下
  136. // dis_DrawCurve(aun_red_buffer,20);
  137. // dis_DrawCurve(aun_ir_buffer,0);
  138. // OLED_Refresh_Gram();//更新显示到OLED
  139. }
  140. }
  141. static void ExampleEntry(void)
  142. {
  143. osThreadAttr_t attr;
  144. attr.name = "ExampleTask";
  145. attr.attr_bits = 0U;
  146. attr.cb_mem = NULL;
  147. attr.cb_size = 0U;
  148. attr.stack_mem = NULL;
  149. attr.stack_size = TASK_STACK_SIZE;
  150. attr.priority = TASK_PRIO;
  151. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  152. printf("Failed to create ExampleTask!\n");
  153. }
  154. }
  155. APP_FEATURE_INIT(ExampleEntry);