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