shellmatta_utils.h 5.2 KB

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