From 31578725f98a3a49ffafd4bc9afdcf3b503c7b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=90=83=E6=B2=B9=E7=82=B8=E9=B8=A1?= <1425962791@qq.com> Date: Fri, 5 Dec 2025 20:16:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5PD=E5=BF=AB=E5=85=85=E8=AF=B1?= =?UTF-8?q?=E9=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Power_Pico/User/GUI/screens/ui_SetPage.c | 95 ++++++++++++++++++++-- Power_Pico/User/Tasks/Src/user_PDUFPTask.c | 54 +++++++++--- Power_Pico/User/Tasks/Src/user_TasksInit.c | 14 ++-- 3 files changed, 136 insertions(+), 27 deletions(-) diff --git a/Power_Pico/User/GUI/screens/ui_SetPage.c b/Power_Pico/User/GUI/screens/ui_SetPage.c index 9260265..b49f801 100644 --- a/Power_Pico/User/GUI/screens/ui_SetPage.c +++ b/Power_Pico/User/GUI/screens/ui_SetPage.c @@ -5,8 +5,6 @@ #include "../ui.h" #include "./ui_PPSPage.h" -// comunicate with PD UFP Task -#include "user_PDUFPTask.h" lv_obj_t * ui_SetPage = NULL; // backlight @@ -33,16 +31,51 @@ static lv_obj_t * ui_LabelPPS = NULL; static lv_obj_t * ui_PanelAbout = NULL; static lv_obj_t * ui_LabelAbout = NULL; -static lv_timer_t * setting_timer = NULL; +static lv_timer_t * _setting_timer = NULL; + +static lv_obj_t * msgbox = NULL; // msgbox 对象 +static lv_obj_t * spinner = NULL; // spinner 对象 +static bool waiting_for_signal = false; // 标志位,表示是否在等待信号 static lv_obj_t * panels[6]; // 存储所有 panel 的指针 static int current_panel_index = 0; // 当前选中的 panel 索引 +static void show_msgbox(void); +static void hide_msgbox(void); + // event funtions +// comunicate with PD UFP Task +#include "user_PDUFPTask.h" + +/////////////////////// Timer ////////////////////// +static void _setting_timer_cb(lv_timer_t * timer) { + PD_handle_event_t pd_handle_event; + // 非阻塞获取消息 + if(osMessageQueueGet(PD_handle_event_MsgQueue, &pd_handle_event, NULL, 0)==osOK) { + if(pd_handle_event == PD_EVT_PPS_READY) { + // 隐藏等待框 + hide_msgbox(); + lv_lib_pm_goto("PPS Page", NULL); + } else if(pd_handle_event == PD_EVT_PPS_FAILED) { + // 隐藏等待框 + hide_msgbox(); + } + } +} + +///////////////// outer function /////////////////// + +static void _send_pps_start_msg(void) { + PD_command_msg_t pd_ui_msg; + pd_ui_msg.event = PD_CMD_START; + osMessageQueuePut(PD_cmd_MessageQueue, &pd_ui_msg, 0, 1); +} + #include "key.h" void ui_set_page_key_handler(void *key_event) { + if(waiting_for_signal) return; int panel_count = sizeof(panels) / sizeof(panels[0]); // key boot if (((key_event_t*)key_event)->id == KEY_ID_B && ((key_event_t*)key_event)->type == KEY_EVT_CLICK) @@ -140,9 +173,10 @@ void ui_set_page_key_handler(void *key_event) // 发送开启PD诱骗的信号 if (((key_event_t*)key_event)->id == KEY_ID_Y && ((key_event_t*)key_event)->type == KEY_EVT_CLICK) { - PD_UI_MSG_t pd_ui_msg; - pd_ui_msg.event = PD_UI_EVT_START; - osMessageQueuePut(PD_UI_MessageQueue, &pd_ui_msg, 0, 5); + // 发送pps开始信号到PD UFP Task + _send_pps_start_msg(); + // 弹出 msgbox 等待PD完成,并锁住按键操作 + show_msgbox(); // lv_lib_pm_goto("PPS Page", NULL); } break; @@ -156,6 +190,8 @@ void ui_set_page_key_handler(void *key_event) } } +/////////////////////// para_initialize ////////////////////// + static void on_setpage_loaded(lv_event_t * e) { // 当 SetPage 页面加载完成后,滚动到当前选中的 panel lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON); @@ -185,6 +221,46 @@ static void _setting_init(void) { } } +/////////////////////// ui_initialize ////////////////////// + +// 显示 msgbox +static void show_msgbox(void) { + + // 获取当前视角y pos + int32_t view_y = lv_obj_get_scroll_top(ui_SetPage); + // 创建 msgbox + msgbox = lv_obj_create(ui_SetPage); + lv_obj_set_size(msgbox, 200, 160); + lv_obj_center(msgbox); + lv_obj_set_pos(msgbox, 0, view_y); + lv_obj_set_style_bg_color(msgbox, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_DEFAULT); + lv_obj_set_style_bg_opa(msgbox, 255, LV_PART_MAIN | LV_STATE_DEFAULT); + + // 添加 spinner + spinner = lv_spinner_create(msgbox); + lv_obj_set_size(spinner, 60, 60); + lv_obj_center(spinner); + + // 添加文本 + lv_obj_t * label = lv_label_create(msgbox); + lv_label_set_text(label, "Waiting for PD..."); + lv_obj_set_align(label, LV_ALIGN_BOTTOM_MID); + lv_obj_set_style_text_font(label, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT); + + // 设置标志位 + waiting_for_signal = true; +} + +// 隐藏 msgbox +static void hide_msgbox(void) { + if (msgbox) { + lv_obj_del(msgbox); + msgbox = NULL; + spinner = NULL; + } + waiting_for_signal = false; +} + // build funtions void ui_SetPage_screen_init(void) @@ -344,6 +420,9 @@ void ui_SetPage_screen_init(void) panels[5] = ui_PanelAbout; lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED); + // timer + _setting_timer = lv_timer_create(_setting_timer_cb, 500, NULL); + // lv_obj_add_event_cb(ui_SetPage, on_setpage_loaded, LV_EVENT_SCREEN_LOADED, NULL); @@ -351,7 +430,7 @@ void ui_SetPage_screen_init(void) void ui_SetPage_screen_destroy(void) { - if(setting_timer) { - lv_timer_delete(setting_timer); + if(_setting_timer) { + lv_timer_delete(_setting_timer); } } diff --git a/Power_Pico/User/Tasks/Src/user_PDUFPTask.c b/Power_Pico/User/Tasks/Src/user_PDUFPTask.c index 4d8e10e..8a34e48 100644 --- a/Power_Pico/User/Tasks/Src/user_PDUFPTask.c +++ b/Power_Pico/User/Tasks/Src/user_PDUFPTask.c @@ -10,8 +10,29 @@ /* Private variables ---------------------------------------------------------*/ +uint8_t _pd_working_status = 0; +osTimerId_t PD_UFP_Task_timer_id; + /* Private function prototypes -----------------------------------------------*/ +static void _timer_callback(void *argument) +{ + PD_handle_event_t ready; + static uint8_t time_count = 0; + time_count++; + if(is_PPS_ready()) { + ready = PD_EVT_PPS_READY; + osMessageQueuePut(PD_handle_event_MsgQueue, &ready, 0, 1); + time_count = 0; + osTimerStop(PD_UFP_Task_timer_id); + } else if(time_count >= 10) { // 5秒超时 + ready = PD_EVT_PPS_FAILED; + osMessageQueuePut(PD_handle_event_MsgQueue, &ready, 0, 1); + time_count = 0; + osTimerStop(PD_UFP_Task_timer_id); + } +} + /** * @brief task for PD_UFP * @param argument: Not used @@ -21,31 +42,40 @@ void PDUFPTask(void *argument) { // 快充诱骗时需要另外一个端口也供电,因为快充口会有可能断电 // 可以检测status_power来判断是否有PPS供电 - PD_UI_MSG_t ui_msg; - uint8_t _working_status = 0; - // set - PD_protocol_set_power_option(&app_pd.protocol, PD_POWER_OPTION_MAX_20V); + PD_command_msg_t ui_msg; + // 创建定时器 + PD_UFP_Task_timer_id = osTimerNew(_timer_callback, osTimerPeriodic, NULL, NULL); while(1) { - if(osMessageQueueGet(PD_UI_MessageQueue, &ui_msg, NULL, 1)==osOK) { - if(ui_msg.event == PD_UI_EVT_START) { + // 非阻塞获取消息 + if(osMessageQueueGet(PD_cmd_MessageQueue, &ui_msg, NULL, 0)==osOK) { + if(!_pd_working_status && ui_msg.event == PD_CMD_START) { // FUSB CC pin connect HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET); PD_protocol_init(&app_pd.protocol); - PD_protocol_set_PPS(&app_pd.protocol, PPS_V(5.0), PPS_A(1.0), true); // 初始PPS 5V 1A + PD_protocol_set_power_option(&app_pd.protocol, PD_POWER_OPTION_MAX_5V); + PD_protocol_set_PPS(&app_pd.protocol, PPS_V(5.0), PPS_A(1.0), false); // 初始PPS 5V 1A send_power_request(); - _working_status = 1; - } else if(ui_msg.event == PD_UI_EVT_STOP) { + if(osTimerIsRunning(PD_UFP_Task_timer_id)) { + osTimerStop(PD_UFP_Task_timer_id); + } + osTimerStart(PD_UFP_Task_timer_id, 1000); // 1s + _pd_working_status = 1; - } else if(ui_msg.event == PD_UI_EVT_SET_PPS) { + } else if(_pd_working_status && ui_msg.event == PD_CMD_STOP) { + + + + } else if(_pd_working_status && ui_msg.event == PD_CMD_SET_PPS) { // 设置PPS电压电流 if(ui_msg.set_voltage > 0 && ui_msg.set_current > 0 && ui_msg.set_voltage <= 20.0 && ui_msg.set_current <= 5.0) { - PD_protocol_set_PPS(&app_pd.protocol, PPS_V(ui_msg.set_voltage), PPS_A(ui_msg.set_current), true); + PD_protocol_set_PPS(&app_pd.protocol, PPS_V(ui_msg.set_voltage), PPS_A(ui_msg.set_current), false); send_power_request(); } } } - if(_working_status) + + if(_pd_working_status) { if (fusb302_timer() || HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_RESET) { diff --git a/Power_Pico/User/Tasks/Src/user_TasksInit.c b/Power_Pico/User/Tasks/Src/user_TasksInit.c index ff261b7..907d51c 100644 --- a/Power_Pico/User/Tasks/Src/user_TasksInit.c +++ b/Power_Pico/User/Tasks/Src/user_TasksInit.c @@ -60,7 +60,7 @@ const osThreadAttr_t KeyTask_attributes = { osThreadId_t PDUFPTaskHandle; const osThreadAttr_t PDUFPTask_attributes = { .name = "PDUFPTask", - .stack_size = 128 * 2, + .stack_size = 128 * 5, .priority = (osPriority_t) osPriorityLow2, }; @@ -76,10 +76,10 @@ const osThreadAttr_t LvHandlerTask_attributes = { /* Message queues ------------------------------------------------------------*/ // Key task 中发送出的按键信息的消息队列 osMessageQueueId_t Key_MessageQueue; -// UI layer发送给PDUFPTask任务的消息队列 -osMessageQueueId_t PD_UI_MessageQueue; -// PDUFPTask任务发送给UI层的消息队列 -osMessageQueueId_t PD_Task_MessageQueue; +// UI layer发送给PDUFPTask任务的命令消息队列 +osMessageQueueId_t PD_cmd_MessageQueue; +// PDUFPTask任务发送给UI层的通知处理情况的消息队列 +osMessageQueueId_t PD_handle_event_MsgQueue; /* Private function prototypes -----------------------------------------------*/ void LvHandlerTask(void *argument); @@ -99,8 +99,8 @@ void User_Tasks_Init(void) /* add queues, ... */ Key_MessageQueue = osMessageQueueNew(4, sizeof(key_event_t), NULL); - PD_UI_MessageQueue = osMessageQueueNew(4, sizeof(PD_UI_MSG_t), NULL); - PD_Task_MessageQueue = osMessageQueueNew(4, 1, NULL); // uint8_t message + PD_cmd_MessageQueue = osMessageQueueNew(4, sizeof(PD_command_msg_t), NULL); + PD_handle_event_MsgQueue = osMessageQueueNew(4, 1, NULL); // uint8_t message /* add threads, ... */ HardwareInitTaskHandle = osThreadNew(HardwareInitTask, NULL, &HardwareInitTask_attributes);