/*Includes ----------------------------------------------*/ #include "system.h" #include "exception.h" #include "app_charge.h" #include "app_flash.h" #include "app_self_checking.h" #include "hal_led.h" #include "hal_mt.h" #include "hal_ble_uart0.h" #include "hal_ble_client.h" #include "app_flash.h" #include "app_pair_chargerpin.h" #include "bsp_time.h" #include "hal_scan_manage.h" #include "ble_comm.h" #include "bll_imu.h" /*Private macro ------------------------------------------------*/ #define SELF_CHECK_WHEEL_EMPTY_TIMES_MAX MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX //允许最大轮空次数 #define SELF_CHECK_ITEM_SUCCESS 0 //自检项目成功 #define SELF_CHECK_ITEM_FAIL 1 //自检项目失败 enum{ SELF_CHECK_ITEM_FRONT_SENSOR=0, //自检项目 - 前脚传感器 SELF_CHECK_ITEM_BACK_SENSOR, //自检项目 - 后脚传感器 SELF_CHECK_ITEM_BATTERY, //自检项目 - 电量 SELF_CHECK_ITEM_CHARGE, //自检项目 - 充电电压 SELF_CHECK_ITEM_ANT, //自检项目 - 天线 SELF_CHECK_ITEM_SUSPEND_MODE, //自检项目 - 异常挂起 SELF_CHECK_ITEMS //自检项数 }; #define SELF_CHECK_LED_DISPLAY_TIME 3000 //自检灯显示时长 - ms为单位 #define SELF_CHECK_LED_TOGGLE_TIME 200 //自检灯翻转时长 - ms为单位 #define SELF_CHECK_SCAN_TRIGGER_TIMEOUT 10000 //扫描触发自检超时时间 - ms为单位 #define SELF_CHECK_SET_IMU_TIMEOUT 20000 //等待IMU初始化成功的时间 #define SELF_CHECK_SCAN_TARGET_ADV_NAME "SH_SELF_CHECK" //扫描触发的目标广播名字 /*STRUCTION -------------------------------------------------- --*/ typedef enum { SELF_CHECK_STAGE_READY, //准备阶段 SELF_CHECK_STAGE_CHECK, //自检阶段 } SELF_CHECK_STAGE_e; typedef enum{ SELF_CHECK_LED_ON, //自检灯 - 开 SELF_CHECK_LED_OFF, //自检灯 - 关 } SELF_CHECK_LED_SWITCH_e; typedef struct self_check { /*private member*/ SELF_CHECK_STAGE_e stage; //阶段字段 uint8_t result[SELF_CHECK_ITEMS]; //自检结果 uint8_t item_fail_num; //自检项目失败数量 SELF_CHECK_LED_SWITCH_e led_switch; //自检灯开关 uint32_t led_display_time; //自检灯显示时长 uint32_t led_toggle_time; //自检灯翻转时长 uint32_t scan_trigger_timeout; //扫描触发自检超时时间 bool is_scan_success; //是否找到广播名字 } Self_Check_t; /*Local Variable -----------------------------------------------*/ static Self_Check_t ob_self_check; /*Local Functions ----------------------------------------------*/ static void clear_all_except(void) { Except_ClearExceptype(EXCEPT_DATA_BACK_MAG); Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC); Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY); Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG); Except_ClearExceptype(EXCEPT_DATA_CHARGE); Except_ClearExceptype(EXCEPT_DATA_BATTERY); Except_ClearExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW); Except_ClearExceptype(EXCEPT_ANT_DAMAGE); } static void selft_scan_cb(void) { ob_self_check.is_scan_success = true; } static scan_reslt_t scan_reslt_handle ={ .handle = selft_scan_cb, .scanname =SELF_CHECK_SCAN_TARGET_ADV_NAME, }; //static pair_reslt_t UartPair_result ={ // .flag = 0, //}; static void app_self_check_getreslut(){ uint8_t i=0; /*获取自检结果前---------------------------------------------------------------------*/ for(i=0;i SELF_CHECK_SCAN_TRIGGER_TIMEOUT) //超时且没扫描到广播名 { state = 2; ob_self_check.is_scan_success = false; //重置扫描成功标志位 state_selft =3; hal_ble_scan_result_Clear(&scan_reslt_handle); //禁止扫描触发 ST_scan_stop(); DEBUG_LOG("ob_self_check.scan_trigger_timeout\r\n"); } } break; case 2: if(BLE_CHARGE_PULLOUT == app_charge_Getstate()){ state = 0; // app_UartPair_Clear(&UartPair_result); DEBUG_LOG("ob_self_check.scan BLE_Client_T_CHARGE_PULLOUT\r\n"); } break; default: break; } switch(state_selft) { case 0: if(SELF_CHECK_STAGE_CHECK == ob_self_check.stage){//扫描成功 MT_Run(500); bll_imu_Resume_config_param(&selfcheck_bll_imu_param_t);//开启自检模式 state_selft = 1; imu_configcnt =0; } break; case 1: if(BLE_CHARGE_PULLOUT == app_charge_Getstate()){ state_selft = 3; } else { front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&selfcheck_bll_imu_param_t); back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&selfcheck_bll_imu_param_t); if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){ state_selft = 2; displaycnt =0; app_self_check_getreslut(); } else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){ bll_imu_Resume_config_param(&selfcheck_bll_imu_param_t); imu_configcnt++; DEBUG_LOG(">>>>>>222 out sleep mode,%d %d\n",front_CS,back_CS); if(imu_configcnt > 20){imu_configcnt =0; Except_SetExceptype(EXCEPT_DATA_FRONT_ACC); Except_SetExceptype(EXCEPT_DATA_FRONT_GRY); Except_SetExceptype(EXCEPT_DATA_FRONT_MAG); Except_SetExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW); state_selft = 2; displaycnt =0; } } } break; case 2: if(displaycnt < ob_self_check.item_fail_num){ if(TIME_GetTicks() - ob_self_check.led_toggle_time >= SELF_CHECK_LED_TOGGLE_TIME){ob_self_check.led_toggle_time = TIME_GetTicks(); if(ob_self_check.led_switch == SELF_CHECK_LED_ON){ LED_Start(LED_SELF_CHECK,COLOR_LIGHRED); ob_self_check.led_switch = SELF_CHECK_LED_OFF; } else{ LED_Start(LED_SELF_CHECK,COLOR_BLACK); ob_self_check.led_switch = SELF_CHECK_LED_ON; displaycnt++; } } } else if(TIME_GetTicks() - ob_self_check.led_display_time >= SELF_CHECK_LED_DISPLAY_TIME){//显示时间到了,重现读取自检结果,并显示 ob_self_check.led_display_time = TIME_GetTicks(); displaycnt =0; app_self_check_getreslut(); } break; case 3: state_selft = 0; ob_self_check.stage = SELF_CHECK_STAGE_READY; LED_Start(LED_SELF_CHECK,COLOR_BLACK); LED_Stop(LED_SELF_CHECK); ob_self_check.led_switch = SELF_CHECK_LED_OFF; //设置自检灯为关 bll_imu_Resume_unregister_config_param(&selfcheck_bll_imu_param_t);//关掉自检配置 break; default: break; } } //static void app_self_checking_printf_process(void) //{ // DEBUG_LOG("self_check: FRONT_SENSOR:%d BACK_SENSOR:%d BATTERY:%d CHARGE:%d ANT:%d suspend_mode:%d scan_trigger_timeout:%d\n", \ // Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG), \ // Except_IsError(EXCEPT_DATA_BACK_MAG), \ // Except_IsError(EXCEPT_DATA_BATTERY), \ // Except_IsError(EXCEPT_DATA_CHARGE), \ // Except_IsError(EXCEPT_ANT_DAMAGE), \ // Except_IsError(EXCEPT_IMU_SUSPEND_OVERFLOW),ob_self_check.scan_trigger_timeout); //} /*API ----------------------------------------------------------*/ void app_self_checking_Init(void) { ob_self_check.stage = SELF_CHECK_STAGE_READY; ob_self_check.item_fail_num = 0; ob_self_check.led_switch = SELF_CHECK_LED_OFF; ob_self_check.led_display_time = 0; ob_self_check.led_toggle_time = 0; ob_self_check.is_scan_success = false; ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT; Process_Start(10,"app_self_checking_Process",app_self_checking_Process); // Process_Start(1000,"app_self_checking_printf_process",app_self_checking_printf_process); } uint32_t app_self_checking_get_scan_trigger_timeout(void) { return ob_self_check.scan_trigger_timeout; }