697 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			697 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "main.h"
 | 
						||
#include "mywin_inc.h"
 | 
						||
#include "system_file.h"
 | 
						||
#include "system_updata.h"
 | 
						||
#include "date.h"
 | 
						||
 | 
						||
#include "cjson.h"
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
/*--------------------系统设置参数---------------------------------*/
 | 
						||
 | 
						||
 | 
						||
#ifndef BOOTLOADER
 | 
						||
	#define OS_RTT
 | 
						||
	#ifdef OS_RTT
 | 
						||
		#include "rthw.h"
 | 
						||
		#define IRQ_DISABLE() rt_base_t irq_stat=rt_hw_interrupt_disable( )
 | 
						||
		#define IRQ_ENABLE()	rt_hw_interrupt_enable (irq_stat)
 | 
						||
	#else
 | 
						||
		#include "os.h"
 | 
						||
		#define IRQ_DISABLE() {CPU_SR_ALLOC(); CPU_CRITICAL_ENTER();}
 | 
						||
		#define IRQ_ENABLE()	CPU_CRITICAL_EXIT()
 | 
						||
	#endif
 | 
						||
#else
 | 
						||
	#define IRQ_DISABLE()  {}
 | 
						||
	#define IRQ_ENABLE() 	{}
 | 
						||
#endif
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//系统设置结构体
 | 
						||
static SysFile_SetStruct *g_sysSet=0;
 | 
						||
		
 | 
						||
		
 | 
						||
		
 | 
						||
		
 | 
						||
//获取系统设置结构体
 | 
						||
SysFile_SetStruct *SysFile_GetSysFile(void)
 | 
						||
{
 | 
						||
	return g_sysSet;
 | 
						||
}
 | 
						||
		
 | 
						||
 | 
						||
 | 
						||
//闹钟转化为json
 | 
						||
