A serial command line interface with buffer editing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.3 KiB

3 months ago
  1. #ifndef _CMD_H_
  2. #define _CMD_H_
  3. #include <Arduino.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. class Cmd;
  8. typedef void (*CmdFunction)(Cmd *thisCmd, char *command, bool printHelp);
  9. class Cmd {
  10. protected:
  11. const char **m_commands = NULL;
  12. CmdFunction *m_functions = NULL;
  13. CmdFunction m_defaultFunction;
  14. size_t m_size = 0;
  15. size_t m_nextCmd = 0;
  16. bool m_echo = true;
  17. bool m_processing = false;
  18. const char *m_separator = " ";
  19. const char *m_line_indicator = "$ ";
  20. size_t m_buffer_size = 50;
  21. char *m_buffer = NULL;
  22. char *m_bufferTok = NULL;
  23. size_t m_buffer_read = 0;
  24. uint8_t m_buffer_reading_esc = 0;
  25. size_t m_buffer_cursor = 0;
  26. void PrintHelp();
  27. void ParseBuffer();
  28. void StartNewBuffer();
  29. public:
  30. Cmd(size_t size, CmdFunction defaultCallback);
  31. size_t GetSize();
  32. bool AddCmd(const char *cmd, CmdFunction function);
  33. const char **GetCmds();
  34. bool GetEcho();
  35. void SetEcho(bool echo);
  36. const char *GetSeparator();
  37. void SetSeparator(const char *separator);
  38. const char *GetLineIndicator();
  39. void SetLineIndicator(const char *line_indicator);
  40. size_t GetBufferSize();
  41. void SetBufferSize(size_t bufferSize);
  42. const char *GetBuffer();
  43. void PrintBuffer();
  44. char *Parse();
  45. void SendESC(const char *code);
  46. void Loop();
  47. };
  48. #endif // _CMD_H_