#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "string.h"
#include "max30102.h"
#include "ds18b20.h"
#include "beep.h"
#include "key.h"
#include "timer.h"
#include "oled.h"
#include "adxl345.h"
#include "iic.h"
double lat_minutes = 32.138118;
double lon_minutes = 116.447290;
#define GPS_STR_LEN 48
unsigned char display[16];
int temp_door = 38;
int oxy_door = 90;
int heart_door = 100;
uint8_t send_time = 3;
uint8_t printd_flag = 0;
float adx,ady,adz;
float acc,acc2;
uint8_t tiltFlag = 0;
uint8_t fallTime = 0;
uint8_t option = 0;
uint8_t key_num = 0;
uint8_t page_id = 1;
void oled_show_onepage();
void oled_show_twopage();
void get_all_sensor_data();
void Sensor_Data_Handle();
void FallDetection(void);
int main(void) {
delay_init();
OLED_Init();
uart2_init(9600);
USART1_Config();
uart3_init(9600);
DS18B20_Init();
MAX30102_Init();
un_min=0x3FFFF;
un_max=0;
Max30102_right_data();
BEEP_Init();
beep = 0;
KEY_Init();
IIC_init();
adxl345_init();
TIME_Config();
while(1) {
get_all_sensor_data();
if(Time_Period[1]>1000)
{
Time_Period[1] = 0;
if(page_id == 1){
oled_show_onepage();
}else if(page_id == 2){
oled_show_twopage();
}
if(tiltFlag==1){
send_time++;
if(send_time>=5){
send_time=0;
BY_U2_SendBuff(sound_buf,7);
}
}
printd_flag = !printd_flag;
if(printd_flag==1){
printf("\r\n\r\n\r\n\r\n");
printf("*********************\r\n");
printf("心率:%d次/分钟 血氧:%d%%\r\n",dis_hr,dis_spo2);
printf("体温:%d℃\r\n",body_temp);
if(tiltFlag==1)printf("老人摔倒\r\n");
else printf("活动正常\r\n");
printf("经度:%10.6f\r\n",lon_minutes);
printf("纬度:%10.6f\r\n",lat_minutes);
printf("*********************\r\n");
}
}
Sensor_Data_Handle();
}
}
void Sensor_Data_Handle() {
if(body_temp>temp_door||dis_hr>heart_door||(dis_spo2<oxy_door&&dis_spo2>0)||tiltFlag==1)beep=1;
else beep=0;
}
void get_all_sensor_data()
{
body_temp = DS18B20_Get_Temp();
Get_Max30102_Data();
FallDetection();
}
void oled_show_onepage(){
OLED_ShowChinese(16*0,0,0,16,1);
OLED_ShowChinese(16*1,0,1,16,1);
OLED_ShowChar(16*2,0,':',16,1);
OLED_ShowNum(16*2+8,0,dis_hr,3,16,1);
OLED_ShowChinese(16*4,0,2,16,1);
OLED_ShowChinese(16*5,0,3,16,1);
OLED_ShowChar(16*6,0,':',16,1);
OLED_ShowNum(16*6+8,0,dis_spo2,3,16,1);
OLED_ShowChinese(16*0,16,4,16,1);
OLED_ShowChinese(16*1,16,5,16,1);
OLED_ShowChar(16*2,16,':',16,1);
OLED_ShowNum(16*2+8,16,body_temp,2,16,1);
if(tiltFlag==1){
OLED_ShowChinese(16*4,16,20,16,1);
OLED_ShowChinese(16*5,16,21,16,1);
OLED_ShowChinese(16*6,16,22,16,1);
OLED_ShowChinese(16*7,16,23,16,1);
}else {
OLED_ShowChinese(16*4,16,18,16,1);
OLED_ShowChinese(16*5,16,19,16,1);
OLED_ShowChinese(16*6,16,11,16,1);
OLED_ShowChinese(16*7,16,12,16,1);
}
sprintf((char *)display,"%10.6f ",lon_minutes);
OLED_ShowChinese(16*0,32,13,16,1);
OLED_ShowChinese(16*1,32,15,16,1);
OLED_ShowChar(16*2,32,':',16,1);
OLED_ShowString(16*3,32,(u8*)display,16, 1);
sprintf((char *)display,"%10.6f ",lat_minutes);
OLED_ShowChinese(16*0,48,14,16,1);
OLED_ShowChinese(16*1,48,15,16,1);
OLED_ShowChar(16*2,48,':',16,1);
OLED_ShowString(16*3,48,(u8*)display,16, 1);
OLED_Refresh();
}
void oled_show_twopage(){
uint8_t a,b,c,d;
if(option==1){a=0;b=1;c=1;d=1;}
else if(option==2){a=1;b=0;c=1;d=1;}
else if(option==3){a=1;b=1;c=0;d=1;}
else {a=1;b=1;c=1;d=1;}
OLED_ShowChinese(16*0,0,0,16,1);
OLED_ShowChinese(16*1,0,1,16,1);
OLED_ShowChinese(16*2,0,16,16,1);
OLED_ShowChinese(16*3,0,17,16,1);
OLED_ShowChar(16*4,0,':',16,1);
OLED_ShowNum(16*4+8,0,heart_door,3,16,a);
OLED_ShowChinese(16*0,16,2,16,1);
OLED_ShowChinese(16*1,16,3,16,1);
OLED_ShowChinese(16*2,16,16,16,1);
OLED_ShowChinese(16*3,16,17,16,1);
OLED_ShowChar(16*4,16,':',16,1);
OLED_ShowNum(16*4+8,16,oxy_door,3,16,b);
OLED_ShowChinese(16*0,32,4,16,1);
OLED_ShowChinese(16*1,32,5,16,1);
OLED_ShowChinese(16*2,32,16,16,1);
OLED_ShowChinese(16*3,32,17,16,1);
OLED_ShowChar(16*4,32,':',16,1);
OLED_ShowNum(16*4+8,32,temp_door,3,16,c);
OLED_Refresh();
}
void TIM3_IRQHandler(void) {
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
key_num = KEY_Scan(0);
switch(key_num) {
case 1:
if(page_id==1){
page_id=2;
OLED_Clear();oled_show_twopage();
}else if(page_id==2){
option++;
if(option>3){
option=0;
page_id=1;
OLED_Clear();oled_show_onepage();
}else oled_show_twopage();
}
break;
case 2:
if(page_id==2){
if(option==1)heart_door+=1;
else if(option==2)oxy_door+=1;
else if(option==3)temp_door+=1;
oled_show_twopage();
}
break;
case 3:
if(page_id==2){
if(option==1)heart_door-=1;
else if(option==2)oxy_door-=1;
else if(option==3)temp_door-=1;
oled_show_twopage();
}
break;
case 4:
break;
}
Time_Period[0]++;
Time_Period[1]++;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
void FallDetection(void)
{
u8 i;
adxl345_read_average(&adx,&ady,&adz,10);
acc=ady;
acc2=adx;
if(acc<0)acc=-acc;
if(acc2<0)acc2=-acc2;
if(((u16)acc)>=190 || ((u16)acc2)>=190)
{
tiltFlag=1;
}else{
tiltFlag=0;
}
}