cJSON *SysFile_AlarmToJson(AlarmStruct *alarm)
 | 
						||
{
 | 
						||
	cJSON *root = cJSON_CreateObject();
 | 
						||
	cJSON_AddNumberToObject(root, "year", alarm->year);
 | 
						||
	cJSON_AddNumberToObject(root, "month", alarm->month);
 | 
						||
	cJSON_AddNumberToObject(root, "day", alarm->day);
 | 
						||
	cJSON_AddNumberToObject(root, "hour", alarm->hour);
 | 
						||
	cJSON_AddNumberToObject(root, "min", alarm->min);
 | 
						||
	cJSON_AddNumberToObject(root, "week", alarm->week);
 | 
						||
	cJSON_AddNumberToObject(root, "power", alarm->power);
 | 
						||
	cJSON_AddNumberToObject(root, "tip", alarm->tip);
 | 
						||
 | 
						||
	return root;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//设置转化为json
 | 
						||
cJSON *SysFile_SetToJson(SysFile_SetStruct *set)
 | 
						||
{
 | 
						||
	cJSON *root = cJSON_CreateObject();
 | 
						||
 | 
						||
//	cJSON_AddNumberToObject(root, "nfcPower", set->nfcPower);
 | 
						||
 | 
						||
//	cJSON_AddNumberToObject(root, "bluetoothPower", set->bluetoothPower);
 | 
						||
//	cJSON_AddStringToObject(root, "bluetoothName", set->bluetoothName);
 | 
						||
//	cJSON_AddStringToObject(root, "bluetoothMac", set->bluetoothMac);
 | 
						||
 | 
						||
//	cJSON_AddNumberToObject(root, "heartRatePower", set->heartRatePower);
 | 
						||
 | 
						||
	cJSON_AddNumberToObject(root, "screenLightPower", set->screenLightPower);
 | 
						||
	cJSON_AddNumberToObject(root, "screenLight", set->screenLight);
 | 
						||
	cJSON_AddNumberToObject(root, "screenOffTime", set->screenOffTime);
 | 
						||
	cJSON_AddNumberToObject(root, "screenAutoLight", set->screenAutoLight);
 | 
						||
 | 
						||
	cJSON_AddStringToObject(root, "bootPic", set->bootPic);
 | 
						||
 | 
						||
	cJSON_AddNumberToObject(root, "alarmNum", set->alarmNum);
 | 
						||
	cJSON *js_array = cJSON_CreateArray();
 | 
						||
	cJSON_AddItemToObject(root, "alarm", js_array);
 | 
						||
	for (int i = 0; i<set->alarmNum; i++)
 | 
						||
	{
 | 
						||
		cJSON_AddItemToArray(js_array, SysFile_AlarmToJson(&set->alarm[i]));
 | 
						||
	}
 | 
						||
	cJSON_AddStringToObject(root, "alarmRing", set->alarmRing);
 | 
						||
 | 
						||
	cJSON_AddNumberToObject(root, "time12Hours", set->time12Hours);
 | 
						||
 | 
						||
	cJSON_AddStringToObject(root, "backPicPath", set->backPicPath);
 | 
						||
 | 
						||
//	cJSON_AddNumberToObject(root, "highStand", set->highStand);
 | 
						||
//	cJSON_AddNumberToObject(root, "highOff", set->highOff);
 | 
						||
//	
 | 
						||
//	cJSON_AddItemToObject(root, "heartMax",cJSON_CreateIntArray(set->heartMax,7));
 | 
						||
//	cJSON_AddItemToObject(root, "heartMin",cJSON_CreateIntArray(set->heartMin,7));
 | 
						||
//	cJSON_AddItemToObject(root, "tempMax",cJSON_CreateIntArray(set->tempMax,7));
 | 
						||
//	cJSON_AddItemToObject(root, "tempMin",cJSON_CreateIntArray(set->tempMin,7));
 | 
						||
//	cJSON_AddItemToObject(root, "humiMax",cJSON_CreateIntArray(set->humiMax,7));
 | 
						||
//	cJSON_AddItemToObject(root, "humiMin",cJSON_CreateIntArray(set->humiMin,7));
 | 
						||
//	cJSON_AddItemToObject(root, "preMax",cJSON_CreateIntArray(set->preMax,7));
 | 
						||
//	cJSON_AddItemToObject(root, "preMin",cJSON_CreateIntArray(set->preMin,7));
 | 
						||
//	cJSON_AddItemToObject(root, "highMax",cJSON_CreateIntArray(set->highMax,7));
 | 
						||
//	cJSON_AddItemToObject(root, "highMin",cJSON_CreateIntArray(set->highMin,7));
 | 
						||
 | 
						||
	return root;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//保存设置文件
 | 
						||
void SysFile_SaveSetFile(void)
 | 
						||
{
 | 
						||
	u32 file_size = SYSFILE_SETFILE_SIZE;
 | 
						||
	SysFile_SetStruct *set = SysFile_GetSysFile();
 | 
						||
	cJSON *root = SysFile_SetToJson(set);
 | 
						||
	char *out = cJSON_PrintUnformatted(root);
 | 
						||
	int json_size = strlen(out) + 1;
 | 
						||
	if (json_size>file_size)
 | 
						||
	{
 | 
						||
		//一般情况下这个结构体不可能超过4K
 | 
						||
		while (1);
 | 
						||
	}
 | 
						||
	u8 *databuff = mymalloc(file_size);
 | 
						||
	mymemset(databuff, 0, file_size);
 | 
						||
	mymemcpy(databuff, out, json_size);
 | 
						||
	if (FLASH_FindFile(SYSFILE_SETFILE_NAME, 0) == 0)
 | 
						||
	{
 | 
						||
		u32 flash_write_addr = FLASH_GetUsed();
 | 
						||
		FLASH_CheckErase(flash_write_addr, file_size);
 | 
						||
		FLASH_AddFile(SYSFILE_SETFILE_NAME, flash_write_addr, file_size);
 | 
						||
		FLASH_WriteData((u8*)databuff, flash_write_addr, file_size);
 | 
						||
	}
 | 
						||
	else
 | 
						||
	{
 | 
						||
		u32 file_addr = FLASH_FindFile(SYSFILE_SETFILE_NAME, 0);
 | 
						||
		FLASH_CheckErase(file_addr, file_size);
 | 
						||
		FLASH_WriteData((u8*)databuff, file_addr, file_size);
 | 
						||
	}
 | 
						||
	myfree(databuff);
 | 
						||
	cJSON_Delete(root);
 | 
						||
	myfree(out);
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//将json转化为闹钟结构体
 | 
						||
int SysFile_JsonToAlarm(AlarmStruct *alarm, cJSON *json)
 | 
						||
{
 | 
						||
	cJSON *temp=0;
 | 
						||
	temp=cJSON_GetObjectItem(json, "year");
 | 
						||
	if (temp) alarm->year = temp->valueint;
 | 
						||
	temp = cJSON_GetObjectItem(json, "month");
 | 
						||
	if (temp) alarm->month= temp->valueint;
 | 
						||
	temp= cJSON_GetObjectItem(json, "day");
 | 
						||
	if (temp) alarm->day = temp->valueint;
 | 
						||
	temp = cJSON_GetObjectItem(json, "hour");
 | 
						||
	if (temp) alarm->hour= temp->valueint;
 | 
						||
	temp = cJSON_GetObjectItem(json, "min");
 | 
						||
	if (temp) alarm->min= temp->valueint;
 | 
						||
	temp = cJSON_GetObjectItem(json, "week");
 | 
						||
	if (temp) alarm->week= temp->valueint;
 | 
						||
	temp = cJSON_GetObjectItem(json, "power");
 | 
						||
	if (temp) alarm->power= temp->valueint;
 | 
						||
	temp = cJSON_GetObjectItem(json, "tip");
 | 
						||
	if (temp) alarm->tip= temp->valueint;
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//将json转化为设置结构体,生成这个结构体
 | 
						||
SysFile_SetStruct *SysFile_JsonToSet(char *jsonstr)
 | 
						||
{
 | 
						||
	cJSON *temp=0;
 | 
						||
	cJSON *root = cJSON_Parse(jsonstr);
 | 
						||
	SysFile_SetStruct *set= mymalloc(sizeof(SysFile_SetStruct));
 | 
						||
	mymemset(set, 0, sizeof(SysFile_SetStruct));
 | 
						||
	char *str = 0;
 | 
						||
 | 
						||
//	set->nfcPower=cJSON_GetObjectItem(root, "nfcPower")->valueint;
 | 
						||
//	
 | 
						||
//	set->bluetoothPower = cJSON_GetObjectItem(root, "bluetoothPower")->valueint;
 | 
						||
//	str = cJSON_GetObjectItem(root, "bluetoothName")->valuestring;
 | 
						||
//	mymemcpy(set->bluetoothName, str, strlen(str) + 1);
 | 
						||
//	str = cJSON_GetObjectItem(root, "bluetoothMac")->valuestring;
 | 
						||
//	mymemcpy(set->bluetoothMac, str, strlen(str) + 1);
 | 
						||
 | 
						||
//	set->heartRatePower = cJSON_GetObjectItem(root, "heartRatePower")->valueint;
 | 
						||
 | 
						||
	temp=cJSON_GetObjectItem(root, "screenLightPower");
 | 
						||
	if (temp) set->screenLightPower = temp->valueint;
 | 
						||
	temp=cJSON_GetObjectItem(root, "screenLightPower");
 | 
						||
	if (temp) set->screenLight = temp->valueint;
 | 
						||
	temp=cJSON_GetObjectItem(root, "screenOffTime");
 | 
						||
	if (temp) set->screenOffTime = temp->valueint;
 | 
						||
	temp=cJSON_GetObjectItem(root, "screenAutoLight");
 | 
						||
	if (temp) set->screenAutoLight = temp->valueint;
 | 
						||
 | 
						||
	temp=cJSON_GetObjectItem(root, "bootPic");
 | 
						||
	if (temp) { str = temp->valuestring;
 | 
						||
	mymemcpy(set->bootPic, str, strlen(str) + 1);}
 | 
						||
 | 
						||
	temp=cJSON_GetObjectItem(root, "alarmNum");
 | 
						||
	if (temp)
 | 
						||
	{
 | 
						||
		set->alarmNum = temp->valueint;
 | 
						||
		cJSON *alarm = cJSON_GetObjectItem(root, "alarm");
 | 
						||
		for (int i = 0; i < set->alarmNum; i++)
 | 
						||
		{
 | 
						||
			cJSON *json = cJSON_GetArrayItem(alarm, i);
 | 
						||
			SysFile_JsonToAlarm(&set->alarm[i], json);
 | 
						||
		}
 | 
						||
	}
 | 
						||
	temp=cJSON_GetObjectItem(root, "alarmRing");
 | 
						||
	if (temp) { str = temp->valuestring;
 | 
						||
	mymemcpy(set->alarmRing, str, strlen(str) + 1);}
 | 
						||
 | 
						||
	temp=cJSON_GetObjectItem(root, "time12Hours");
 | 
						||
	if (temp) set->time12Hours = temp->valueint;
 | 
						||
 | 
						||
	temp=cJSON_GetObjectItem(root, "backPicPath");
 | 
						||
	if (temp) { str = temp->valuestring;
 | 
						||
	mymemcpy(set->backPicPath, str, strlen(str) + 1);}
 | 
						||
 | 
						||
//	set->highStand = cJSON_GetObjectItem(root, "highStand")->valueint;
 | 
						||
//	set->highOff = cJSON_GetObjectItem(root, "highOff")->valueint;
 | 
						||
 | 
						||
//	cJSON *js_temp = cJSON_GetObjectItem(root, "heartMax");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->heartMax[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "heartMin");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->heartMin[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "tempMax");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->tempMax[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "tempMin");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->tempMin[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "humiMax");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->humiMax[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "humiMin");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->humiMin[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "preMax");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->preMax[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "preMin");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->preMin[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "highMax");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->highMax[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
//	js_temp = cJSON_GetObjectItem(root, "highMin");
 | 
						||
//	for (int i = 0; i < 7; i++) { set->highMin[i] = cJSON_GetArrayItem(js_temp, i)->valueint; }
 | 
						||
	
 | 
						||
	cJSON_Delete(root);
 | 
						||
	return set;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//取得设置参数
 | 
						||
void SysFile_GetSetFile (void )
 | 
						||
{
 | 
						||
	if (g_sysSet == 0)
 | 
						||
	{
 | 
						||
		u32 file_size = SYSFILE_SETFILE_SIZE;
 | 
						||
		char *jsonstr = mymalloc(file_size);
 | 
						||
		if (FLASH_FindFile(SYSFILE_SETFILE_NAME, 0))
 | 
						||
		{
 | 
						||
			FLASH_ReadFile(SYSFILE_SETFILE_NAME, 0, (u8*)jsonstr, file_size);
 | 
						||
			g_sysSet = SysFile_JsonToSet(jsonstr);
 | 
						||
		}
 | 
						||
		else
 | 
						||
		{
 | 
						||
			g_sysSet = mymalloc(sizeof(SysFile_SetStruct));
 | 
						||
			mymemset(g_sysSet, 0, sizeof(SysFile_SetStruct));
 | 
						||
		}
 | 
						||
		myfree(jsonstr);
 | 
						||
	}
 | 
						||
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
static void SysFile_InitTest(void);
 | 
						||
 | 
						||
//设置文件初始化
 | 
						||
void SysFile_SetFileInit (void)
 | 
						||
{
 | 
						||
	SysFile_GetSetFile ();
 | 
						||
	
 | 
						||
	//初始化测试数据
 | 
						||
	SysFile_InitTest();
 | 
						||
	
 | 
						||
	//初始化闹钟
 | 
						||
	for (int i=0;i<g_sysSet->alarmNum;i++)
 | 
						||
	{
 | 
						||
		SysFile_InitAlarm (&g_sysSet->alarm[i]);
 | 
						||
	}
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
static void SysFile_InitTest(void)
 | 
						||
{
 | 
						||
	//默认的背景图片
 | 
						||
	if (g_sysSet->backPicPath[0] == 0)
 | 
						||
	{
 | 
						||
		SysFile_SetBackPicPath("背景.pic");
 | 
						||
	}
 | 
						||
 | 
						||
	//默认的启动图片
 | 
						||
	if (g_sysSet->bootPic[0] == 0)
 | 
						||
	{
 | 
						||
		SysFile_SetBootPicPath("飞马.pic");
 | 
						||
	}
 | 
						||
 | 
						||
	//添加几个闹钟,测试用
 | 
						||
	if (g_sysSet->alarmNum == 0)
 | 
						||
	{
 | 
						||
		AlarmStruct alarm = { 0 };
 | 
						||
		alarm.hour = 10;
 | 
						||
		alarm.min = 20;
 | 
						||
		alarm.power = 1;
 | 
						||
		alarm.week = 0x3;
 | 
						||
		SysFile_AddAlarm(&alarm);
 | 
						||
		alarm.power = 0;
 | 
						||
		alarm.week = 0;
 | 
						||
		SysFile_AddAlarm(&alarm);
 | 
						||
		alarm.hour = 7;
 | 
						||
		alarm.min = 0;
 | 
						||
		alarm.week = 0x7f;
 | 
						||
		SysFile_AddAlarm(&alarm);
 | 
						||
	}
 | 
						||
 | 
						||
	//测试数据
 | 
						||
//	for (int i=0;i<7;i++)
 | 
						||
//	{
 | 
						||
//		g_sysSet->highMax[i]=i*100+200*i;
 | 
						||
//		g_sysSet->highMin[i]=i*100;
 | 
						||
//		g_sysSet->preMax[i]=i*i*100;
 | 
						||
//		g_sysSet->preMin[i]=400;
 | 
						||
//		g_sysSet->humiMax[i]=i*i*i;
 | 
						||
//		g_sysSet->humiMin[i]=i*i;
 | 
						||
//		g_sysSet->heartMax[i]=150-i*10;
 | 
						||
//		g_sysSet->heartMin[i]=i*10;
 | 
						||
//	}
 | 
						||
	
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//获取保存的心率数据
 | 
						||
int SysFile_GetHeartData (int *max,int *min)
 | 
						||
{
 | 
						||
	SysFile_SetStruct *set=SysFile_GetSysFile( );
 | 
						||
 | 
						||
	for (int i=0;i<7;i++)
 | 
						||
	{
 | 
						||
//		max[i]=set->heartMax[i];
 | 
						||
//		min[i]=set->heartMin[i];
 | 
						||
	}
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//获取保存的温度数据
 | 
						||
int SysFile_GetTempData (int *max,int *min)
 | 
						||
{
 | 
						||
	SysFile_SetStruct *set=SysFile_GetSysFile( );
 | 
						||
 | 
						||
	for (int i=0;i<7;i++)
 | 
						||
	{
 | 
						||
//		max[i]=set->tempMax[i];
 | 
						||
//		min[i]=set->tempMin[i];
 | 
						||
	}
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//获取保存的湿度数据
 | 
						||
int SysFile_GetHumiData (int *max,int *min)
 | 
						||
{
 | 
						||
	SysFile_SetStruct *set=SysFile_GetSysFile( );
 | 
						||
 | 
						||
	for (int i=0;i<7;i++)
 | 
						||
	{
 | 
						||
//		max[i]=set->humiMax[i];
 | 
						||
//		min[i]=set->humiMin[i];
 | 
						||
	}
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//获取保存的气压数据
 | 
						||
int SysFile_GetPreData (int *max,int *min)
 | 
						||
{
 | 
						||
	SysFile_SetStruct *set=SysFile_GetSysFile( );
 | 
						||
 | 
						||
	for (int i=0;i<7;i++)
 | 
						||
	{
 | 
						||
//		max[i]=set->preMax[i];
 | 
						||
//		min[i]=set->preMin[i];
 | 
						||
	}
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//获取保存的高度数据
 | 
						||
int SysFile_GetHighData (int *max,int *min)
 | 
						||
{
 | 
						||
	SysFile_SetStruct *set=SysFile_GetSysFile( );
 | 
						||
 | 
						||
	for (int i=0;i<7;i++)
 | 
						||
	{
 | 
						||
//		max[i]=set->highMax[i];
 | 
						||
//		min[i]=set->highMin[i];
 | 
						||
	}
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//设置背景图片路径
 | 
						||
int SysFile_SetBackPicPath (char *path)
 | 
						||
{
 | 
						||
	if (g_sysSet==0) return -1;
 | 
						||
	mymemcpy (g_sysSet->backPicPath,path,strlen(path)+1);
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//设置闹钟铃声路径
 | 
						||
int SysFile_SetAlarmRingPath (char *path)
 | 
						||
{
 | 
						||
	if (g_sysSet==0) return -1;
 | 
						||
	mymemcpy (g_sysSet->alarmRing,path,strlen(path)+1);
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//设置启动图片路径
 | 
						||
int SysFile_SetBootPicPath(char *path)
 | 
						||
{
 | 
						||
	if (g_sysSet == 0) return -1;
 | 
						||
	mymemcpy(g_sysSet->bootPic, path, strlen(path) + 1);
 | 
						||
	return 0;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//添加一个闹钟
 | 
						||
int SysFile_AddAlarm (AlarmStruct *a)
 | 
						||
{
 | 
						||
	int ret=-1;
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		if (g_sysSet->alarmNum<SYSFILE_SETFILE_ALARMNUM)
 | 
						||
		{
 | 
						||
			mymemcpy (&g_sysSet->alarm[g_sysSet->alarmNum],a,sizeof (AlarmStruct));
 | 
						||
			SysFile_InitAlarm (&g_sysSet->alarm[g_sysSet->alarmNum]);
 | 
						||
			g_sysSet->alarmNum++;
 | 
						||
			ret=0;
 | 
						||
		}
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//修改一个闹钟
 | 
						||
int SysFile_AlterAlarm (int index,AlarmStruct *a)
 | 
						||
{
 | 
						||
	int ret=-1;
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		if (index<g_sysSet->alarmNum)
 | 
						||
		{
 | 
						||
			mymemcpy (&g_sysSet->alarm[index],a,sizeof (AlarmStruct));
 | 
						||
			SysFile_InitAlarm (&g_sysSet->alarm[index]);
 | 
						||
			ret=0;
 | 
						||
		}
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//删除一个闹钟
 | 
						||
int SysFile_DelAlarm (int index)
 | 
						||
{
 | 
						||
	int ret=-1;
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		if (index<g_sysSet->alarmNum)
 | 
						||
		{
 | 
						||
			mymemset (&g_sysSet->alarm[index],0,sizeof (AlarmStruct));
 | 
						||
			g_sysSet->alarmNum--;
 | 
						||
			for (int i=index;i<g_sysSet->alarmNum;i++)
 | 
						||
			{
 | 
						||
				mymemcpy (&g_sysSet->alarm[i],&g_sysSet->alarm[i+1],sizeof (AlarmStruct));
 | 
						||
				
 | 
						||
			}
 | 
						||
			ret=0;
 | 
						||
		}
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//删除所有闹钟
 | 
						||
int SysFile_DelAllAlarm (void)
 | 
						||
{
 | 
						||
	int ret=-1;
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		for (int i=0;i<g_sysSet->alarmNum;i++)
 | 
						||
		{
 | 
						||
			mymemset (&g_sysSet->alarm[i],0,sizeof (AlarmStruct));
 | 
						||
		}
 | 
						||
		g_sysSet->alarmNum=0;
 | 
						||
		ret=0;
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//取得指定索引的闹钟
 | 
						||
int SysFile_GetAlarm (int index,AlarmStruct *a)
 | 
						||
{
 | 
						||
	int ret=-1;
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		if (index<g_sysSet->alarmNum)
 | 
						||
		{
 | 
						||
			mymemcpy (a,&g_sysSet->alarm[index],sizeof (AlarmStruct));
 | 
						||
			ret=0;
 | 
						||
		}
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
//获取闹钟个数
 | 
						||
int SysFile_GetAlarmNum (void)
 | 
						||
{
 | 
						||
	int ret=0;
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		ret=g_sysSet->alarmNum;
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//取得下一天
 | 
						||
static void get_nextDay (int *year,int *month,int *day)
 | 
						||
{
 | 
						||
	if (*day<date_get_month_day_max(*year,*month))
 | 
						||
	{
 | 
						||
		(*day)++;
 | 
						||
	}
 | 
						||
	else
 | 
						||
	{
 | 
						||
		(*day)=1;
 | 
						||
		if (*month<12)
 | 
						||
		{
 | 
						||
			(*month)++;
 | 
						||
		}
 | 
						||
		else
 | 
						||
		{
 | 
						||
			(*month)=1;
 | 
						||
			(*year)++;
 | 
						||
		}
 | 
						||
	}
 | 
						||
}
 | 
						||
 | 
						||
//获取星期号,星期天至星期六 0~6
 | 
						||
static int get_week (int year,int month,int day)
 | 
						||
{
 | 
						||
	int ret=date_get_week_by_day(year,month,day);
 | 
						||
	if (ret==7) ret=0;
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
//升级闹钟日期
 | 
						||
void SysFile_UpDataAlarm (AlarmStruct *a)
 | 
						||
{
 | 
						||
	int year=a->year;
 | 
						||
	int month=a->month;
 | 
						||
	int day=a->day;
 | 
						||
	if (a->week==0) return;
 | 
						||
	do
 | 
						||
	{
 | 
						||
		get_nextDay(&year,&month,&day);
 | 
						||
	}
 | 
						||
	while (((1<<get_week(year,month,day))&a->week)==0);
 | 
						||
	a->year=year;
 | 
						||
	a->month=month;
 | 
						||
	a->day=day;
 | 
						||
}
 | 
						||
 | 
						||
//初始化闹钟
 | 
						||
void SysFile_InitAlarm (AlarmStruct *a)
 | 
						||
{
 | 
						||
	int year=0;int month=0;int day=0;
 | 
						||
	int hour=0;int min=0;int sec=0;
 | 
						||
	if (a->week==0) return;
 | 
						||
	date_rtc_get_date (&year,&month,&day);
 | 
						||
	date_rtc_get_time (&hour,&min,&sec);
 | 
						||
	int fresh=0;//此项为1时需要加一天
 | 
						||
	if (a->hour<hour)
 | 
						||
	{
 | 
						||
		fresh=1;
 | 
						||
	}
 | 
						||
	else if (a->hour==hour)
 | 
						||
	{
 | 
						||
		if (a->min<min)
 | 
						||
			fresh=1;
 | 
						||
	}
 | 
						||
	//闹钟时间已经过了,设置下一天的闹钟
 | 
						||
	if (fresh) get_nextDay(&year,&month,&day);
 | 
						||
	while (((1<<get_week(year,month,day))&a->week)==0)
 | 
						||
	{
 | 
						||
		get_nextDay(&year,&month,&day);
 | 
						||
	}
 | 
						||
	a->year=year;
 | 
						||
	a->month=month;
 | 
						||
	a->day=day;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
//校验闹钟时间到了吗,1,有闹钟时间到,0,没有
 | 
						||
//到时间的闹钟自动把日期设置为下一天
 | 
						||
int SysFile_CheckAlarm (void) 
 | 
						||
{
 | 
						||
	int ret=0;
 | 
						||
	int year=0;int month=0;int day=0;int hour=0;int min=0;int sec=0;
 | 
						||
	date_rtc_get_date (&year,&month,&day);
 | 
						||
	date_rtc_get_time (&hour,&min,&sec);
 | 
						||
	IRQ_DISABLE();
 | 
						||
	if (g_sysSet)
 | 
						||
	{
 | 
						||
		for (int i=0;i<g_sysSet->alarmNum;i++)
 | 
						||
		{
 | 
						||
			if (g_sysSet->alarm[i].power)
 | 
						||
			{
 | 
						||
				if ((g_sysSet->alarm[i].year==year)&&(g_sysSet->alarm[i].month==month)&&
 | 
						||
					(g_sysSet->alarm[i].day==day)&&(g_sysSet->alarm[i].hour==hour)&&
 | 
						||
					(g_sysSet->alarm[i].min==min))
 | 
						||
				{
 | 
						||
					//此时闹钟时间到
 | 
						||
					ret=1;
 | 
						||
					//修改闹钟日期为下一次
 | 
						||
					SysFile_UpDataAlarm(&g_sysSet->alarm[i]);
 | 
						||
				}
 | 
						||
			}
 | 
						||
		}
 | 
						||
	}
 | 
						||
	IRQ_ENABLE();
 | 
						||
	return ret;
 | 
						||
}
 | 
						||
 | 
						||
 |