Alex Leung
/
HealthTracker
Test version
main.cpp@0:4be500de690c, 2018-03-20 (annotated)
- Committer:
- a2824256
- Date:
- Tue Mar 20 02:09:21 2018 +0000
- Revision:
- 0:4be500de690c
- Child:
- 1:bd9f39f9d91c
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a2824256 | 0:4be500de690c | 1 | #include "mbed.h" |
a2824256 | 0:4be500de690c | 2 | #include "algorithm.h" |
a2824256 | 0:4be500de690c | 3 | #include "MAX30102.h" |
a2824256 | 0:4be500de690c | 4 | #include "LEDS.h" |
a2824256 | 0:4be500de690c | 5 | #include "WIFI.h" |
a2824256 | 0:4be500de690c | 6 | |
a2824256 | 0:4be500de690c | 7 | #define MAX_BRIGHTNESS 255 |
a2824256 | 0:4be500de690c | 8 | extern int* ReadDS1621(void); |
a2824256 | 0:4be500de690c | 9 | uint32_t aun_ir_buffer[500]; //IR LED sensor data |
a2824256 | 0:4be500de690c | 10 | int32_t n_ir_buffer_length; //data length |
a2824256 | 0:4be500de690c | 11 | uint32_t aun_red_buffer[500]; //Red LED sensor data |
a2824256 | 0:4be500de690c | 12 | int32_t n_sp02; //SPO2 value |
a2824256 | 0:4be500de690c | 13 | int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid |
a2824256 | 0:4be500de690c | 14 | int32_t n_heart_rate; //heart rate value |
a2824256 | 0:4be500de690c | 15 | int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid |
a2824256 | 0:4be500de690c | 16 | uint8_t uch_dummy; |
a2824256 | 0:4be500de690c | 17 | |
a2824256 | 0:4be500de690c | 18 | Serial pc(USBTX, USBRX); //initializes the serial port, TX-PA2, RX-PA3 |
a2824256 | 0:4be500de690c | 19 | |
a2824256 | 0:4be500de690c | 20 | PwmOut pwmled(PA_10); //initializes the pwm output PB3 that connects to the LED |
a2824256 | 0:4be500de690c | 21 | DigitalIn INT(PC_5); //pin PB7 connects to the interrupt output pin of the MAX30102 |
a2824256 | 0:4be500de690c | 22 | DigitalOut led(PC_9); //PC13 connects to the on board user LED |
a2824256 | 0:4be500de690c | 23 | AnalogIn xd(PA_0); |
a2824256 | 0:4be500de690c | 24 | AnalogIn yd(PA_1); |
a2824256 | 0:4be500de690c | 25 | AnalogIn zd(PA_4); |
a2824256 | 0:4be500de690c | 26 | |
a2824256 | 0:4be500de690c | 27 | // the setup routine runs once when you press reset: |
a2824256 | 0:4be500de690c | 28 | int main() { |
a2824256 | 0:4be500de690c | 29 | uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats |
a2824256 | 0:4be500de690c | 30 | int i; |
a2824256 | 0:4be500de690c | 31 | int32_t n_brightness; |
a2824256 | 0:4be500de690c | 32 | float f_temp; |
a2824256 | 0:4be500de690c | 33 | int* temp; |
a2824256 | 0:4be500de690c | 34 | |
a2824256 | 0:4be500de690c | 35 | maxim_max30102_reset(); //resets the MAX30102 |
a2824256 | 0:4be500de690c | 36 | |
a2824256 | 0:4be500de690c | 37 | // initialize serial communication at 115200 bits per second: |
a2824256 | 0:4be500de690c | 38 | //pc.baud(115200); |
a2824256 | 0:4be500de690c | 39 | //while(1); |
a2824256 | 0:4be500de690c | 40 | //pc.format(8,SerialBase::None,1); |
a2824256 | 0:4be500de690c | 41 | wait(5); |
a2824256 | 0:4be500de690c | 42 | //init wifi |
a2824256 | 0:4be500de690c | 43 | scan_demo(&wifi); |
a2824256 | 0:4be500de690c | 44 | int ret = wifi.connect("Wo4G-YDL0", "by353006", NSAPI_SECURITY_WPA_WPA2); |
a2824256 | 0:4be500de690c | 45 | if (ret != 0) { |
a2824256 | 0:4be500de690c | 46 | pc.printf("\r\nConnection error\r\n"); |
a2824256 | 0:4be500de690c | 47 | return -1; |
a2824256 | 0:4be500de690c | 48 | } |
a2824256 | 0:4be500de690c | 49 | |
a2824256 | 0:4be500de690c | 50 | //wifi.disconnect(); |
a2824256 | 0:4be500de690c | 51 | //read and clear status register |
a2824256 | 0:4be500de690c | 52 | maxim_max30102_read_reg(0,&uch_dummy); |
a2824256 | 0:4be500de690c | 53 | |
a2824256 | 0:4be500de690c | 54 | maxim_max30102_init(); //initializes the MAX30102 |
a2824256 | 0:4be500de690c | 55 | |
a2824256 | 0:4be500de690c | 56 | |
a2824256 | 0:4be500de690c | 57 | n_brightness=0; |
a2824256 | 0:4be500de690c | 58 | un_min=0x3FFFF; |
a2824256 | 0:4be500de690c | 59 | un_max=0; |
a2824256 | 0:4be500de690c | 60 | |
a2824256 | 0:4be500de690c | 61 | n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps |
a2824256 | 0:4be500de690c | 62 | |
a2824256 | 0:4be500de690c | 63 | //read the first 500 samples, and determine the signal range |
a2824256 | 0:4be500de690c | 64 | for(i=0;i<n_ir_buffer_length;i++) |
a2824256 | 0:4be500de690c | 65 | { |
a2824256 | 0:4be500de690c | 66 | while(INT.read()==1); //wait until the interrupt pin asserts |
a2824256 | 0:4be500de690c | 67 | |
a2824256 | 0:4be500de690c | 68 | maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO |
a2824256 | 0:4be500de690c | 69 | |
a2824256 | 0:4be500de690c | 70 | if(un_min>aun_red_buffer[i]) |
a2824256 | 0:4be500de690c | 71 | un_min=aun_red_buffer[i]; //update signal min |
a2824256 | 0:4be500de690c | 72 | if(un_max<aun_red_buffer[i]) |
a2824256 | 0:4be500de690c | 73 | un_max=aun_red_buffer[i]; //update signal max |
a2824256 | 0:4be500de690c | 74 | //pc.printf("red=%i", aun_red_buffer[i]); |
a2824256 | 0:4be500de690c | 75 | //pc.printf(", ir=%i\n", aun_ir_buffer[i]); |
a2824256 | 0:4be500de690c | 76 | wait(0.01); |
a2824256 | 0:4be500de690c | 77 | } |
a2824256 | 0:4be500de690c | 78 | un_prev_data=aun_red_buffer[i]; |
a2824256 | 0:4be500de690c | 79 | |
a2824256 | 0:4be500de690c | 80 | |
a2824256 | 0:4be500de690c | 81 | //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples) |
a2824256 | 0:4be500de690c | 82 | 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); |
a2824256 | 0:4be500de690c | 83 | |
a2824256 | 0:4be500de690c | 84 | //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second |
a2824256 | 0:4be500de690c | 85 | while(1) |
a2824256 | 0:4be500de690c | 86 | { |
a2824256 | 0:4be500de690c | 87 | i=0; |
a2824256 | 0:4be500de690c | 88 | un_min=0x3FFFF; |
a2824256 | 0:4be500de690c | 89 | un_max=0; |
a2824256 | 0:4be500de690c | 90 | |
a2824256 | 0:4be500de690c | 91 | //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top |
a2824256 | 0:4be500de690c | 92 | for(i=100;i<500;i++) |
a2824256 | 0:4be500de690c | 93 | { |
a2824256 | 0:4be500de690c | 94 | aun_red_buffer[i-100]=aun_red_buffer[i]; |
a2824256 | 0:4be500de690c | 95 | aun_ir_buffer[i-100]=aun_ir_buffer[i]; |
a2824256 | 0:4be500de690c | 96 | |
a2824256 | 0:4be500de690c | 97 | //update the signal min and max |
a2824256 | 0:4be500de690c | 98 | if(un_min>aun_red_buffer[i]) |
a2824256 | 0:4be500de690c | 99 | un_min=aun_red_buffer[i]; |
a2824256 | 0:4be500de690c | 100 | if(un_max<aun_red_buffer[i]) |
a2824256 | 0:4be500de690c | 101 | un_max=aun_red_buffer[i]; |
a2824256 | 0:4be500de690c | 102 | } |
a2824256 | 0:4be500de690c | 103 | |
a2824256 | 0:4be500de690c | 104 | //take 100 sets of samples before calculating the heart rate. |
a2824256 | 0:4be500de690c | 105 | for(i=400;i<500;i++) |
a2824256 | 0:4be500de690c | 106 | { |
a2824256 | 0:4be500de690c | 107 | un_prev_data=aun_red_buffer[i-1]; |
a2824256 | 0:4be500de690c | 108 | while(INT.read()==1); |
a2824256 | 0:4be500de690c | 109 | maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); |
a2824256 | 0:4be500de690c | 110 | |
a2824256 | 0:4be500de690c | 111 | if(aun_red_buffer[i]>un_prev_data)//just to determine the brightness of LED according to the deviation of adjacent two AD data |
a2824256 | 0:4be500de690c | 112 | { |
a2824256 | 0:4be500de690c | 113 | f_temp=aun_red_buffer[i]-un_prev_data; |
a2824256 | 0:4be500de690c | 114 | f_temp/=(un_max-un_min); |
a2824256 | 0:4be500de690c | 115 | f_temp*=MAX_BRIGHTNESS; |
a2824256 | 0:4be500de690c | 116 | n_brightness-=(int)f_temp; |
a2824256 | 0:4be500de690c | 117 | if(n_brightness<0) |
a2824256 | 0:4be500de690c | 118 | n_brightness=0; |
a2824256 | 0:4be500de690c | 119 | } |
a2824256 | 0:4be500de690c | 120 | else |
a2824256 | 0:4be500de690c | 121 | { |
a2824256 | 0:4be500de690c | 122 | f_temp=un_prev_data-aun_red_buffer[i]; |
a2824256 | 0:4be500de690c | 123 | f_temp/=(un_max-un_min); |
a2824256 | 0:4be500de690c | 124 | f_temp*=MAX_BRIGHTNESS; |
a2824256 | 0:4be500de690c | 125 | n_brightness+=(int)f_temp; |
a2824256 | 0:4be500de690c | 126 | if(n_brightness>MAX_BRIGHTNESS) |
a2824256 | 0:4be500de690c | 127 | n_brightness=MAX_BRIGHTNESS; |
a2824256 | 0:4be500de690c | 128 | } |
a2824256 | 0:4be500de690c | 129 | |
a2824256 | 0:4be500de690c | 130 | pwmled.write(1-(float)n_brightness/256);//pwm control led brightness |
a2824256 | 0:4be500de690c | 131 | if(n_brightness<120) |
a2824256 | 0:4be500de690c | 132 | led=1; |
a2824256 | 0:4be500de690c | 133 | else |
a2824256 | 0:4be500de690c | 134 | led=0; |
a2824256 | 0:4be500de690c | 135 | |
a2824256 | 0:4be500de690c | 136 | //send samples and calculation result to terminal program through UART |
a2824256 | 0:4be500de690c | 137 | //pc.printf("red="); |
a2824256 | 0:4be500de690c | 138 | //pc.printf("%i", aun_red_buffer[i]); |
a2824256 | 0:4be500de690c | 139 | //pc.printf(", ir="); |
a2824256 | 0:4be500de690c | 140 | //pc.printf("%i", aun_ir_buffer[i]); |
a2824256 | 0:4be500de690c | 141 | temp = ReadDS1621(); |
a2824256 | 0:4be500de690c | 142 | http_demo(&wifi); |
a2824256 | 0:4be500de690c | 143 | LightLEDs(n_heart_rate); |
a2824256 | 0:4be500de690c | 144 | //pc.printf("HR=%i, temperature=%i:%i,SpO2=%i,x=%f,y=%f,z=%f\n", n_heart_rate,temp[0],temp[1],n_sp02,xd.read(),yd.read(),zd.read()); |
a2824256 | 0:4be500de690c | 145 | //pc.printf("HR=%i\n", n_heart_rate); |
a2824256 | 0:4be500de690c | 146 | //pc.printf("HRvalid=%i, ", ch_hr_valid); |
a2824256 | 0:4be500de690c | 147 | //pc.printf("SpO2=%i, ", n_sp02); |
a2824256 | 0:4be500de690c | 148 | //pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid); |
a2824256 | 0:4be500de690c | 149 | } |
a2824256 | 0:4be500de690c | 150 | 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); |
a2824256 | 0:4be500de690c | 151 | wait(0.01); |
a2824256 | 0:4be500de690c | 152 | } |
a2824256 | 0:4be500de690c | 153 | } |