cli.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef _CLI_H__
  2. #define _CLI_H__
  3. #include "SEGGER_RTT.h"
  4. #include "string.h"
  5. #include "stdlib.h"
  6. #include "cli_vt100.h" //¿ÉÖ±½Ó¸´ÖÆÎļþ
  7. typedef enum
  8. {
  9. NRF_CLI_VT100_COLOR_BLACK = 0,
  10. NRF_CLI_VT100_COLOR_RED,
  11. NRF_CLI_VT100_COLOR_GREEN,
  12. NRF_CLI_VT100_COLOR_YELLOW,
  13. NRF_CLI_VT100_COLOR_BLUE,
  14. NRF_CLI_VT100_COLOR_MAGENTA,
  15. NRF_CLI_VT100_COLOR_CYAN,
  16. NRF_CLI_VT100_COLOR_WHITE,
  17. } nrf_cli_vt100_color_t;
  18. typedef struct
  19. {
  20. nrf_cli_vt100_color_t col; // text color
  21. nrf_cli_vt100_color_t bgcol; // background color
  22. } nrf_cli_vt100_colors_t;
  23. typedef void (*cli_char_put_t)(unsigned char);
  24. typedef unsigned char (*cli_char_get_t)(void);
  25. typedef char (*cli_char_ok_t)(void);
  26. typedef struct
  27. {
  28. const char *_p_cmd_str;
  29. const char *_p_cmd_help_str;
  30. const void *_p_cmd_hander;
  31. } cli_section_cmd_t;
  32. typedef struct
  33. {
  34. char const *const p_name;
  35. cli_char_put_t char_put;
  36. cli_char_get_t char_get;
  37. cli_char_ok_t char_ok;
  38. unsigned char cmdbuf[256];
  39. unsigned char cmdwp;
  40. unsigned char cmdrp;
  41. unsigned char historyrp;
  42. char tempbuf[256];
  43. unsigned chartemplen;
  44. unsigned char tab_sta;
  45. nrf_cli_vt100_colors_t col;
  46. } cli_t;
  47. /**
  48. * @brief CLI command handler prototype.
  49. */
  50. typedef void (*cli_cmd_t)(cli_t const *p_cli, unsigned short argc, char **argv);
  51. #define CLI_CMD_REGISTER(_p_cmd, _p_help, _p_handler) \
  52. __attribute__((section("cli_cmd"))) cli_section_cmd_t const cli_cmd_##_p_cmd##_row = \
  53. { \
  54. ._p_cmd_str = (const char *)STRINGIFY(_p_cmd), \
  55. ._p_cmd_help_str = (const char *)_p_help, \
  56. ._p_cmd_hander = (const void *)_p_handler}
  57. #define cli_fprintf(p_cli, color, ...) \
  58. { \
  59. nrf_cli_vt100_colors_t col; \
  60. vt100_colors_store(p_cli, &col); \
  61. vt100_color_set(p_cli, color); \
  62. cli_printf(p_cli, __VA_ARGS__); \
  63. vt100_colors_restore(p_cli, &col); \
  64. }
  65. #define cli_printf(p_cli, ...) \
  66. { \
  67. p_cli->chartemplen = sprintf(p_cli->tempbuf, __VA_ARGS__); \
  68. for (int i = 0; i < p_cli->chartemplen; i++) \
  69. p_cli->char_put(p_cli->tempbuf[i]); \
  70. }
  71. #define NRF_CLI_VT100_CMD(_p_cli_, _cmd_) \
  72. { \
  73. static char const cmd[] = _cmd_; \
  74. cli_printf(_p_cli_, "%s", cmd); \
  75. }
  76. #define CLI_DEFINE(name, cli_prefix, p_char_ok, p_char_get, p_char_put) \
  77. cli_t name = { \
  78. .p_name = cli_prefix, \
  79. .char_ok = p_char_ok, \
  80. .char_get = p_char_get, \
  81. .char_put = p_char_put, \
  82. .cmdbuf[255] = '*', \
  83. .col.col = NRF_CLI_VT100_COLOR_WHITE}
  84. void cli_process(cli_t *p_cli);
  85. void cli_exe_cmd(cli_t *p_cli,char *cmd);
  86. extern cli_t clirtt;
  87. #endif