shellmatta_utils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file shellmatta_utils.h
  10. * @brief util/helper functions of shellmatta
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. /**
  14. * @addtogroup shellmatta_utils
  15. * @{
  16. */
  17. #ifndef _SHELLMATTA_UTILS_H_
  18. #define _SHELLMATTA_UTILS_H_
  19. #include "shellmatta.h"
  20. #include <stdint.h>
  21. /**
  22. * @brief returns the minimum of a and b
  23. * @param[in] a parameter a
  24. * @param[in] b parameter b
  25. */
  26. #define SHELLMATTA_MIN(a,b) (((a) > (b)) ? (b) : (a))
  27. /**
  28. * @brief returns the maximum of a and b
  29. * @param[in] a parameter a
  30. * @param[in] b parameter b
  31. */
  32. #define SHELLMATTA_MAX(a,b) (((a) < (b)) ? (b) : (a))
  33. /**
  34. * @brief calls fct with cnt bytes from buffer (to print cnt same bytes)
  35. * @param[in] buffer buffer to send (shall contain the same char)
  36. * @param[in] cnt count of bytes to send
  37. * @param[in] fct write function
  38. */
  39. #define SHELLMATTA_PRINT_BUFFER(buffer,cnt,fct) \
  40. while((cnt) > sizeof((buffer))) \
  41. { \
  42. (cnt) -= sizeof((buffer)); \
  43. (fct)((buffer), sizeof((buffer))); \
  44. } \
  45. if((cnt) != 0u) \
  46. { \
  47. (fct)((buffer), (cnt)); \
  48. }
  49. /** @brief help command which prints all shellmatta commands as table */
  50. extern const shellmatta_cmd_t helpCmd;
  51. /** @brief magic used to check if a shellmatta instance is initiated */
  52. #define SHELLMATTA_MAGIC 0x5101E110u
  53. /** @brief overwritable output buffer size */
  54. #ifndef SHELLMATTA_OUTPUT_BUFFER_SIZE
  55. #define SHELLMATTA_OUTPUT_BUFFER_SIZE 128u
  56. #endif
  57. /** @defgroup Shellmatta Help command overwrites
  58. * @{
  59. * overwritable help command parameters - the help command is built in and cannot be removed, but you can change
  60. * the command, alias and help texts by defining them in your build process
  61. * To change the settings set one of these defines:
  62. * #SHELLMATTA_HELP_COMMAND to overwrite the help command
  63. * #SHELLMATTA_HELP_ALIAS to overwrite the help alias
  64. * #SHELLMATTA_HELP_HELP_TEXT to overwrite the help text
  65. * #SHELLMATTA_HELP_USAGE_TEXT to overwrite the usage text
  66. * e.g. use _-DSHELLMATTA_HELP_ALIAS=\"?\"_ as compile option to change the alias to ?
  67. */
  68. #ifndef SHELLMATTA_HELP_COMMAND
  69. #define SHELLMATTA_HELP_COMMAND (char*)"help" /**< help command */
  70. #endif
  71. #ifndef SHELLMATTA_HELP_ALIAS
  72. #define SHELLMATTA_HELP_ALIAS (char*)"h" /**< help command alias */
  73. #endif
  74. #ifndef SHELLMATTA_HELP_HELP_TEXT
  75. #define SHELLMATTA_HELP_HELP_TEXT (char*)"Print this help text" /**< help command help text */
  76. #endif
  77. #ifndef SHELLMATTA_HELP_USAGE_TEXT
  78. #define SHELLMATTA_HELP_USAGE_TEXT (char*)"help" /**< help command usage text */
  79. #endif
  80. /**
  81. * @}
  82. */
  83. void utils_writeEcho( shellmatta_instance_t *inst,
  84. const char *data,
  85. uint32_t length);
  86. uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base);
  87. void utils_saveCursorPos(shellmatta_instance_t *inst);
  88. void utils_restoreCursorPos(shellmatta_instance_t *inst);
  89. void utils_eraseLine(shellmatta_instance_t *inst);
  90. void utils_rewindCursor(shellmatta_instance_t *inst, uint32_t length);
  91. void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length);
  92. void utils_insertChars( shellmatta_instance_t *inst,
  93. char *data,
  94. uint32_t length);
  95. void utils_removeChars( shellmatta_instance_t *inst,
  96. uint32_t length,
  97. bool backspace);
  98. void utils_clearInput(shellmatta_instance_t *inst);
  99. void utils_terminateInput(shellmatta_instance_t *inst);
  100. #endif
  101. /** @} */