Simple example of the use of mail_driven_task library
Dependencies: mail-driven-task mbed-rtos mbed-src
Revision 0:7e8dc6570c55, committed 2014-12-22
- Comitter:
- mzta
- Date:
- Mon Dec 22 04:02:35 2014 +0000
- Commit message:
- Initial commit.
Changed in this revision
diff -r 000000000000 -r 7e8dc6570c55 adc_task/adc_task.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/adc_task/adc_task.cpp Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,44 @@ +#include "Task.h" +#include "taskdef.h" +#include "adc_task.h" + +AnalogIn ain0(A0); +AnalogIn ain1(A1); + +void adcTaskMain(void const *argument) +{ + MailPacket *mail; + Task *self = (Task *)argument; + + while (true) { + mail = self->waitMail(); + switch(mail->messageId) { + case TMSG_ADC_DATA_REQ: { + self->log(LOG_INFO, "recieve DATA_REQ"); + AdcData *data = new AdcData; + data->a0 = ain0.read(); + data->a1 = ain1.read(); + Task::sendMail(TID_KICK, TMSG_ADC_DATA_NOTIFY, data); + break; + } + case TMSG_ADC_AVEDATA_REQ: { + int *cnt = (int *)mail->packet; + float a0 = 0, a1 = 0; + self->log(LOG_INFO, "recieve AVEDATA_REQ <count=%d>", *cnt); + for (int i = 0; i < *cnt; i++) { + a0 += ain0.read(); a1 += ain1.read(); + Thread::wait(1000); + } + AdcData *data = new AdcData; + data->a0 = a0 / *cnt; + data->a1 = a1 / *cnt; + Task::sendMail(TID_KICK, TMSG_ADC_AVEDATA_NOTIFY, data); + delete cnt, a0, a1; + break; + } + default: + break; + } + self->deleteMail(mail); + } +}
diff -r 000000000000 -r 7e8dc6570c55 adc_task/adc_task.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/adc_task/adc_task.h Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,9 @@ +#ifndef ADC_TASK_H +#define ADC_TASK_H + +struct AdcData { + float a0; + float a1; +}; + +#endif
diff -r 000000000000 -r 7e8dc6570c55 kick_task/kick_task.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kick_task/kick_task.cpp Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,47 @@ +#include "Task.h" +#include "taskdef.h" +#include "adc_task.h" + +void kickTaskMain(void const *argument) +{ + MailPacket *mail; + Task *self = (Task *)argument; + + while (true) { + mail = self->waitMail(); + switch(mail->messageId) { + case TMSG_ADC_DATA_NOTIFY: + case TMSG_ADC_AVEDATA_NOTIFY: + { + AdcData *data = (AdcData *)mail->packet; + if (mail->messageId == TMSG_ADC_DATA_NOTIFY) { + self->log(LOG_INFO, + "recieve ADC_DATA_NOTIFY <a0=%.2f, a1=%.2f>", + data->a0, data->a1); + } else { + self->log(LOG_INFO, + "recieve ADC_AVEDATA_NOTIFY <a0=%.2f, a1=%.2f>", + data->a0, data->a1); + Task::sendMail(TID_LED, TMSG_LED_STOP_BLINK, NULL); + } + delete data; + break; + } + case TMSG_KICK_TEST1: + self->log(LOG_INFO, "recieve KICK_TEST1"); + Task::sendMail(TID_ADC, TMSG_ADC_DATA_REQ, NULL); + break; + case TMSG_KICK_TEST2: { + self->log(LOG_INFO, "recieve KICK_TEST2"); + int *i = new int; + *i = 10; + Task::sendMail(TID_LED, TMSG_LED_START_BLINK, NULL); + Task::sendMail(TID_ADC, TMSG_ADC_AVEDATA_REQ, i); + break; + } + default: + break; + } + self->deleteMail(mail); + } +} \ No newline at end of file
diff -r 000000000000 -r 7e8dc6570c55 led_task/led_task.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led_task/led_task.cpp Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,50 @@ +#include "Task.h" +#include "taskdef.h" + +DigitalOut led1(LED1); +RtosTimer *blinkTimer; +extern void ledToggle(void const *n); + +inline void ledTaskInit() +{ + led1 = 1; + blinkTimer = new RtosTimer(ledToggle, osTimerPeriodic, NULL); +} + +void ledToggle(void const *n) +{ + led1 = !led1; +} + +void ledTaskMain(void const *argument) +{ + Task *self = (Task *)argument; + + ledTaskInit(); + + while (true) { + MailPacket *mail = self->waitMail(); + switch(mail->messageId) { + case TMSG_LED_TURNON: + self->log(LOG_DEBUG, "TURNON"); + led1 = 0; + break; + case TMSG_LED_TURNOFF: + self->log(LOG_DEBUG, "TURNOFF"); + led1 = 1; + break; + case TMSG_LED_START_BLINK: + self->log(LOG_DEBUG, "START_BLINK"); + blinkTimer->start(1000); + break; + case TMSG_LED_STOP_BLINK: + self->log(LOG_DEBUG, "STOP_BLINK"); + blinkTimer->stop(); + Task::sendMail(TID_LED, TMSG_LED_TURNOFF, NULL); + break; + default: + break; + } + self->deleteMail(mail); + } +} \ No newline at end of file
diff -r 000000000000 -r 7e8dc6570c55 mail-driven-task.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mail-driven-task.lib Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mzta/code/mail-driven-task/#0c5255720d77
diff -r 000000000000 -r 7e8dc6570c55 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,26 @@ +#include "mbed.h" +#include "Task.h" +#include "taskdef.h" + +TaskConfig taskConfig[NUM_OF_TASKS] = { + TaskConfig(TNAME_LED, ledTaskMain), + TaskConfig(TNAME_ADC, adcTaskMain, osPriorityAboveNormal), + TaskConfig(TNAME_KICK, kickTaskMain, osPriorityBelowNormal) +}; + +int main() +{ + Task::init(taskConfig, NUM_OF_TASKS); + Task::Logger::setLogLevel(LOG_INFO); + + Task::sendMail(TID_KICK, TMSG_KICK_TEST1, NULL); + Thread::wait(3000); + + Task::sendMail(TID_KICK, TMSG_KICK_TEST2, NULL); + Thread::wait(2000); + + Task::sendMail(TID_KICK, TMSG_KICK_TEST1, NULL); + + /** dummy loop */ + while (true) {} +}
diff -r 000000000000 -r 7e8dc6570c55 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#13a25134ac60
diff -r 000000000000 -r 7e8dc6570c55 mbed-src.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-src.lib Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-src/#8a0b45cd594f
diff -r 000000000000 -r 7e8dc6570c55 taskdef.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taskdef.h Mon Dec 22 04:02:35 2014 +0000 @@ -0,0 +1,46 @@ +#ifndef TASKDEF_H +#define TASKDEF_H + +/** + * Task ID definition + */ +enum { + TID_LED = 0, + TID_ADC, + TID_KICK, + NUM_OF_TASKS +}; + +/** + * Task Name definition + */ +#define TNAME_LED "LedTask" +#define TNAME_ADC "AdcTask" +#define TNAME_KICK "kickTask" + +/** + * Message ID definition + */ +#define MSG(t,m) (t<<16 | m) + +#define TMSG_LED_TURNON MSG(TID_LED, 0x01) +#define TMSG_LED_TURNOFF MSG(TID_LED, 0x02) +#define TMSG_LED_START_BLINK MSG(TID_LED, 0x03) +#define TMSG_LED_STOP_BLINK MSG(TID_LED, 0x04) + +#define TMSG_ADC_DATA_REQ MSG(TID_ADC, 0x01) +#define TMSG_ADC_AVEDATA_REQ MSG(TID_ADC, 0x02) +#define TMSG_ADC_DATA_NOTIFY MSG(TID_ADC, 0x03) +#define TMSG_ADC_AVEDATA_NOTIFY MSG(TID_ADC, 0x04) + +#define TMSG_KICK_TEST1 MSG(TID_KICK, 0x01) +#define TMSG_KICK_TEST2 MSG(TID_KICK, 0x02) + +/** + * Task Main Routine + */ +extern void ledTaskMain(void const *argument); +extern void adcTaskMain(void const *argument); +extern void kickTaskMain(void const *argument); + +#endif