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

Committer:
mzta
Date:
Thu Dec 25 01:58:14 2014 +0000
Revision:
1:381a6f6263c7
Parent:
0:0c5255720d77
Child:
2:b12e4c1338db
add api document comment.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mzta 0:0c5255720d77 1 #ifndef TASK_H
mzta 0:0c5255720d77 2 #define TASK_H
mzta 0:0c5255720d77 3
mzta 0:0c5255720d77 4 #include <cstdio>
mzta 0:0c5255720d77 5 #include <cstdarg>
mzta 0:0c5255720d77 6 #include "mbed.h"
mzta 0:0c5255720d77 7 #include "rtos.h"
mzta 0:0c5255720d77 8
mzta 0:0c5255720d77 9 using namespace std;
mzta 0:0c5255720d77 10
mzta 0:0c5255720d77 11 typedef enum {
mzta 0:0c5255720d77 12 LOG_DEBUG = 0,
mzta 0:0c5255720d77 13 LOG_INFO,
mzta 0:0c5255720d77 14 LOG_WARN,
mzta 0:0c5255720d77 15 LOG_ERR
mzta 0:0c5255720d77 16 } LogLevel;
mzta 0:0c5255720d77 17
mzta 0:0c5255720d77 18 #define MAX_MAIL_NUM 16
mzta 0:0c5255720d77 19 #define LOG_BUF_SIZE 64
mzta 0:0c5255720d77 20
mzta 0:0c5255720d77 21 typedef void (*Routine)(void const *argument);
mzta 0:0c5255720d77 22 typedef uint32_t MessageID;
mzta 0:0c5255720d77 23 typedef uint32_t TaskID;
mzta 0:0c5255720d77 24
mzta 0:0c5255720d77 25 struct TaskConfig {
mzta 0:0c5255720d77 26 TaskConfig(
mzta 0:0c5255720d77 27 const char *task_name,
mzta 0:0c5255720d77 28 Routine func,
mzta 0:0c5255720d77 29 osPriority task_priority = osPriorityNormal,
mzta 0:0c5255720d77 30 uint32_t stack_size = DEFAULT_STACK_SIZE,
mzta 0:0c5255720d77 31 unsigned char *stack_pointer = NULL
mzta 0:0c5255720d77 32 ) {
mzta 0:0c5255720d77 33 name = task_name;
mzta 0:0c5255720d77 34 routine = func;
mzta 0:0c5255720d77 35 priority = task_priority;
mzta 0:0c5255720d77 36 stackSize = stack_size;
mzta 0:0c5255720d77 37 stackPointer = stack_pointer;
mzta 0:0c5255720d77 38 }
mzta 0:0c5255720d77 39 const char *name;
mzta 0:0c5255720d77 40 Routine routine;
mzta 0:0c5255720d77 41 osPriority priority;
mzta 0:0c5255720d77 42 uint32_t stackSize;
mzta 0:0c5255720d77 43 unsigned char *stackPointer;
mzta 0:0c5255720d77 44 };
mzta 0:0c5255720d77 45
mzta 0:0c5255720d77 46 struct MailPacket {
mzta 0:0c5255720d77 47 uint32_t messageId;
mzta 0:0c5255720d77 48 void *packet;
mzta 0:0c5255720d77 49 };
mzta 0:0c5255720d77 50
mzta 0:0c5255720d77 51 typedef Mail<MailPacket, MAX_MAIL_NUM> MailBox;
mzta 0:0c5255720d77 52
mzta 0:0c5255720d77 53 class Task {
mzta 0:0c5255720d77 54 public:
mzta 1:381a6f6263c7 55 /** Initialize task config table.
mzta 1:381a6f6263c7 56 @param cfg array of TaskConfig
mzta 1:381a6f6263c7 57 @param num_of_task length of cfg
mzta 1:381a6f6263c7 58 @return void
mzta 1:381a6f6263c7 59 */
mzta 0:0c5255720d77 60 static void init(TaskConfig *config, uint32_t num_of_task);
mzta 1:381a6f6263c7 61
mzta 1:381a6f6263c7 62 /** Send mail to task specified by task_id with Any data.
mzta 1:381a6f6263c7 63 @param task_id destination task's id
mzta 1:381a6f6263c7 64 @param message_id mail id
mzta 1:381a6f6263c7 65 @param packet any data pointer
mzta 1:381a6f6263c7 66 @return void
mzta 1:381a6f6263c7 67 */
mzta 0:0c5255720d77 68 static void sendMail(TaskID task_id, MessageID message_id, void *packet);
mzta 1:381a6f6263c7 69
mzta 1:381a6f6263c7 70
mzta 0:0c5255720d77 71 static int readClock();
mzta 0:0c5255720d77 72
mzta 0:0c5255720d77 73 Thread *thread;
mzta 1:381a6f6263c7 74
mzta 1:381a6f6263c7 75 /** Wait for mail addressed to itself.
mzta 1:381a6f6263c7 76 @return received MailPacket
mzta 1:381a6f6263c7 77 */
mzta 0:0c5255720d77 78 MailPacket *waitMail();
mzta 1:381a6f6263c7 79
mzta 1:381a6f6263c7 80 /** Delete a mail queue block. it's responsible for received task.
mzta 1:381a6f6263c7 81 @param mail received MailPacket
mzta 1:381a6f6263c7 82 @return void
mzta 1:381a6f6263c7 83 */
mzta 0:0c5255720d77 84 void deleteMail(MailPacket *mail);
mzta 1:381a6f6263c7 85
mzta 1:381a6f6263c7 86 /** Output log to task's own stream pointer.
mzta 1:381a6f6263c7 87 @param lv loglevel
mzta 1:381a6f6263c7 88 @param format log format
mzta 1:381a6f6263c7 89 @param ... log arguments
mzta 1:381a6f6263c7 90 @return void
mzta 1:381a6f6263c7 91 */
mzta 0:0c5255720d77 92 void log(LogLevel lv, const char *format, ...);
mzta 0:0c5255720d77 93
mzta 0:0c5255720d77 94 class Logger {
mzta 0:0c5255720d77 95 public:
mzta 1:381a6f6263c7 96 /** Logger constructor
mzta 1:381a6f6263c7 97 @param task_name For log prefix
mzta 1:381a6f6263c7 98 @param sp Stream pointer for output destination(the default is USB serial).
mzta 1:381a6f6263c7 99 */
mzta 0:0c5255720d77 100 Logger(const char *task_name, Stream *sp = Logger::sci);
mzta 1:381a6f6263c7 101
mzta 1:381a6f6263c7 102 /** Log level setter
mzta 1:381a6f6263c7 103 @param lv log level
mzta 1:381a6f6263c7 104 @return void
mzta 1:381a6f6263c7 105 */
mzta 0:0c5255720d77 106 static void setLogLevel(LogLevel lv);
mzta 0:0c5255720d77 107 void setLogStream(Stream *sp);
mzta 0:0c5255720d77 108 static const char *lvSym[];
mzta 0:0c5255720d77 109 static Stream *sci;
mzta 0:0c5255720d77 110 static LogLevel lv;
mzta 0:0c5255720d77 111 Stream *sp;
mzta 0:0c5255720d77 112 const char *tag;
mzta 0:0c5255720d77 113 char buf[LOG_BUF_SIZE];
mzta 0:0c5255720d77 114 };
mzta 0:0c5255720d77 115
mzta 0:0c5255720d77 116 private:
mzta 1:381a6f6263c7 117 /** Create a new thread with its own logger.
mzta 1:381a6f6263c7 118 @param cfg TaskConfig struct pointer
mzta 1:381a6f6263c7 119 @return void
mzta 1:381a6f6263c7 120 */
mzta 0:0c5255720d77 121 Task(TaskConfig *config);
mzta 0:0c5255720d77 122 ~Task();
mzta 0:0c5255720d77 123
mzta 0:0c5255720d77 124 static Timer clock;
mzta 0:0c5255720d77 125 static Task **taskTable;
mzta 0:0c5255720d77 126 static TaskConfig *taskConfig;
mzta 0:0c5255720d77 127
mzta 0:0c5255720d77 128 MailBox mbox;
mzta 0:0c5255720d77 129 Logger *logger;
mzta 0:0c5255720d77 130 };
mzta 0:0c5255720d77 131
mzta 0:0c5255720d77 132 #endif