83|0

16

帖子

1

TA的资源

一粒金砂(中级)

楼主
 

【Follow me第二季第3期】任务4:设计一个类似信号发生器功能的例程 [复制链接]

  本帖最后由 左手阿飞 于 2025-1-14 12:21 编辑

最后扩展任务,设计一个类似信号发生器例程。

信号发生器需要有相应波形的原始波形数据。

在这个任务卡了很多天了,也参考了其他大佬的帖子,但是还是没搞定,做了一些二级菜单,波形出不来。

展示一下现有成果吧。

增加了一个一级菜单,

static st_menu_fn_tbl_t s_menu_items[] =
{
    {"Wave Generator"                         , wave_generator},

    {"Hello Information"                       , Hello_display_menu},
    {"Kit Information"                         , kis_display_menu},
    {"Web Server"                              , eth_emb_display_menu},
    {"Network Name Lookup"                     , eth_www_display_menu},
    {"Quad-SPI and Octo-SPI Speed Comparison"  , ext_display_menu},
    {"Cryptography and USB High speed (MSC)"   , enc_display_menu},

    {"Next Steps", ns_display_menu },
    {"", NULL }
};

 

 

在菜单中加入选项。但是在增加波形,编译就通不过了。

#define WAVE_TRIANGLE 1
#define WAVE_SQUARE 2
#define WAVE_SINE    3

uint16_t amplitude_scale;
uint16_t period_scale;
uint16_t wavetype;

// Function to get user input for customization
bool customize_wave(uint16_t* amplitude_scale, uint16_t* period_scale) {
    char choice;
    sprintf(s_print_buffer, "Use default wave settings? (y/n): ");
    print_to_console((void*)s_print_buffer);
    choice = input_from_console();

    if (choice == 'n' || choice == 'N') {
        // Prompt for amplitude scaling factor
        sprintf(s_print_buffer, "Enter amplitude scaling factor (1-10): ");
        print_to_console((void*)s_print_buffer);
        // Assume input_from_console returns ASCII digits; convert to integer
        char amp_input = input_from_console();
        *amplitude_scale = (uint16_t)(amp_input - '0');
        if (*amplitude_scale < 1 || *amplitude_scale > 10) {
            *amplitude_scale = 1; // Default value if out of range
        }

        // Prompt for period scaling factor
        sprintf(s_print_buffer, "Enter period scaling factor (1-10): ");
        print_to_console((void*)s_print_buffer);
        char per_input = input_from_console();
        *period_scale = (uint16_t)(per_input - '0');
        if (*period_scale < 1 || *period_scale > 10) {
            *period_scale = 1; // Default value if out of range
        }

        return true; // Customized settings
    }

    // Use default settings
    *amplitude_scale = 1;
    *period_scale = 1;
    return false; // Default settings
}

test_fn wave_generator(void)     // Added
{
    int8_t c = -1;
    uint16_t buffer[3]; // Increased buffer size to store wave type, amplitude, and period
    amplitude_scale = 1;
    period_scale = 1;

    // Clear the screen and move cursor to home position
    sprintf(s_print_buffer, "%s%s", gp_clear_screen, gp_cursor_home);
    print_to_console((void*)s_print_buffer);

    // Display the wave control menu
    sprintf(s_print_buffer,
            "Wave Control:\r\n"
            "1: Triangle Wave\r\n"
            "2: Square Wave\r\n"
            "3: Sine Wave\r\n"
            "r: Read from flash\r\n"
            "w: Write to flash\r\n"
            "space: Exit\r\n");
    print_to_console((void*)s_print_buffer);

    while (CONNECTION_ABORT_CRTL != c)
    {
        c = input_from_console();
        if ((MENU_EXIT_CRTL == c) || (CONNECTION_ABORT_CRTL == c))
        {
            break;
        }
        if (0 != c)
        {
            // Display the selected option
            sprintf(s_print_buffer, "Selected option: %c\r\n", c);
            print_to_console((void*)s_print_buffer);

            if ('1' == c) {
                wavetype = WAVE_TRIANGLE;          // Select Triangle Wave
            }
            else if ('2' == c) {
                wavetype = WAVE_SQUARE;            // Select Square Wave
            }
            else if ('3' == c) {
                wavetype = WAVE_SINE;              // Select Sine Wave
            }
            else if ('r' == c) {                    // Read saved parameters from flash
                readFromFlash(buffer);
                sprintf(s_print_buffer, "Read from buffer: Wave Type=%d, Amplitude Scale=%d, Period Scale=%d\r\n",
                        buffer[0], buffer[1], buffer[2]);
                print_to_console((void*)s_print_buffer);
                wavetype = buffer[0];
                amplitude_scale = buffer[1];
                period_scale = buffer[2];
                continue; // Continue to next iteration
            }
            else if ('w' == c) {                    // Write current parameters to flash
                buffer[0] = wavetype;
                buffer[1] = amplitude_scale;
                buffer[2] = period_scale;
                SaveWaveToFlash(buffer);
                sprintf(s_print_buffer, "Wave parameters saved to flash.\r\n");
                print_to_console((void*)s_print_buffer);
                continue; // Continue to next iteration
            }
            else {
                sprintf(s_print_buffer, "Invalid option selected.\r\n");
                print_to_console((void*)s_print_buffer);
                continue; // Continue to next iteration
            }

            // After selecting wave type, prompt for customization
            if (wavetype == WAVE_TRIANGLE || wavetype == WAVE_SQUARE || wavetype == WAVE_SINE) {
                bool is_custom = customize_wave(&litude_scale, &period_scale);
                if (is_custom) {
                    sprintf(s_print_buffer, "Custom settings applied: Amplitude Scale=%d, Period Scale=%d\r\n",
                            amplitude_scale, period_scale);
                }
                else {
                    sprintf(s_print_buffer, "Default settings applied: Amplitude Scale=1, Period Scale=1\r\n");
                }
                print_to_console((void*)s_print_buffer);
            }
        }
    }

    // Clear the display menu event bit
    xEventGroupClearBits(g_update_console_event, STATUS_DISPLAY_MENU_KIS);
    return (0);
}

 

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表