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@0:0c5255720d77, 2014-12-22 (annotated)
- Committer:
- mzta
- Date:
- Mon Dec 22 01:06:33 2014 +0000
- Revision:
- 0:0c5255720d77
- Child:
- 1:381a6f6263c7
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:0c5255720d77 | 55 | static void init(TaskConfig *config, uint32_t num_of_task); |
mzta | 0:0c5255720d77 | 56 | static void sendMail(TaskID task_id, MessageID message_id, void *packet); |
mzta | 0:0c5255720d77 | 57 | static int readClock(); |
mzta | 0:0c5255720d77 | 58 | |
mzta | 0:0c5255720d77 | 59 | Thread *thread; |
mzta | 0:0c5255720d77 | 60 | MailPacket *waitMail(); |
mzta | 0:0c5255720d77 | 61 | void deleteMail(MailPacket *mail); |
mzta | 0:0c5255720d77 | 62 | void log(LogLevel lv, const char *format, ...); |
mzta | 0:0c5255720d77 | 63 | |
mzta | 0:0c5255720d77 | 64 | class Logger { |
mzta | 0:0c5255720d77 | 65 | public: |
mzta | 0:0c5255720d77 | 66 | Logger(const char *task_name, Stream *sp = Logger::sci); |
mzta | 0:0c5255720d77 | 67 | static void setLogLevel(LogLevel lv); |
mzta | 0:0c5255720d77 | 68 | void setLogStream(Stream *sp); |
mzta | 0:0c5255720d77 | 69 | static const char *lvSym[]; |
mzta | 0:0c5255720d77 | 70 | static Stream *sci; |
mzta | 0:0c5255720d77 | 71 | static LogLevel lv; |
mzta | 0:0c5255720d77 | 72 | Stream *sp; |
mzta | 0:0c5255720d77 | 73 | const char *tag; |
mzta | 0:0c5255720d77 | 74 | char buf[LOG_BUF_SIZE]; |
mzta | 0:0c5255720d77 | 75 | }; |
mzta | 0:0c5255720d77 | 76 | |
mzta | 0:0c5255720d77 | 77 | private: |
mzta | 0:0c5255720d77 | 78 | Task(TaskConfig *config); |
mzta | 0:0c5255720d77 | 79 | ~Task(); |
mzta | 0:0c5255720d77 | 80 | |
mzta | 0:0c5255720d77 | 81 | static Timer clock; |
mzta | 0:0c5255720d77 | 82 | static Task **taskTable; |
mzta | 0:0c5255720d77 | 83 | static TaskConfig *taskConfig; |
mzta | 0:0c5255720d77 | 84 | |
mzta | 0:0c5255720d77 | 85 | MailBox mbox; |
mzta | 0:0c5255720d77 | 86 | Logger *logger; |
mzta | 0:0c5255720d77 | 87 | }; |
mzta | 0:0c5255720d77 | 88 | |
mzta | 0:0c5255720d77 | 89 | #endif |