1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #ifndef KBF_TASK_H
- #define KBF_TASK_H
- #include <string>
- using std::string;
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- namespace kbf {
- /**
- * @brief RTOS task handling functions.
- */
- class Task {
- public:
- /**
- * @brief task function type
- */
- typedef void (&TaskFunc)(void *data);
- /**
- * @brief Creates an RTOS task.
- *
- * @param name name of the task; max 16 bytes, see #name
- * @param function function to run
- * @param args arguments to pass to callback; default is nullptr
- */
- Task(string name, TaskFunc function, void *args = nullptr);
- /**
- * @brief Destructor.
- *
- * @note will call stop() if isRunning()
- */
- ~Task();
- /**
- * @brief Deletes the RTOS task.
- */
- void stop();
- /**
- * @brief Returns whether the task is running.
- *
- * @return true if task is running; false otherwise
- */
- [[nodiscard]] bool isRunning() const;
- /**
- * @brief function to be run as a new task
- */
- TaskFunc function;
- /**
- * @brief name of the RTOS task
- *
- * @note RTOS seems to impose a 16-byte limit on task names. If the provided name is longer,
- * it will be silently truncated.
- */
- const string name;
- private:
- bool running = false;
- TaskHandle_t handle;
- void *data;
- /**
- * @brief RTOS task callback. Calls the function passed to the ctor then deletes RTOS task.
- *
- * @param pvParameters Task *
- */
- static void task(void *pvParameters);
- };
- }
- #endif //KBF_TASK_H
|