123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #ifndef KBF_RTOS_H
- #define KBF_RTOS_H
- #include <string>
- #include <vector>
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- #include <freertos/event_groups.h>
- namespace kbf::rtos {
- /**
- * @brief RTOS task handling functions.
- * @deprecated use kbf::task instead
- */
- 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(std::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 std::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);
- };
- /**
- * RTOS event group.
- */
- class EventGroup {
- public:
- /** @brief Tag used for logging. */
- static const constexpr char *const TAG = "kbf::rtos::EventGroup";
- /**
- * @brief Default constructor.
- */
- EventGroup();
- /**
- * @brief Sets a bit to 1.
- *
- * @param bit
- */
- void setBit(int bit) const;
- /**
- * @brief Sets a bit to 0.
- *
- * @param bit
- */
- void clearBit(int bit) const;
- /**
- * @brief Sets all bits to 0.
- */
- void clear() const;
- /**
- * @brief Get the value of a bit.
- *
- * @param bit
- * @return true if bit is 1; false otherwise
- */
- [[nodiscard]] bool getBit(int bit) const;
- /**
- * @see getBit()
- */
- bool operator[](int bit) const;
- /**
- * @brief Blocks execution until a bit is set to 1 or timeoutMs milliseconds have passed.
- *
- * @param bit bit to wait for
- * @param timeoutMs timeout in milliseconds
- * @return true if bit is set to 1; false if timeout is reached
- */
- bool waitForBit(int bit, int timeoutMs = portMAX_DELAY) const; // NOLINT(modernize-use-nodiscard)
- /**
- * @brief Blocks execution until all bits are set to 1 or timeoutMs milliseconds have passed.
- *
- * @param bits bits to wait for
- * @param timeoutMs timeout in milliseconds
- * @return true if all bits are set to 1; false if timeout is reached
- */
- bool waitForAll(const std::vector<int> &bits, // NOLINT(modernize-use-nodiscard)
- int timeoutMs = portMAX_DELAY) const;
- bool waitForAny(const std::vector<int> &bits, // NOLINT(modernize-use-nodiscard)
- int timeoutMs = portMAX_DELAY) const;
- private:
- EventGroupHandle_t handle;
- [[nodiscard]] bool wait(const std::vector<int> &bits, int timeoutMs, bool all) const;
- };
- }
- #endif //KBF_RTOS_H
|