Task class is an implementation of mail driven thread, for it makes to use Thread and Mail classes in mbed-rtos library easier.

Dependents:   mail_driven_task_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Task.h Source File

Task.h

00001 #ifndef TASK_H
00002 #define TASK_H
00003 
00004 #include <cstdio>
00005 #include <cstdarg>
00006 #include "mbed.h"
00007 #include "rtos.h"
00008 
00009 using namespace std;
00010 
00011 /** Log level symbol definition */
00012 typedef enum {
00013     LOG_DEBUG = 0,
00014     LOG_INFO,
00015     LOG_WARN,
00016     LOG_ERR
00017 } LogLevel;
00018 
00019 /** max length of mail queue block per task */
00020 #define MAX_MAIL_NUM 16
00021 
00022 /** max length of log string */
00023 #define LOG_BUF_SIZE 64
00024 
00025 typedef void (*Routine)(void const *argument);
00026 typedef uint32_t MessageID;
00027 typedef uint32_t TaskID;
00028 
00029 /** struct for static task configuration */
00030 struct TaskConfig {
00031     TaskConfig(
00032         const char *task_name,
00033         Routine func,
00034         osPriority task_priority = osPriorityNormal,
00035         uint32_t stack_size = DEFAULT_STACK_SIZE,
00036         unsigned char *stack_pointer = NULL
00037     ) {
00038         name         = task_name;
00039         routine      = func;
00040         priority     = task_priority;
00041         stackSize    = stack_size;
00042         stackPointer = stack_pointer;
00043     }
00044     const char *name;
00045     Routine routine;
00046     osPriority priority;
00047     uint32_t stackSize;
00048     unsigned char *stackPointer;
00049 };
00050 
00051 /** struct for mail event passing */
00052 struct MailPacket {
00053     uint32_t messageId;
00054     void *packet;
00055 };
00056 
00057 typedef Mail<MailPacket, MAX_MAIL_NUM> MailBox;
00058 
00059 /** Task class : A Thread which driven by mail event, and it have own logger. */
00060 class Task
00061 {
00062 public:
00063     /** Initialize task config table.
00064      * @param cfg array of TaskConfig
00065      * @param num_of_task length of cfg
00066      * @return void
00067      */
00068     static void init(TaskConfig *config, uint32_t num_of_task);
00069 
00070     /** Send mail to task specified by task_id with Any data.
00071      * @param task_id destination task's id
00072      * @param message_id mail id
00073      * @param packet any data pointer
00074      * @return void
00075      */
00076     static void sendMail(TaskID task_id, MessageID message_id, void *packet);
00077 
00078 
00079     static int readClock();
00080 
00081     Thread *thread;
00082 
00083     /** Wait for mail addressed to itself.
00084      * @return received MailPacket
00085      */
00086     MailPacket *waitMail();
00087 
00088     /** Delete a mail queue block. it's responsible for received task.
00089      * @param mail received MailPacket
00090      * @return void
00091      */
00092     void deleteMail(MailPacket *mail);
00093 
00094     /** Output log to task's own stream pointer.
00095      * @param lv loglevel
00096      * @param format log format
00097      * @param ... log arguments
00098      * @return void
00099      */
00100     void log(LogLevel lv, const char *format, ...);
00101 
00102     /** Logger class */
00103     class Logger
00104     {
00105     public:
00106         /** Logger constructor
00107          * @param task_name For log prefix
00108          * @param sp Stream pointer for output destination(the default is USB serial).
00109          */
00110         Logger(const char *task_name, Stream *sp = Logger::sci);
00111 
00112         /** Log level setter
00113          * @param lv log level
00114          * @return void
00115          */
00116         static void setLogLevel(LogLevel lv);
00117         static const char *lvSym[];
00118         static Stream *sci;
00119         static LogLevel lv;
00120         Stream *sp;
00121         const char *tag;
00122         char buf[LOG_BUF_SIZE];
00123     };
00124 
00125 private:
00126     /** Create a new thread with its own logger.
00127      * @param cfg TaskConfig struct pointer
00128      * @return void
00129      */
00130     Task(TaskConfig *config);
00131     ~Task();
00132 
00133     static Timer clock;
00134     static Task **taskTable;
00135     static TaskConfig *taskConfig;
00136 
00137     MailBox mbox;
00138     Logger  *logger;
00139 };
00140 
00141 #endif