task.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef KBF_TASK_H
  2. #define KBF_TASK_H
  3. #include <string>
  4. using std::string;
  5. #include <freertos/FreeRTOS.h>
  6. #include <freertos/task.h>
  7. namespace kbf {
  8. /**
  9. * @brief RTOS task handling functions.
  10. */
  11. class Task {
  12. public:
  13. /**
  14. * @brief task function type
  15. */
  16. typedef void (&TaskFunc)(void *data);
  17. /**
  18. * @brief Creates an RTOS task.
  19. *
  20. * @param name name of the task; max 16 bytes, see #name
  21. * @param function function to run
  22. * @param args arguments to pass to callback; default is nullptr
  23. */
  24. Task(string name, TaskFunc function, void *args = nullptr);
  25. /**
  26. * @brief Destructor.
  27. *
  28. * @note will call stop() if isRunning()
  29. */
  30. ~Task();
  31. /**
  32. * @brief Deletes the RTOS task.
  33. */
  34. void stop();
  35. /**
  36. * @brief Returns whether the task is running.
  37. *
  38. * @return true if task is running; false otherwise
  39. */
  40. [[nodiscard]] bool isRunning() const;
  41. /**
  42. * @brief function to be run as a new task
  43. */
  44. TaskFunc function;
  45. /**
  46. * @brief name of the RTOS task
  47. *
  48. * @note RTOS seems to impose a 16-byte limit on task names. If the provided name is longer,
  49. * it will be silently truncated.
  50. */
  51. const string name;
  52. private:
  53. bool running = false;
  54. TaskHandle_t handle;
  55. void *data;
  56. /**
  57. * @brief RTOS task callback. Calls the function passed to the ctor then deletes RTOS task.
  58. *
  59. * @param pvParameters Task *
  60. */
  61. static void task(void *pvParameters);
  62. };
  63. }
  64. #endif //KBF_TASK_H