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

It makes simple to implement asynchronous processing between threads. Task class is an implementation of mail driven thread, for it makes to use Thread and Mail classes in mbed-rtos library easier.

The example of the use of this library, please refer to the following program.

Import programmail_driven_task_example

Simple example of the use of mail_driven_task library

Task.h

Committer:
mzta
Date:
2014-12-22
Revision:
0:0c5255720d77
Child:
1:381a6f6263c7

File content as of revision 0:0c5255720d77:

#ifndef TASK_H
#define TASK_H

#include <cstdio>
#include <cstdarg>
#include "mbed.h"
#include "rtos.h"

using namespace std;

typedef enum {
    LOG_DEBUG = 0,
    LOG_INFO,
    LOG_WARN,
    LOG_ERR
} LogLevel;

#define MAX_MAIL_NUM 16
#define LOG_BUF_SIZE 64

typedef void (*Routine)(void const *argument);
typedef uint32_t MessageID;
typedef uint32_t TaskID;

struct TaskConfig {
    TaskConfig(
        const char *task_name,
        Routine func,
        osPriority task_priority = osPriorityNormal,
        uint32_t stack_size = DEFAULT_STACK_SIZE,
        unsigned char *stack_pointer = NULL
    ) {
        name         = task_name;
        routine      = func;
        priority     = task_priority;
        stackSize    = stack_size;
        stackPointer = stack_pointer;
    }
    const char *name;
    Routine routine;
    osPriority priority;
    uint32_t stackSize;
    unsigned char *stackPointer;
};

struct MailPacket {
    uint32_t messageId;
    void *packet;
};

typedef Mail<MailPacket, MAX_MAIL_NUM> MailBox;

class Task {
public:
    static void init(TaskConfig *config, uint32_t num_of_task);
    static void sendMail(TaskID task_id, MessageID message_id, void *packet);
    static int readClock();

    Thread *thread;
    MailPacket *waitMail();
    void deleteMail(MailPacket *mail);
    void log(LogLevel lv, const char *format, ...);

    class Logger {
    public:
        Logger(const char *task_name, Stream *sp = Logger::sci);
        static void setLogLevel(LogLevel lv);
        void setLogStream(Stream *sp);
        static const char *lvSym[];
        static Stream *sci;
        static LogLevel lv;
        Stream *sp;        
        const char *tag;
        char buf[LOG_BUF_SIZE];
    };

private:
    Task(TaskConfig *config);
    ~Task();

    static Timer clock;
    static Task **taskTable;
    static TaskConfig *taskConfig;

    MailBox mbox;
    Logger  *logger;
};

#endif