【Follow me第二季第3期】进阶任务:示例程序中新增命令打印信息
[复制链接]
本帖最后由 一只小跳帽 于 2024-12-8 21:03 编辑
本个任务较为简单 首先打开示例 我们上个任务已经分析过主要是有一个主界面然后再进入主界面后方的对应的函数我们就可以仿照进行增加但是也要分析一下执行的过程下面首先是对主界面数组的分析
* Function Name: main_display_menu
* Description : .
* Return Value : The main menu controller.
*********************************************************************************************************************/
int8_t main_display_menu(void)
{
int8_t c = -1;
int8_t menu_limit = 0;
sprintf (s_print_buffer, "%s%s", gp_clear_screen, gp_cursor_home);
/* ignoring -Wpointer-sign is OK when treating signed char_t array as as unsigned */
print_to_console((void*)s_print_buffer);
sprintf (s_print_buffer, MODULE_NAME, FULL_NAME);
/* ignoring -Wpointer-sign is OK when treating signed char_t array as as unsigned */
print_to_console((void*)s_print_buffer);
sprintf (s_print_buffer, SUB_OPTIONS);
/* ignoring -Wpointer-sign is OK when treating signed char_t array as as unsigned */
print_to_console((void*)s_print_buffer);
for (int8_t test_active = 0; NULL != s_menu_items[test_active].p_func; test_active++ )
{
sprintf (s_print_buffer, "\r\n %d. %s", (test_active + 1), s_menu_items[menu_limit++ ].p_name);
/* ignoring -Wpointer-sign is OK when treating signed char_t array as as unsigned */
print_to_console((void*)s_print_buffer);
}
/* ignoring -Wpointer-sign is OK for a constant string */
print_to_console((uint8_t *)"\r\n");
while ((0 != c))
{
c = input_from_console ();
if (0 != c)
{
/* Cast, as compiler will assume calc is int */
c = (int8_t) (c - '0');
g_selected_menu = c;
if ((c > 0) && (c <= menu_limit))
{
s_menu_items[c - 1].p_func ();
break;
}
}
}
/* Cast, as compiler will assume calc is int */
return ((int8_t) (c - '0'));
}
/**********************************************************************************************************************
End of function main_display_menu
这里可以看到首先进行遍历数组将数组中的字符串进行打印
for (int8_t test_active = 0; NULL != s_menu_items[test_active].p_func; test_active++ )
{
sprintf (s_print_buffer, "\r\n %d. %s", (test_active + 1), s_menu_items[menu_limit++ ].p_name);
/* ignoring -Wpointer-sign is OK when treating signed char_t array as as unsigned */
print_to_console((void*)s_print_buffer);
}
再在下面进行接收执行对应数组中的方法
while ((0 != c))
{
c = input_from_console ();
if (0 != c)
{
/* Cast, as compiler will assume calc is int */
c = (int8_t) (c - '0');
g_selected_menu = c;
if ((c > 0) && (c <= menu_limit))
{
s_menu_items[c - 1].p_func ();
break;
}
}
}
下面我们增加自己的打印方法
新建一个.c和.h文件
就为user_print
/*
* user_print.c
*
* Created on: 2024年12月8日
* Author: li
*/
#include "user_print.h"
#include <stdio.h>
#include <string.h>
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "semphr.h"
#include "queue.h"
#include "task.h"
#include "common_init.h"
#include "common_data.h"
#include "common_utils.h"
static char_t s_print_buffer[BUFFER_LINE_LENGTH] = {};
void user_print()
{
int8_t c = -1;
print_to_console("请输入需要打印的数据");
print_to_console((uint8_t *)"\r\n");
while ((0 != c))
{
c = input_from_console ();
sprintf(s_print_buffer,"%c",c);
print_to_console(s_print_buffer);
}
}
这里的作用就是将输入的字符再次打印出来我们可以看到如下效果
就是将本次打印的字符串再次打印出来
本次任务就完成了这个对于我们完成最后的任务很有帮助
下面的视频为本次任务的完整视频更多的细节大家可以查看下面的视频
|