/*Includes ----------------------------------------------*/ #include "system.h" #include "exception.h" #include "ble_comm.h" #include "app_charge.h" #include "app_flash.h" #include "app_self_checking.h" #include "hal_led.h" #include "hal_monitor.h" #include "hal_ble_uart0.h" #include "hal_ble_client.h" #include "hal_mode_manage.h" /*Private macro ------------------------------------------------*/ #define SELF_CHECK_WHEEL_EMPTY_TIMES_MAX MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX //允许最大轮空次数 #define SELF_CHECK_ITEMS 6 //自检项数 #define SELF_CHECK_ITEM_SUCCESS 0 //自检项目成功 #define SELF_CHECK_ITEM_FAIL 1 //自检项目失败 #define SELF_CHECK_ITEM_FRONT_SENSOR_INDEX 0 //自检项目 - 前脚传感器 #define SELF_CHECK_ITEM_BACK_SENSOR_INDEX 1 //自检项目 - 后脚传感器 #define SELF_CHECK_ITEM_BATTERY_INDEX 2 //自检项目 - 电量 #define SELF_CHECK_ITEM_CHARGE_INDEX 3 //自检项目 - 充电电压 #define SELF_CHECK_ITEM_ANT_INDEX 4 //自检项目 - 天线 #define SELF_CHECK_ITEM_SUSPEND_MODE_INDEX 5 //自检项目 - 异常挂起 #define SELF_CHECK_PROCESS_CYCLE 10 //自检线程周期 - ms为单位 #define SELF_CHECK_LED_DISPLAY_TIME (3000/SELF_CHECK_PROCESS_CYCLE) //自检灯显示时长 - ms为单位 #define SELF_CHECK_LED_TOGGLE_TIME (200/SELF_CHECK_PROCESS_CYCLE) //自检灯翻转时长 - ms为单位 #define SELF_CHECK_SCAN_TRIGGER_TIMEOUT (10000/SELF_CHECK_PROCESS_CYCLE) //扫描触发自检超时时间 - ms为单位 #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; //阶段字段 int wheel_empty_times; //轮空次数 uint8_t result[SELF_CHECK_ITEMS]; //自检结果 uint8_t item_fail_num; //自检项目失败数量 SELF_CHECK_LED_SWITCH_e led_switch; //自检灯开关 uint16_t led_display_time; //自检灯显示时长 uint16_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_MODE_SUSPEND_OVERFLOW); Except_ClearExceptype(EXCEPT_ANT_DAMAGE); } static void scan_report(unsigned char *adv_data, unsigned short adv_data_len,signed char rssi) { //查找目标广播名字 if(advdata_name_find(adv_data, adv_data_len,SELF_CHECK_SCAN_TARGET_ADV_NAME)) { ob_self_check.is_scan_success = true; } } static void app_self_checking_Process(void) { switch(app_charge_Getstate()) { case BLE_Client_T_CHARGE_PULLOUT: //没充电 ob_self_check.is_scan_success = false; //重置扫描成功标志位 ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT; //重置扫描触发超时时间 advdata_report_Evt_Regist(NULL); //禁止扫描触发 break; case BLE_Client_T_CHARGE_DONE: //充电 case BLE_Client_T_CHARGE_INSERT: if(ob_self_check.scan_trigger_timeout != 0) //扫描监测 { advdata_report_Evt_Regist(scan_report); ob_self_check.scan_trigger_timeout--; } if(ob_self_check.is_scan_success == true) { ob_self_check.scan_trigger_timeout = 0; //清空扫描触发超时时间 clear_all_except(); //清除所有异常 memset(ob_self_check.result, SELF_CHECK_ITEM_SUCCESS, SELF_CHECK_ITEMS); hal_mode_set(HAL_MODE_SELF_CHECK); //扫描监测到目标广播名,开启自检模式 ob_self_check.stage = SELF_CHECK_STAGE_CHECK; //进入自检阶段 } if(ob_self_check.scan_trigger_timeout == 0 && ob_self_check.is_scan_success == false) //超时且没扫描到广播名 { ob_self_check.stage = SELF_CHECK_STAGE_READY; //设置阶段为准备阶段 } break; } switch(ob_self_check.stage) { case SELF_CHECK_STAGE_READY: //准备阶段 ob_self_check.wheel_empty_times = 0; //清空轮空次数 LED_Stop(LED_SELF_CHECK); ob_self_check.led_switch = SELF_CHECK_LED_OFF; //设置自检灯为关 ob_self_check.led_display_time = 0; //清空自检灯显示时长 ob_self_check.led_toggle_time = 0; //清空自检灯翻转时长 if(hal_mode_get() == HAL_MODE_SELF_CHECK)hal_mode_set(HAL_MODE_NORMAL); //当前是自检模式,退出。 break; case SELF_CHECK_STAGE_CHECK: //自检阶段 ob_self_check.wheel_empty_times++; if(ob_self_check.wheel_empty_times >= SELF_CHECK_WHEEL_EMPTY_TIMES_MAX){ ob_self_check.wheel_empty_times = SELF_CHECK_WHEEL_EMPTY_TIMES_MAX; //灯显示结果,逻辑应该是先显示完灯后在去重新检测 if(ob_self_check.led_display_time != 0) { if(ob_self_check.item_fail_num != 0) { if(ob_self_check.led_toggle_time != 0) { if(ob_self_check.led_switch == SELF_CHECK_LED_ON)LED_Start(LED_SELF_CHECK,COLOR_RED); if(ob_self_check.led_switch == SELF_CHECK_LED_OFF)LED_Start(LED_SELF_CHECK,COLOR_BLACK); ob_self_check.led_toggle_time--; if(ob_self_check.led_toggle_time == 0) { if(ob_self_check.led_switch == SELF_CHECK_LED_ON)ob_self_check.led_switch = SELF_CHECK_LED_OFF; else{ ob_self_check.led_switch = SELF_CHECK_LED_ON; ob_self_check.item_fail_num--; } ob_self_check.led_toggle_time = SELF_CHECK_LED_TOGGLE_TIME; } } } else { LED_Start(LED_SELF_CHECK,COLOR_BLACK); } ob_self_check.led_display_time--; return; } /*获取自检结果前---------------------------------------------------------------------*/ ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS; ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS; ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_SUCCESS; ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_SUCCESS; ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_SUCCESS; ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_SUCCESS; ob_self_check.item_fail_num = 0; /*获取自检结果中---------------------------------------------------------------------*/ //若进入不了自检模式 if(hal_mode_get() != HAL_MODE_SELF_CHECK){ Except_SetExceptype(EXCEPT_DATA_FRONT_ACC); Except_SetExceptype(EXCEPT_DATA_FRONT_GRY); Except_SetExceptype(EXCEPT_DATA_FRONT_MAG); Except_SetExceptype(EXCEPT_MODE_SUSPEND_OVERFLOW); } //自检结果 - 异常挂起 if(Except_IsError(EXCEPT_MODE_SUSPEND_OVERFLOW)){ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_SUSPEND_MODE_INDEX + 1;} //自检结果 - 前脚传感器 if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;} if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;} if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;} if(Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.item_fail_num = SELF_CHECK_ITEM_FRONT_SENSOR_INDEX + 1;} //自检结果 - 后脚传感器 if(Except_IsError(EXCEPT_DATA_BACK_MAG)){ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BACK_SENSOR_INDEX + 1;} //自检结果 - 电量 if(Except_IsError(EXCEPT_DATA_BATTERY)){ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BATTERY_INDEX + 1;} //自检结果 - 充电电压 if(Except_IsError(EXCEPT_DATA_CHARGE)){ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_CHARGE_INDEX + 1;} //自检结果 - 天线 if(Except_IsError(EXCEPT_ANT_DAMAGE)){ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_ANT_INDEX + 1;} /*获取自检结果后---------------------------------------------------------------------*/ //设置自检灯显示时长和翻转时长 if(ob_self_check.item_fail_num != 0){ ob_self_check.led_switch = SELF_CHECK_LED_ON; ob_self_check.led_display_time = SELF_CHECK_LED_DISPLAY_TIME; ob_self_check.led_toggle_time = SELF_CHECK_LED_TOGGLE_TIME; }else{ ob_self_check.led_switch = SELF_CHECK_LED_ON; if(mFlash.mClient.isConfig == 'C') { LED_Start(LED_SELF_CHECK,COLOR_BLUE); } else { LED_Start(LED_SELF_CHECK,COLOR_CYAN); } } } break; } } static void app_self_checking_printf_process(void) { SEGGER_RTT_printf(0,"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_MODE_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.wheel_empty_times = 0; 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(SELF_CHECK_PROCESS_CYCLE,"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; }