387 lines
9.6 KiB
C
387 lines
9.6 KiB
C
/**
|
||
* @file app_flow.c
|
||
* @author xxx
|
||
* @date 2023-08-30 08:58:43
|
||
* @brief 此文件用于管理各个任务流程
|
||
* @copyright Copyright (c) 2023 by xxx, All Rights Reserved.
|
||
*/
|
||
|
||
#include <math.h>
|
||
#include "app.h"
|
||
#include "main.h"
|
||
#include "flow.h"
|
||
#include "tim.h"
|
||
#include "filter.h"
|
||
|
||
#include "mode.h"
|
||
#include "at_hc24.h"
|
||
#include "bootload.h"
|
||
|
||
__IO flow_event_e flow_event = FLOW_EVENT_NORMAL; // 流程事件
|
||
|
||
static struct flow business_fw; // 业务流程
|
||
static struct flow system_fw; // 系统检测
|
||
static struct flow btn_fw; // 按键检测
|
||
static struct flow idle_fw; // 空闲任务
|
||
// bootloader
|
||
static struct flow bootload_system_fw; // BOOTLOAD系统流程
|
||
// 子任务
|
||
static struct flow current_fw; // 电流检测
|
||
|
||
uint16_t scheduler_use_time = 0;
|
||
|
||
/**
|
||
* @brief 菜单检测
|
||
* @return {*}
|
||
* @note
|
||
*/
|
||
void menu_inspection(void)
|
||
{
|
||
|
||
}
|
||
|
||
/**
|
||
* @brief 图标检测
|
||
* @return {*}
|
||
* @note
|
||
*/
|
||
static void icon_inspection(void)
|
||
{
|
||
if (FALSE == HART_IS_ENABLE())
|
||
{
|
||
driver_icon_enable.Bits.hart = 0; // 禁用HART
|
||
}
|
||
else
|
||
{
|
||
driver_icon_enable.Bits.hart = 1; // 使能HART
|
||
}
|
||
|
||
if (FALSE == BLE_IS_ENABLE()) // 检查蓝牙是否已启用
|
||
{
|
||
driver_icon_enable.Bits.bluetooth = 0; // 禁用蓝牙
|
||
}
|
||
else
|
||
{
|
||
driver_icon_enable.Bits.bluetooth = 1; // 使能蓝牙
|
||
}
|
||
|
||
driver_icon_enable.Bits.work_mode = (uint8_t)udevice.dev_work_mode; // 工作模式
|
||
|
||
// TODO 设备是否锁定检查
|
||
}
|
||
|
||
static void bootload_start_inspection(void)
|
||
{
|
||
}
|
||
|
||
/**
|
||
* @brief 温度检测任务
|
||
* @return {*}
|
||
* @note 该函数用于检测当前温度并执行相应的任务:
|
||
* 1. LCD的开启和关闭
|
||
*/
|
||
static void temperature_inspection(void)
|
||
{
|
||
// 定义一个布尔变量lcd_close,用于标记LCD是否已关闭
|
||
static BOOL lcd_close = FALSE;
|
||
// 获取当前温度
|
||
rt_data.temperature = get_temperature();
|
||
// 判断温度是否大于等于LCD工作温度最小值,如果是,则启动LCD
|
||
if (rt_data.temperature >= LCD_WORK_TEMP_MIN)
|
||
{
|
||
lcd_init(); // 初始化LCD
|
||
if (lcd_close == TRUE) // 检查LCD是否已关闭,如果是,则打开LCD
|
||
{
|
||
lcd_close = FALSE;
|
||
}
|
||
|
||
gui_set_scandir(udevice.display_direction); // 更新扫描方向
|
||
gui_open(); // 打开LCD
|
||
}
|
||
// 否则,关闭LCD
|
||
else
|
||
{
|
||
lcd_dinit();
|
||
lcd_close = TRUE;
|
||
}
|
||
}
|
||
|
||
/************************************************ 以下为应用线程 ************************************************/
|
||
|
||
/**
|
||
* @brief 电流检测任务(子任务)
|
||
* @return {*}
|
||
* @note 该函数用于检测当前电量并执行相应的任务:
|
||
* 1. HART的启动和关闭
|
||
* 2. 蓝牙的启动和关闭
|
||
*/
|
||
static uint8_t current_inspection(struct flow *fl)
|
||
{
|
||
FL_HEAD(fl);
|
||
// 定义一个浮点数变量loop_current,用于存储当前电流
|
||
static float32 current = 0.0;
|
||
// 计算当前电流,方法为读取ADC通道ADC_LOOP_CHANNEL的平均值并转换为浮点数
|
||
current = loop_current_convert(adc_result_average(ADCS_1, ADC_LOOP_CHANNEL));
|
||
|
||
// 判断当前电流是否大于等于3.8mA,如果是,则启动UART
|
||
if (current >= INPUT_CURRENT_MIN)
|
||
{
|
||
driver_init(); // 初始化驱动
|
||
if (FALSE == HART_IS_ENABLE()) // 检查HART是否已启用,如果否,则初始化HART UART
|
||
{
|
||
hart_uart_init();
|
||
}
|
||
}
|
||
// 否则,关闭UART
|
||
else
|
||
{
|
||
driver_dinit(); // 关闭驱动
|
||
hart_uart_dinit(); // 关闭HART UART
|
||
}
|
||
|
||
// 判断当前电流是否大于等于xx mA且工作模式在离线模式下,如果是,则启动蓝牙
|
||
if ((current >= BLE_CURRENT_WORK && udevice.dev_work_mode == OFF_LINE_MODE) ||
|
||
(current >= BLE_CURRENT_WORK && H24_BLE_OUTPUT_DBG == TRUE))
|
||
{
|
||
if (FALSE == BLE_IS_ENABLE()) // 检查蓝牙是否已启用,如果否,则初始化蓝牙
|
||
{
|
||
hart_ble_init();
|
||
}
|
||
else
|
||
{
|
||
// 检查蓝牙是否正常工作
|
||
if (BIT_IS_CLR(hc_24_state, BIT2))
|
||
{
|
||
h24_bluetooth_work(1);
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
|
||
if (BIT_IS_SET(hc_24_state, BIT0))
|
||
{
|
||
// 检查蓝牙名称
|
||
h24_bluetooth_work(2);
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
|
||
if (BIT_IS_CLR(hc_24_state, BIT1))
|
||
{
|
||
// 设置蓝牙名称
|
||
h24_bluetooth_work(3);
|
||
}
|
||
BIT_SET(hc_24_state, BIT2);
|
||
}
|
||
else
|
||
{
|
||
BIT_SET(hc_24_state, BIT2); // 如果蓝牙被主动连接的话,上面的AT指令不会有响应
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// h24_bluetooth_work(100);
|
||
}
|
||
}
|
||
}
|
||
// 否则,关闭蓝牙
|
||
else
|
||
{
|
||
}
|
||
FL_TAIL(fl);
|
||
}
|
||
|
||
/**
|
||
* @brief 按键检测任务
|
||
* @return {*}
|
||
*/
|
||
static uint8_t btn_inspection(struct flow *fl)
|
||
{
|
||
FL_HEAD(fl);
|
||
for (;;)
|
||
{
|
||
scheduler_time_start();
|
||
// 按键检测和处理
|
||
button_ticks();
|
||
scheduler_use_time = scheduler_time_stop();
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_10MSEC);
|
||
}
|
||
FL_TAIL(fl);
|
||
}
|
||
|
||
/**
|
||
* @brief 系统检测
|
||
* @param {struct flow *} fl
|
||
* @return {*}
|
||
*/
|
||
static uint8_t systom_inspection(struct flow *fl)
|
||
{
|
||
FL_HEAD(fl);
|
||
static BOOL run_first = TRUE;
|
||
for (;;)
|
||
{
|
||
scheduler_time_start();
|
||
// 当前电流检测
|
||
current_inspection(¤t_fw);
|
||
// 温度检测
|
||
temperature_inspection();
|
||
scheduler_use_time = scheduler_time_stop();
|
||
// 图标检测
|
||
icon_inspection();
|
||
scheduler_use_time = scheduler_time_stop();
|
||
if (run_first == TRUE)
|
||
{
|
||
run_first = FALSE;
|
||
|
||
#if LCD_DESIGN == FALSE
|
||
ENABLE_TIM(TIM7); // TIM7用于算法控制
|
||
#endif
|
||
}
|
||
else
|
||
{
|
||
scheduler_use_time = scheduler_time_stop();
|
||
|
||
fal_execution_inspection(1000);
|
||
scheduler_use_time = scheduler_time_stop();
|
||
|
||
set_app_preload_language_flag(udevice.display_language);
|
||
|
||
scheduler_use_time = scheduler_time_stop();
|
||
}
|
||
|
||
scheduler_use_time = scheduler_time_stop();
|
||
|
||
bootload_start_inspection();
|
||
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
|
||
}
|
||
FL_TAIL(fl);
|
||
}
|
||
|
||
/**
|
||
* @brief 业务流程
|
||
* @param {flow} *fl
|
||
* @return {*}
|
||
*/
|
||
static uint8_t business_inspection(struct flow *fl)
|
||
{
|
||
FL_HEAD(fl);
|
||
for (;;)
|
||
{
|
||
scheduler_time_start();
|
||
// 工作模式检测
|
||
mode_detection();
|
||
|
||
scheduler_use_time = scheduler_time_stop();
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_100MSEC);
|
||
}
|
||
FL_TAIL(fl);
|
||
}
|
||
|
||
/**
|
||
* @brief 空闲任务用来计算CPU占用率和内存使用率
|
||
* @param {flow} *fl
|
||
* @return {*}
|
||
* @note
|
||
*/
|
||
static uint8_t idle_inspection(struct flow *fl)
|
||
{
|
||
FL_HEAD(fl);
|
||
for (;;)
|
||
{
|
||
rt_data.cpu_percent = scheduler_time_occupancy_get(1000);
|
||
rt_data.cpu_temperature = adc_result_temperature(adc_result_average(ADCS_1, ADC_TEMP_CHANNEL));
|
||
rt_data.mem_percent = my_mem_perused(SRAMIN);
|
||
leds_toggle(LEDS_2_BLUE);
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
|
||
}
|
||
FL_TAIL(fl);
|
||
}
|
||
|
||
/**
|
||
* @brief bootload系统检测
|
||
* @param {struct flow *} fl
|
||
* @return {*}
|
||
*/
|
||
static uint8_t bootload_systom_inspection(struct flow *fl)
|
||
{
|
||
FL_HEAD(fl);
|
||
for (;;)
|
||
{
|
||
// 电流检测
|
||
current_inspection(¤t_fw);
|
||
|
||
if (TRUE == BLE_IS_ENABLE())
|
||
{
|
||
leds_off(LEDS_1_RED);
|
||
leds_toggle(LEDS_1_GREEN);
|
||
}
|
||
else
|
||
{
|
||
leds_off(LEDS_1_GREEN);
|
||
leds_toggle(LEDS_1_RED);
|
||
}
|
||
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
|
||
}
|
||
FL_TAIL(fl);
|
||
}
|
||
|
||
/**
|
||
* @brief: 流程启动
|
||
* @return {*}
|
||
*/
|
||
void flow_start(void)
|
||
{
|
||
if (TRUE == app_hart_is_idle())
|
||
{
|
||
switch (flow_event)
|
||
{
|
||
case FLOW_EVENT_NORMAL:
|
||
{
|
||
systom_inspection(&system_fw); // 系统检测
|
||
business_inspection(&business_fw); // 业务流程检测
|
||
btn_inspection(&btn_fw); // 按键检测
|
||
idle_inspection(&idle_fw); // 空闲任务用来计算CPU占用率
|
||
break;
|
||
}
|
||
case FLOW_EVENT_DIAGNOSIS:
|
||
{
|
||
systom_inspection(&system_fw); // 系统检测
|
||
current_inspection(¤t_fw); // 当前电流检测
|
||
break;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
|
||
hart_rx_process(); // HART接收处理
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief: bootload流程启动
|
||
* @return {*}
|
||
*/
|
||
void bootload_flow_start(void)
|
||
{
|
||
bootload_systom_inspection(&bootload_system_fw); // BOOTLOAD系统流程
|
||
if (BIT_IS_CLR(hc_24_state, BIT2) == FALSE)
|
||
{
|
||
bootload_inspection(); // BOOTLOAD检测
|
||
if (bootload_timeout() == TRUE)
|
||
{
|
||
// 如果超时退出bootload模式
|
||
set_app_preload_bootload_flag(FALSE);
|
||
leds_off_all();
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 初始化流程
|
||
* @return {*}
|
||
*/
|
||
void flow_init(void)
|
||
{
|
||
FL_INIT(¤t_fw); // 电流检测
|
||
FL_INIT(&system_fw); // 系统检测
|
||
FL_INIT(&business_fw); // 业务流程
|
||
FL_INIT(&btn_fw); // 按键检测
|
||
FL_INIT(&idle_fw); // 空闲任务
|
||
FL_INIT(&bootload_system_fw); // bootload系统流程
|
||
FL_INIT(¤t_fw); // 电流检测
|
||
}
|