shellmatta_utils.h 4.7 KB

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