#include "stm32f10x.h"                  // Device header
#include "Key.h"
#include "OLED.h"
#include "MyRTC.h"
#include "MPU6050.h"
#include "Delay.h"
#include <math.h>
#include "pedometer.h"
#include "Timer.h"

uint8_t KeyNum,flag=1,stepnum=0,Setstep=10;
uint16_t alarmselect[]={00,00};
int16_t AX, AY, AZ, GX, GY, GZ;	
	
int menu1(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	//开启GPIOA的时钟
															//使用各个外设前必须开启时钟，否则对外设的操作无效
	
	/*GPIO初始化*/
	GPIO_InitTypeDef GPIO_InitStructure;					//定义结构体变量
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;		//GPIO模式，赋值为推挽输出模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;				//GPIO引脚，赋值为第1号引脚
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		//GPIO速度，赋值为50MHz
	
	GPIO_Init(GPIOA, &GPIO_InitStructure);					//将赋值后的构体变量传递给GPIO_Init函数
	GPIO_SetBits(GPIOA, GPIO_Pin_0);						//函数内部会自动根据结构体的参数配置相应寄存器
															//实现GPIOA的初始化
	
	OLED_ShowString(0,0,"    当前时间     ",OLED_8X16);
	OLED_ShowString(0,16,"    今日步数     ",OLED_8X16);
	OLED_ShowString(0,32,"    闹钟设置     ",OLED_8X16);
	OLED_ShowString(0,48,"    步数设置     ",OLED_8X16);
	OLED_Update();
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)  //上一项
		{
			flag--;
			if(flag==0){flag=4;}
		}
		if(KeyNum==2)  //下一项
		{
			flag++;
			if(flag==5){flag=1;}
		}
		if(KeyNum==3)  //确认
		{
			OLED_Clear();
			OLED_Update();
			return flag;
		}
		switch(flag)
		{
			case 1:
			{
				OLED_ShowString(0,0,"    当前时间     ",OLED_8X16);
				OLED_ShowString(0,16,"    今日步数     ",OLED_8X16);
				OLED_ShowString(0,32,"    闹钟设置     ",OLED_8X16);
				OLED_ShowString(0,48,"    步数设置     ",OLED_8X16);
				OLED_ReverseArea(0,0,128,16);
				OLED_Update();
				break;
			}
			case 2:
			{
				OLED_ShowString(0,0,"    当前时间     ",OLED_8X16);
				OLED_ShowString(0,16,"    今日步数     ",OLED_8X16);
				OLED_ShowString(0,32,"    闹钟设置     ",OLED_8X16);
				OLED_ShowString(0,48,"    步数设置     ",OLED_8X16);
				OLED_ReverseArea(0,16,128,16);
				OLED_Update();
				break;
			}
			case 3:
			{
				OLED_ShowString(0,0,"    当前时间     ",OLED_8X16);
				OLED_ShowString(0,16,"    今日步数     ",OLED_8X16);
				OLED_ShowString(0,32,"    闹钟设置     ",OLED_8X16);
				OLED_ShowString(0,48,"    步数设置     ",OLED_8X16);
				OLED_ReverseArea(0,32,128,16);
				OLED_Update();
				break;
			}
			case 4:
			{
				OLED_ShowString(0,0,"    当前时间     ",OLED_8X16);
				OLED_ShowString(0,16,"    今日步数     ",OLED_8X16);
				OLED_ShowString(0,32,"    闹钟设置     ",OLED_8X16);
				OLED_ShowString(0,48,"    步数设置     ",OLED_8X16);
				OLED_ReverseArea(0,48,128,16);
				OLED_Update();
				break;
			}
		}
	}			
}

int menu2_RencentTime(void)
{
	MyRTC_ReadTime();							//RTC读取时间，最新的时间存储到MyRTC_Time数组中
	OLED_ShowString(0,0,"<-                ",OLED_8X16);	
	OLED_Printf(0,16,OLED_8X16,"Data:%d-%d-%d",MyRTC_Time[0],MyRTC_Time[1],MyRTC_Time[2]);
	OLED_Printf(0,32,OLED_8X16,"Time:%d:%d:%d",MyRTC_Time[3],MyRTC_Time[4],MyRTC_Time[5]);
	OLED_Update();

	while(1)
	{
		MyRTC_ReadTime();
		KeyNum=Key_GetNum();
		if(KeyNum==3)  //确认
		{
			OLED_Clear();
			OLED_Update();
			return flag;
		}

		OLED_ShowString(0,0,"<-                ",OLED_8X16);
		OLED_Printf(0,16,OLED_8X16,"Data:%d-%d-%d",MyRTC_Time[0],MyRTC_Time[1],MyRTC_Time[2]);
		OLED_Printf(0,32,OLED_8X16,"Time:%d:%d:%d",MyRTC_Time[3],MyRTC_Time[4],MyRTC_Time[5]);
		OLED_ReverseArea(0,0,128,16);
		OLED_Update();
	}
}

