查看: 2046|回复: 0

[原创] 类似 uboot 菜单的 c 应用程序

[复制链接]
  • TA的每日心情
    开心
    2024-1-16 17:48
  • 签到天数: 592 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2018-9-4 17:40:23 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 robe.zhang 于 2018-9-4 17:53 编辑

    核心代码不到 200 行,整个程序不到 400 行代码,模块化编写,很容易扩展功能,很容易扩展菜单选项,从 uboot 源码和 kernel 驱动,学的框架,先看运行效果:


    源码:
    1. /*============================ h 头文件,全局变量,结构体,枚举类型  */
    2. #include<stdio.h>
    3. #include<string.h>
    4. #include<stdlib.h>
    5. #include<unistd.h>

    6. #define CHAR_SIZE 0x80

    7. #define MENU_SIZE array_size()

    8. #define MODULE_ITEM(item_x,menu,type_x,fn_x) \
    9.         item_type item_x={ \
    10.                 .name=menu, \
    11.                 .type=type_x, \
    12.                 .fn_cmd=fn_x, \
    13.         };


    14. char input[CHAR_SIZE];
    15. int error_info;
    16. volatile int menu_color=37;
    17. volatile int menu_bg_color=40;

    18. typedef struct item{
    19.         char name[CHAR_SIZE];
    20.         int type;
    21.         int (*fn_cmd)(void);
    22. }item_type;

    23. enum type{
    24.         e_menu,
    25.         e_item,
    26.         e_max,
    27. };
    28. /* ===============================================  菜单项
    29. * 增加,删除菜单项方法:
    30. * 1,实现功能函数,并使用MODULE_ITEM声明一个菜单项
    31. *        int fn_a(void)
    32. *        {
    33. *                printf("sleeping......\n");
    34. *                sleep(1);
    35. *                printf("sucessed!\n");
    36. *                return 0;
    37. *        }
    38. *        MODULE_ITEM(item_a,"a:        delay 1 sec.",e_item,fn_a)
    39. * 2,把菜单项(item_a)添加到第 240 行的 menu 列表中。
    40. *         item_type *menu[]={
    41. *                &item_a,
    42. *                &item_b,
    43. *         }
    44. */
    45. int fn_a(void)
    46. {
    47.         printf("sleeping......\n");
    48.         sleep(1);
    49.         printf("sucessed!\n");
    50.         return 0;
    51. }
    52. MODULE_ITEM(item_a,"a:        delay 1 sec.",e_item,fn_a)

    53. int fn_b(void)
    54. {
    55.         printf("hello,world.\n");
    56.         return 0;
    57. }
    58. MODULE_ITEM(item_b,"b:        print "hello,world!"",e_item,fn_b)

    59. int fn_c(void)
    60. {
    61.         printf("beep......\n");
    62.         system("echo -n "\a" ");
    63.         return 0;
    64. }
    65. MODULE_ITEM(item_c,"c:        beep",e_item,fn_c)

    66. int fn_d(void)
    67. {
    68.         system("echo "\e[32m        hello linux!\e[0m"");
    69.         return 0;
    70. }
    71. MODULE_ITEM(item_d,"d:        print green font",e_item,fn_d)

    72. int fn_e(void)
    73. {
    74.         system("echo "\e[33m        hello linux!\e[0m"");
    75.         return 0;
    76. }
    77. MODULE_ITEM(item_e,"e:        print yellow font",e_item,fn_e)

    78. int fn_f(void)
    79. {
    80.         system("echo "\e[34m        hello linux!\e[0m"");
    81.         return 0;
    82. }
    83. MODULE_ITEM(item_f,"f:        print blue font",e_item,fn_f)

    84. int fn_g(void)
    85. {
    86.         system("echo "\e[35m        hello linux!\e[0m"");
    87.         return 0;
    88. }
    89. MODULE_ITEM(item_g,"g:        print 紫色 font",e_item,fn_g)

    90. int fn_h(void)
    91. {
    92.         system("echo "\e[36m        hello linux!\e[0m"");
    93.         return 0;
    94. }
    95. MODULE_ITEM(item_h,"h:        print 青色 font",e_item,fn_h)

    96. int fn_i(void)
    97. {
    98.         menu_color=31;
    99.         return 0;
    100. }
    101. MODULE_ITEM(item_i,"i:        change menu font color to red",e_item,fn_i)

    102. int fn_j(void)
    103. {
    104.         menu_color=32;
    105.         return 0;
    106. }
    107. MODULE_ITEM(item_j,"j:        change menu font color to green",e_item,fn_j)

    108. int fn_k(void)
    109. {
    110.         menu_color=33;
    111.         return 0;
    112. }
    113. MODULE_ITEM(item_k,"k:        change menu font color to yellow",e_item,fn_k)

    114. int fn_l(void)
    115. {
    116.         menu_color=34;
    117.         return 0;
    118. }
    119. MODULE_ITEM(item_l,"l:        change menu font color to blue",e_item,fn_l)

    120. int fn_m(void)
    121. {
    122.         menu_color=35;
    123.         return 0;
    124. }
    125. MODULE_ITEM(item_m,"m:        change menu font color to 紫色",e_item,fn_m)

    126. int fn_n(void)
    127. {
    128.         menu_color=36;
    129.         return 0;
    130. }
    131. MODULE_ITEM(item_n,"n:        change menu font color to 青色",e_item,fn_n)

    132. int fn_o(void)
    133. {
    134.         menu_color=30;
    135.         return 0;
    136. }
    137. MODULE_ITEM(item_o,"o:        change menu font color to black",e_item,fn_o)

    138. int fn_p(void)
    139. {
    140.         menu_color=37;
    141.         return 0;
    142. }
    143. MODULE_ITEM(item_p,"p:        change menu font color to default",e_item,fn_p)

    144. int fn_q(void)
    145. {
    146.         menu_bg_color=41;
    147.         return 0;
    148. }
    149. MODULE_ITEM(item_q,"q:        change menu background color to red",e_item,fn_q)

    150. int fn_r(void)
    151. {
    152.         menu_bg_color=42;
    153.         return 0;
    154. }
    155. MODULE_ITEM(item_r,"r:        change menu background color to green",e_item,fn_r)

    156. int fn_s(void)
    157. {
    158.         menu_bg_color=43;
    159.         return 0;
    160. }
    161. MODULE_ITEM(item_s,"s:        change menu background color to yellow",e_item,fn_s)

    162. int fn_t(void)
    163. {
    164.         menu_bg_color=44;
    165.         return 0;
    166. }
    167. MODULE_ITEM(item_t,"t:        change menu background color to blue",e_item,fn_t)

    168. int fn_u(void)
    169. {
    170.         menu_bg_color=45;
    171.         return 0;
    172. }
    173. MODULE_ITEM(item_u,"u:        change menu background color to 紫色",e_item,fn_u)

    174. int fn_v(void)
    175. {
    176.         menu_bg_color=46;
    177.         return 0;
    178. }
    179. MODULE_ITEM(item_v,"v:        change menu background color to 青色",e_item,fn_v)

    180. int fn_w(void)
    181. {
    182.         menu_bg_color=47;
    183.         return 0;
    184. }
    185. MODULE_ITEM(item_w,"w:        change menu background color to black",e_item,fn_w)

    186. int fn_x(void)
    187. {
    188.         exit(0);
    189.         return 0;
    190. }
    191. MODULE_ITEM(item_x,"x:        exit.",e_item,fn_x)

    192. int fn_y(void)
    193. {
    194.         menu_bg_color=40;
    195.         menu_color=37;
    196.         return 0;
    197. }
    198. MODULE_ITEM(item_y,"y:        reset menu color to default",e_item,fn_y)

    199. int fn_z(void)
    200. {
    201.         printf("this project is the demo for uboot code arch.\n");
    202.         printf("                 build by Robe 2018.9.4 \n");
    203.         return 0;
    204. }
    205. MODULE_ITEM(item_z,"z:        print project info.",e_item,fn_z)

    206. /* ==============================================菜单列表  */
    207. item_type *menu[]={
    208.         &item_a,
    209.         &item_b,
    210.         &item_c,
    211.         &item_d,
    212.         &item_e,
    213.         &item_f,
    214.         &item_g,
    215.         &item_h,
    216.         &item_i,
    217.         &item_j,
    218.         &item_k,
    219.         &item_l,
    220.         &item_m,
    221.         &item_n,
    222.         &item_o,
    223.         &item_p,
    224.         &item_q,
    225.         &item_r,
    226.         &item_s,
    227.         &item_t,
    228.         &item_u,
    229.         &item_v,
    230.         &item_w,
    231.         &item_x,
    232.         &item_y,
    233.         &item_z,
    234.         NULL,
    235. };

    236. /*=====================================================main */
    237. int array_size(void)
    238. {
    239.         int i;
    240.         for(i=0;;i++)
    241.                 if (menu[i]==NULL) break;
    242.         return i;       
    243. }

    244. int init(void)
    245. {
    246.         error_info=1;
    247.         memset(input,0,CHAR_SIZE);
    248. }

    249. int list_menu(void)
    250. {
    251.         printf("\033[0;%d;%dm",menu_color,menu_bg_color);
    252.         system("clear");
    253.         printf("============ MENU: by Robe @2018.9.4 ==========\n");
    254.         for(int i=0;i<MENU_SIZE;i++)
    255.                 {
    256.                         printf("%s \n",menu[i]->name);
    257.                 }
    258.         printf("===============================================");
    259.         printf("\033[0;0m\n");
    260. }

    261. int input_selection(void)
    262. {
    263.         printf("please select the item: ");               
    264.         scanf("%s",input);
    265. }

    266. int action(void)
    267. {
    268.         for(int i=0;i<MENU_SIZE;i++)
    269.         {
    270.                 if(input[0]==menu[i]->name[0])
    271.                 {
    272.                         menu[i]->fn_cmd();
    273.                         error_info=0;
    274.                 }       
    275.         }
    276.         printf("\n");
    277.         if (error_info==1)
    278.         {
    279.                 printf("error:        canot find your select item \n");
    280.                 system("echo "\a"");
    281.         }
    282. }

    283. int after_action(void)
    284. {
    285.         usleep(1000000);
    286. }

    287. int main(void)
    288. {
    289.         while(1)
    290.         {
    291.                 init();
    292.                 list_menu();
    293.                 input_selection();
    294.                 action();
    295.                 after_action();
    296.         }
    297.         return 0;
    298. }
    复制代码







    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-17 03:55 , Processed in 0.096372 second(s), 14 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.