/*
 * task.hpp
 *
 *  Created on: Sep 14, 2023
 *      Author: Dario
 */

#ifndef INCLUDE_TACOS_TASK_HPP_
#define INCLUDE_TACOS_TASK_HPP_

#include <cmsis_os2.h>

#include <sta/rtos/thread.hpp>


namespace sta
{
	namespace tacos
	{
		class TacosThread : public RtosThread
		{
		public:
			/**
			 *
			 */
			TacosThread(const char* name, osPriority_t prio);

			TacosThread();

			virtual ~TacosThread();

			/**
			 * @brief Start the execution of this thread.
			 */
			void start();

			/**
			 * @brief Checks if this thread is currently running.
			 */
			bool isRunning();

			/**
			 * @brief Get the currently running instance.
			 */
			osThreadId_t getInstance();

			/**
			 *
			 */
			const char* getName() const;

			bool operator==(const TacosThread& other) const;
			bool operator<(const TacosThread& other) const;

			/**
			 * @brief A function that wraps this task's functionality in a loop. This loop will run until
			 * termination is requested.
			 */
			void loop();

			/**
			 * @brief This function is executed first when this thread is started.
			 */
			virtual void init();

			/**
			 * @brief The body of the thread's loop. Has to be implemented by the user.
			 */
			virtual void func() = 0;


			virtual void cleanup();

		private:

			/**
			 * @brief Static function to pass to RTOS to run as a thread. Calls the loop function implemented here.
			 */
			static void entry_point(void* arg);

		private:
			osThreadId_t instance_;
			osThreadAttr_t attribs_;
			bool running_;
		};
	}
}


#endif /* INCLUDE_TACOS_TASK_HPP_ */
