rtos.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef KBF_RTOS_H
  2. #define KBF_RTOS_H
  3. #include <string>
  4. #include <vector>
  5. #include <freertos/FreeRTOS.h>
  6. #include <freertos/task.h>
  7. #include <freertos/event_groups.h>
  8. namespace kbf::rtos {
  9. /**
  10. * @brief RTOS task handling functions.
  11. * @deprecated use kbf::task instead
  12. */
  13. class Task {
  14. public:
  15. /**
  16. * @brief task function type
  17. */
  18. typedef void (&TaskFunc)(void *data);
  19. /**
  20. * @brief Creates an RTOS task.
  21. *
  22. * @param name name of the task; max 16 bytes, see #name
  23. * @param function function to run
  24. * @param args arguments to pass to callback; default is nullptr
  25. */
  26. Task(std::string name, TaskFunc function, void *args = nullptr);
  27. /**
  28. * @brief Destructor.
  29. *
  30. * @note will call stop() if isRunning()
  31. */
  32. ~Task();
  33. /**
  34. * @brief Deletes the RTOS task.
  35. */
  36. void stop();
  37. /**
  38. * @brief Returns whether the task is running.
  39. *
  40. * @return true if task is running; false otherwise
  41. */
  42. [[nodiscard]] bool isRunning() const;
  43. /**
  44. * @brief function to be run as a new task
  45. */
  46. TaskFunc function;
  47. /**
  48. * @brief name of the RTOS task
  49. *
  50. * @note RTOS seems to impose a 16-byte limit on task names. If the provided name is longer,
  51. * it will be silently truncated.
  52. */
  53. const std::string name;
  54. private:
  55. bool running = false;
  56. TaskHandle_t handle;
  57. void *data;
  58. /**
  59. * @brief RTOS task callback. Calls the function passed to the ctor then deletes RTOS task.
  60. *
  61. * @param pvParameters Task *
  62. */
  63. static void task(void *pvParameters);
  64. };
  65. /**
  66. * RTOS event group.
  67. */
  68. class EventGroup {
  69. public:
  70. /** @brief Tag used for logging. */
  71. static const constexpr char *const TAG = "kbf::rtos::EventGroup";
  72. /**
  73. * @brief Default constructor.
  74. */
  75. EventGroup();
  76. /**
  77. * @brief Sets a bit to 1.
  78. *
  79. * @param bit
  80. */
  81. void setBit(int bit) const;
  82. /**
  83. * @brief Sets a bit to 0.
  84. *
  85. * @param bit
  86. */
  87. void clearBit(int bit) const;
  88. /**
  89. * @brief Sets all bits to 0.
  90. */
  91. void clear() const;
  92. /**
  93. * @brief Get the value of a bit.
  94. *
  95. * @param bit
  96. * @return true if bit is 1; false otherwise
  97. */
  98. [[nodiscard]] bool getBit(int bit) const;
  99. /**
  100. * @see getBit()
  101. */
  102. bool operator[](int bit) const;
  103. /**
  104. * @brief Blocks execution until a bit is set to 1 or timeoutMs milliseconds have passed.
  105. *
  106. * @param bit bit to wait for
  107. * @param timeoutMs timeout in milliseconds
  108. * @return true if bit is set to 1; false if timeout is reached
  109. */
  110. bool waitForBit(int bit, int timeoutMs = portMAX_DELAY) const; // NOLINT(modernize-use-nodiscard)
  111. /**
  112. * @brief Blocks execution until all bits are set to 1 or timeoutMs milliseconds have passed.
  113. *
  114. * @param bits bits to wait for
  115. * @param timeoutMs timeout in milliseconds
  116. * @return true if all bits are set to 1; false if timeout is reached
  117. */
  118. bool waitForAll(const std::vector<int> &bits, // NOLINT(modernize-use-nodiscard)
  119. int timeoutMs = portMAX_DELAY) const;
  120. bool waitForAny(const std::vector<int> &bits, // NOLINT(modernize-use-nodiscard)
  121. int timeoutMs = portMAX_DELAY) const;
  122. private:
  123. EventGroupHandle_t handle;
  124. [[nodiscard]] bool wait(const std::vector<int> &bits, int timeoutMs, bool all) const;
  125. };
  126. }
  127. #endif //KBF_RTOS_H