#include "sysconfig.h"
#include "at32a423.h"
#include "bsp_flash.h"
#include "app_get_json.h"
#include "string.h"


flash_para_struct g_flash_para;
json_data_struct g_json_data;
sys_config_struct g_sys_config;
sys_staus_struct g_sys_staus;

const sys_config_struct default_config = 
{
	.output_mode = 1,
	.output_ax = 12,
	.output_ay = 12,
	.min_trigger_res_value = 3000,
	.max_trigger_res_value = 40000,
	.div_trigger_res =2700
};

int save_sys_config(sys_config_struct *sys_config)
{
		g_flash_para.output_mode = sys_config->output_mode;
		g_flash_para.output_ax = sys_config->output_ax;
		g_flash_para.output_ay = sys_config->output_ay;
		g_flash_para.min_trigger_res_value = sys_config->min_trigger_res_value;
		g_flash_para.max_trigger_res_value = sys_config->max_trigger_res_value;
		g_flash_para.div_trigger_res = sys_config->div_trigger_res;
		return save_config_params(&g_flash_para);
}

void init_sys_config(sys_config_struct *sys_config)
{	
		//save_sys_config(&default_config);
	 if(load_config_params(&g_flash_para) != 1)
	 {
			memcpy((uint8_t *)sys_config,(uint8_t *)&default_config,sizeof(sys_config_struct));
			save_sys_config(sys_config);
			return ;
	 }
	 
	
	
	 
	 if(g_flash_para.min_trigger_res_value == 0xffff)
	 {
		sys_config->min_trigger_res_value = default_config.min_trigger_res_value;
	 }
	 else
	 {
		 sys_config->min_trigger_res_value = g_flash_para.min_trigger_res_value;
	 }
	 
	 if(g_flash_para.max_trigger_res_value == 0xffff)
	 {
		sys_config->max_trigger_res_value = default_config.max_trigger_res_value;
	 }
	 else
	 {
		 sys_config->max_trigger_res_value = g_flash_para.max_trigger_res_value;
	 }
	 if(g_flash_para.div_trigger_res == 0xffff)
	 {
		sys_config->div_trigger_res = default_config.div_trigger_res;
	 }
	 else
	 {
		 sys_config->div_trigger_res = g_flash_para.div_trigger_res;
	 }		 
	 		 
	 
	 
	 if(g_flash_para.output_mode > 1)
	 {
		 sys_config->output_mode = default_config.output_mode;
	 }
	 else
	 {
		 sys_config->output_mode = g_flash_para.output_mode;
	 }
}

//抓取json信息，如果成功，保存进内存
int cjson_get_sys_config(sys_config_struct *sys_config)
{
		int result;
	
		g_json_data.output_mode = sys_config->output_mode;
		g_json_data.output_ax = sys_config->output_ax;
		g_json_data.output_ay = sys_config->output_ay;
		g_json_data.min_trigger_res_value = sys_config->min_trigger_res_value;
		g_json_data.max_trigger_res_value = sys_config->max_trigger_res_value;
		g_json_data.div_trigger_res = sys_config->div_trigger_res;
	
		result = get_Json_data(&g_json_data);
	
		if(result == 1){
			memcpy(sys_config, &g_json_data, sizeof(sys_config_struct));
			g_flash_para.output_mode=g_json_data.output_mode;
			g_flash_para.output_ax=g_json_data.output_ax;
			g_flash_para.output_ay=g_json_data.output_ay;
			g_flash_para.min_trigger_res_value=g_json_data.min_trigger_res_value;
			g_flash_para.max_trigger_res_value=g_json_data.max_trigger_res_value;
			g_flash_para.div_trigger_res=g_json_data.div_trigger_res;
			load_config_params(&g_flash_para);
		}
		return result;
}

