Skip to content

Instantly share code, notes, and snippets.

@idt12312
Created February 1, 2017 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idt12312/b7f8379ad2b0b7c72079e3bb6723df12 to your computer and use it in GitHub Desktop.
Save idt12312/b7f8379ad2b0b7c72079e3bb6723df12 to your computer and use it in GitHub Desktop.
FreeRTOSのタスクを楽に扱うクラス
#ifndef TASK_TASKBASE_H_
#define TASK_TASKBASE_H_
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
class TaskBase {
public:
// デフォルトコンストラクタを禁止
// 継承先のコンストラクタでTaskBaseの初期化を強制するため
TaskBase() = delete;
TaskBase(const char *_name, int _priority, uint32_t _stack_size=configMINIMAL_STACK_SIZE)
:name(_name), priority(_priority), stack_size(_stack_size)
{
}
virtual ~TaskBase()
{
delete_task();
}
void create_task()
{
xTaskCreate(task_entry_point, name, stack_size, this, priority, &handle);
}
void delete_task()
{
vTaskDelete(handle);
}
protected:
xTaskHandle handle = 0;
const char *name;
int priority;
uint32_t stack_size;
virtual void task() = 0;
static void task_entry_point(void* task_instance)
{
static_cast<TaskBase*>(task_instance)->task();
}
};
#endif /* TASK_TASKBASE_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment