This repository has been archived on 2025-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hd/User/app_flow.c

272 lines
6.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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"
static struct flow business_tm; // 业务流程
static struct flow systom_tm; // 系统检测
static struct flow btn_tm; // 按键
static struct flow idle_tm; // 空闲任务
static BOOL first_run = TRUE;
uint16_t scheduler_use_time = 0;
/**
* @brief 电流检测任务
* @return {*}
* @note 该函数用于检测当前电量并执行相应的任务
*/
static void current_inspection(void)
{
float32 current = 0.0; // 定义一个浮点数变量loop_current用于存储当前电流
// 计算当前电流方法为读取ADC通道ADC_LOOP_CHANNEL的平均值并转换为浮点数
current = loop_current_convert(adc_get_result_average(ADC_LOOP_CHANNEL));
// 判断当前电流是否大于等于3.8mA如果是则启动UART
if (current >= INPUT_CURRENT_MIN)
{
driver_init(); // 初始化驱动
if (FALSE == HART_IS_ENABLE()) // 检查HART是否已启用
{
hart_uart_init(); // 初始化HART UART
}
}
else // 否则关闭UART
{
driver_dinit(); // 关闭驱动
hart_uart_dinit(); // 关闭HART UART
}
// 判断当前电流是否大于等于xx mA如果是则启动蓝牙
if (current >= BLE_CURRENT_WORK)
{
if (FALSE == BLE_IS_ENABLE()) // 检查蓝牙是否已启用
{
hart_ble_init(); // 初始化蓝牙
}
else
{
// 检查蓝牙是否正常工作
if (BIT_IS_CLR(hc_24_state, BIT2))
{
h24_bluetooth_work(1);
delay_ms(100);
if (BIT_IS_SET(hc_24_state, BIT0))
{
// 检查蓝牙名称
h24_bluetooth_work(2);
delay_ms(100);
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(4);
}
}
}
else // 否则,关闭蓝牙
{
hart_ble_dinit(); // 关闭蓝牙
}
}
/**
* @brief 温度检测任务
* @return {*}
* @note 该函数用于检测当前温度并执行相应的任务
*/
static void temperature_inspection(void)
{
// 获取当前温度
temperature = get_temperature();
// 记录温度最大最小值
if (temperature > uRtData.max_recorded_temp)
{
uRtData.max_recorded_temp = temperature;
}
if (temperature < uRtData.min_recorded_temp)
{
uRtData.min_recorded_temp = temperature;
}
}
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.WorkMode; // 工作模式
// TODO 设备是否锁定检查
}
/**
* @brief 压力检测任务
* @return {*}
* @note 该函数用于检测当前压力并执行相应的任务
*/
static void pressure_inspection(void)
{
get_pressure();
}
/************************************************ 以下为应用线程 ************************************************/
/**
* @brief 系统检测
* @param {struct flow *} fl
* @return {*}
*/
static uint8_t systom_inspection(struct flow *fl)
{
FL_HEAD(fl);
first_run = TRUE;
for (;;)
{
scheduler_time_start();
// 当前电流检测
current_inspection();
scheduler_use_time = scheduler_time_stop();
// 温度检测
temperature_inspection();
scheduler_use_time = scheduler_time_stop();
// 图标检测
icon_inspection();
scheduler_use_time = scheduler_time_stop();
// 压力检测
pressure_inspection();
scheduler_use_time = scheduler_time_stop();
// 动作次数检测
scheduler_use_time = scheduler_time_stop();
if (first_run == TRUE)
{
first_run = FALSE;
}
else
{
}
scheduler_use_time = scheduler_time_stop();
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
}
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 {flow} *fl
* @return {*}
*/
static uint8_t business_inspection(struct flow *fl)
{
FL_HEAD(fl);
for (;;)
{
scheduler_time_start();
// 工作模式检测
mode_detection((uint8_t)uDevice.WorkMode);
scheduler_use_time = scheduler_time_stop();
FL_LOCK_DELAY(fl, FL_CLOCK_100MSEC * 2);
}
FL_TAIL(fl);
}
/**
* @brief 空闲任务用来计算CPU占用率
* @param {flow} *fl
* @return {*}
* @note
*/
static uint8_t idle_inspection(struct flow *fl)
{
FL_HEAD(fl);
for (;;)
{
cpu_percent = scheduler_time_occupancy_get(1000);
leds_toggle(LEDS_1_GREEN);
leds_toggle(LEDS_2_BLUE);
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
}
FL_TAIL(fl);
}
/**
* @brief: 流程启动
* @return {*}
*/
void flow_start(void)
{
systom_inspection(&systom_tm); // 系统检测
btn_inspection(&btn_tm); // 按键检测
business_inspection(&business_tm); // 业务流程检测
idle_inspection(&idle_tm); // 空闲任务用来计算CPU占用率
}
/**
* @brief 初始化流程
* @return {*}
*/
void flow_init(void)
{
FL_INIT(&business_tm);
FL_INIT(&systom_tm);
FL_INIT(&btn_tm); // 按键初始化
FL_INIT(&idle_tm);
}