int menu2_TodayStep(void)
{
	MPU6050_Init();
	Timer_Init();
	MPU6050_GetData(&AX, &AY, &AZ, &GX, &GY, &GZ);	
	OLED_ShowString(0,0,"<-                ",OLED_8X16);
	stepnum=calculate_steps(AX,AY,AZ);
	OLED_ShowNum(48,32,stepnum,2,OLED_8X16);
	OLED_Update();
	
	while(1)
	{
		KeyNum=Key_GetNum();
		MPU6050_GetData(&AX, &AY, &AZ, &GX, &GY, &GZ);
		if(KeyNum==3)  //确认
		{
			OLED_Clear();
			OLED_Update();
			return flag;
		}

		OLED_ShowString(0,0,"<-                ",OLED_8X16);
		OLED_ReverseArea(0,0,128,16);
		stepnum=calculate_steps(AX,AY,AZ);
		OLED_ShowNum(48,32,stepnum,2,OLED_8X16);
		OLED_Update();
	}
}

int menu2_SetClock(void)
{
	uint8_t clockflag=0;
	OLED_ShowString(0,0,"<-                ",OLED_8X16);
	OLED_ShowNum(32,16,alarmselect[0],2,OLED_8X16);
	OLED_ShowString(48,16,":",OLED_8X16);
	OLED_ShowNum(56,16,alarmselect[1],2,OLED_8X16);
	OLED_ReverseArea(0,0,128,16);
	OLED_Update();

	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)
		{
			clockflag++;
			if(clockflag==3){clockflag=0;}
		}
		if(KeyNum==2)
		{
			if(clockflag==1)
			{
				alarmselect[0]++;
				if(alarmselect[0]==24){alarmselect[0]=0;}
			}
			if(clockflag==2)
			{
				alarmselect[1]++;
				if(alarmselect[1]==60){alarmselect[1]=0;}
			}
		}
		if(KeyNum==3)  //确认
		{
			OLED_Clear();
			OLED_Update();
			if(clockflag==0){return flag;}
		}
		switch(clockflag)
		{
			case 0:
			{
				OLED_ShowString(0,0,"<-                ",OLED_8X16);
				OLED_ShowNum(32,16,alarmselect[0],2,OLED_8X16);
				OLED_ShowString(48,16,":",OLED_8X16);
				OLED_ShowNum(56,16,alarmselect[1],2,OLED_8X16);
				OLED_ReverseArea(0,0,128,16);
				OLED_Update();
				break;
			}
			case 1:
			{
				OLED_ShowString(0,0,"<-                ",OLED_8X16);
				OLED_ShowNum(32,16,alarmselect[0],2,OLED_8X16);
				OLED_ShowString(48,16,":",OLED_8X16);
				OLED_ShowNum(56,16,alarmselect[1],2,OLED_8X16);
				OLED_ReverseArea(32,16,16,16);
				OLED_Update();
				break;
			}
			case 2:
			{
				OLED_ShowString(0,0,"<-                ",OLED_8X16);
				OLED_ShowNum(32,16,alarmselect[0],2,OLED_8X16);
				OLED_ShowString(48,16,":",OLED_8X16);
				OLED_ShowNum(56,16,alarmselect[1],2,OLED_8X16);
				OLED_ReverseArea(56,16,16,16);
				OLED_Update();
				break;
			}
		}
	}
}

int menu2_SetStep(void)
{
	OLED_ShowString(0,0,"<-                ",OLED_8X16);
	OLED_ShowNum(48,32,Setstep,3,OLED_8X16);
	OLED_Update();

	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==3)  //确认
		{
			OLED_Clear();
			OLED_Update();
			return flag;
		}
		if(KeyNum==1)
		{
			Setstep+=5;
		}
		if(KeyNum==2)
		{
			Setstep-=5;
		}
		OLED_ShowString(0,0,"<-                ",OLED_8X16);
		OLED_ShowNum(48,32,Setstep,3,OLED_8X16);
		OLED_ReverseArea(0,0,128,16);
		OLED_Update();
	}
}

void Pedometer1_Tick(void)
{
	static uint8_t i=0;
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)    //判断是否为定时器TIM2更新中断
	{
		if(stepnum>=Setstep && i<6)
		{
			GPIO_ResetBits(GPIOA, GPIO_Pin_0);		//将PA1引脚设置为低电平，蜂鸣器鸣叫
			i++;
		}
		else
		{
			GPIO_SetBits(GPIOA, GPIO_Pin_0);			
		}
		if(stepnum<Setstep){i=0;}
	}
}

void Pedometer2_Tick(void)
{
	static uint8_t j=0;
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)    //判断是否为定时器TIM2更新中断
	{
		if(MyRTC_Time[3]==alarmselect[0] && MyRTC_Time[4]==alarmselect[1] &&j<3)
		{
			GPIO_ResetBits(GPIOA, GPIO_Pin_0);		//将PA1引脚设置为低电平，蜂鸣器鸣叫
			j++;
		}
		else
		{
			GPIO_SetBits(GPIOA, GPIO_Pin_0);			
		}
	}
}
