shellmatta_utils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2019 - 2021 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 sums up #shellmatta_retCode_t
  35. * @param[in] ret return variable
  36. * @param[in] expression expression with the return value
  37. */
  38. #define SHELLMATTA_RET(ret, expression) (ret) = (SHELLMATTA_OK != (expression)) ? SHELLMATTA_ERROR : (ret)
  39. /**
  40. * @brief calls fct with cnt bytes from buffer (to print cnt same bytes)
  41. * @param[in] buffer buffer to send (shall contain the same char)
  42. * @param[in] cnt count of bytes to send
  43. * @param[in] fct write function
  44. */
  45. #define SHELLMATTA_PRINT_BUFFER(buffer,cnt,fct) \
  46. while((cnt) > sizeof((buffer))) \
  47. { \
  48. (cnt) -= sizeof((buffer)); \
  49. (fct)((buffer), sizeof((buffer))); \
  50. } \
  51. if((cnt) != 0u) \
  52. { \
  53. (fct)((buffer), (cnt)); \
  54. }
  55. /** @brief help command which prints all shellmatta commands as table */
  56. extern const shellmatta_cmd_t helpCmd;
  57. /** @brief magic used to check if a shellmatta instance is initiated */
  58. #define SHELLMATTA_MAGIC 0x5101E110u
  59. /** @brief overwritable output buffer size */
  60. #ifndef SHELLMATTA_OUTPUT_BUFFER_SIZE
  61. #define SHELLMATTA_OUTPUT_BUFFER_SIZE 128u
  62. #endif
  63. /** @defgroup Shellmatta Help command overwrites
  64. * @{
  65. * overwritable help command parameters - the help command is built in and cannot be removed, but you can change
  66. * the command, alias and help texts by defining them in your build process
  67. * To change the settings set one of these defines:
  68. * #SHELLMATTA_HELP_COMMAND to overwrite the help command
  69. * #SHELLMATTA_HELP_ALIAS to overwrite the help alias
  70. * #SHELLMATTA_HELP_HELP_TEXT to overwrite the help text
  71. * #SHELLMATTA_HELP_USAGE_TEXT to overwrite the usage text
  72. * e.g. use _-DSHELLMATTA_HELP_ALIAS=\"?\"_ as compile option to change the alias to ?
  73. */
  74. #ifndef SHELLMATTA_HELP_COMMAND
  75. /** \brief help command */
  76. #define SHELLMATTA_HELP_COMMAND (char*)"help"
  77. #endif
  78. #ifndef SHELLMATTA_HELP_ALIAS
  79. /** \brief help command alias */
  80. #define SHELLMATTA_HELP_ALIAS (char*)"h"
  81. #endif
  82. #ifndef SHELLMATTA_HELP_HELP_TEXT
  83. /** \brief help command help text */
  84. #define SHELLMATTA_HELP_HELP_TEXT (char*)"help [command] - print help or usage information"
  85. #endif
  86. #ifndef SHELLMATTA_HELP_USAGE_TEXT
  87. /** \brief help command usage text */
  88. #define SHELLMATTA_HELP_USAGE_TEXT (char*) "help [command]\r\n" \
  89. "\tcommand: optional command name or alias to print detailled help for"
  90. #endif
  91. /**
  92. * @}
  93. */
  94. void utils_writeEcho( shellmatta_instance_t *inst,
  95. const char *data,
  96. uint32_t length);
  97. uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base);
  98. void utils_saveCursorPos(shellmatta_instance_t *inst);
  99. void utils_restoreCursorPos(shellmatta_instance_t *inst);
  100. void utils_eraseLine(shellmatta_instance_t *inst);
  101. void utils_rewindCursor(shellmatta_instance_t *inst, uint32_t length);
  102. void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length);
  103. void utils_insertChars( shellmatta_instance_t *inst,
  104. char *data,
  105. uint32_t length);
  106. void utils_removeChars( shellmatta_instance_t *inst,
  107. uint32_t length,
  108. bool backspace);
  109. void utils_clearInput(shellmatta_instance_t *inst);
  110. void utils_terminateInput(shellmatta_instance_t *inst);
  111. #endif
  112. /** @} */