【Follow me第二季第3期】进阶任务——示例程序中新增命令打印信息:
[复制链接]
1、quickstart_ek工程中新建两个文件,分别为menu_dac.h/c:
2、menu_adc.c内容如下,主要是进入时,打印提示菜单,然后周期读取数据,如果是1-5打印出相应的字符,如果是0则退出此个菜单:
/*
* menu_dac.c
*
* Created on: 2024年12月14日
* Author: liujianhua
*/
#include <stdio.h>
#include <string.h>
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "semphr.h"
#include "queue.h"
#include "task.h"
#include "bsp_api.h"
#include "common_init.h"
#include "common_data.h"
#include "common_utils.h"
#include "menu_ext.h"
#define CONNECTION_ABORT_CRTL (0x00)
#define MENU_EXIT_CRTL (0x20)
#define MENU_ENTER_RESPONSE_CRTL (0x09)
#define INPUT_BUFFER (0x05)
#define CARRAGE_RETURN (0x0D)
#define BLOCK_LIMIT (0x40)
#define MODULE_NAME "\r\n DAC TEST\r\n"
#define SUB_OPTIONS ("Wave control:\r\n" \
"1: Triangle Wave\r\n" \
"2: Square Wave\r\n" \
"3: Sine Wave\r\n" \
"4: Read from flash \r\n" \
"5: Write to flash \r\n" \
"0: Exit\r\n")
/* Each thread must have a separate print buffer, to avoid clashes on task switching */
static char_t s_print_buffer[BUFFER_LINE_LENGTH] = {};
static char_t s_block_sz_str[INPUT_BUFFER] = {};
/**********************************************************************************************************************
* Function Name: ext_display_menu
* Description : .
* Return Value : .
*********************************************************************************************************************/
test_fn dac_display_menu(void)
{
int32_t c = -1;
uint32_t value = 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, g_selected_menu);
/* 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*)SUB_OPTIONS);
/* Reset input state */
c = -1;
memset(&s_block_sz_str, 0, INPUT_BUFFER);
while ((CONNECTION_ABORT_CRTL != c))
{
c = input_from_console();
value = (uint32_t)(c-'0');
switch (value)
{
case INVALID_CHARACTER:
sprintf(s_print_buffer,"\r\n> Invalid character in entry,please entry'0-5'");
print_to_console((void*)s_print_buffer);
break;
case 1:
sprintf(s_print_buffer,"\r\n> begain Triangle Wave ");
print_to_console((void*)s_print_buffer);
break;
case 2:
sprintf(s_print_buffer,"\r\n> begain Square Wave ");
print_to_console((void*)s_print_buffer);
break;
case 3:
sprintf(s_print_buffer,"\r\n> begain Sine Wave ");
print_to_console((void*)s_print_buffer);
break;
case 4:
sprintf(s_print_buffer,"\r\n> Read from flash ");
print_to_console((void*)s_print_buffer);
break;
case 5:
sprintf(s_print_buffer,"\r\n> Write to flash ");
print_to_console((void*)s_print_buffer);
break;
case 0:
sprintf(s_print_buffer,"\r\n> Exit ");
print_to_console((void*)s_print_buffer);
return 0;
break;
}
}
while ((CONNECTION_ABORT_CRTL != c))
{
if ((MENU_EXIT_CRTL == c) || (0x00 == c))
{
break;
}
c = input_from_console();
}
return (0);
}
/**********************************************************************************************************************
End of function ext_display_menu
*********************************************************************************************************************/
3、头文件则是声明菜单的接口:
#ifndef __MENU_DAC_H__
#define __MENU_DAC_H__
test_fn dac_display_menu(void);
#endif __MENU_DAC_H__
4、将头文件添加到menu_main.h中:
5、在主菜单的列表里添加一条菜单,并把dac_display_menu子菜单函数添加进回调函数:
【实现效果】
按6进入子菜单:
按下0又可以回到主菜单。
|