Purpose: Simple application example with 2 threads communicating 5-byte buffers through a Mail event-object.

Dependencies:   IHM_V2

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Title: mbed-os-mail-5-Bytes-tx-rx
00002 // Author: Jacques-Olivier Klein - IUT de CACHAN
00003 // Date: 2018-02-10 rev. 2019-01-06
00004 
00005 #include "mbed.h"
00006 #include "IHM.h"
00007 
00008 #define MY_MAIL_BUFFER_SIZE 5
00009 #define MY_MAIL_NBER_OF_BUFFER 4
00010 
00011 typedef  struct{
00012     char message [MY_MAIL_BUFFER_SIZE] ;
00013 } Mail_Buffer_T; 
00014 
00015 IHM ihm; 
00016 
00017 DigitalOut L0 (PB_3) ;  // led L0
00018 DigitalOut L1 (PA_7) ;  // led L1
00019 DigitalOut L2 (PA_6) ;  // led L2
00020 
00021 void TxMail  ();
00022 void RxMail  ();
00023 
00024 Mail<Mail_Buffer_T, MY_MAIL_NBER_OF_BUFFER> my_mail_box;
00025 
00026 Thread Thread_TxMail;
00027 Thread Thread_RxMail(osPriorityNormal,500);
00028 
00029 Semaphore mysemaphore(0); 
00030 
00031 int main(void) 
00032 {   ihm.LCD_clear();
00033     ihm.LCD_printf("Mail-5B-tx-rx-%s %s",__DATE__,__TIME__);
00034     printf("\n\rmbed-os-mail-5-Bytes-tx-rx-%s %s\n\r",__DATE__,__TIME__);
00035     printf("DEFAULT_STACK_SIZE:%d\n\r", OS_STACK_SIZE); 
00036 
00037     Thread_TxMail.start(TxMail); 
00038     Thread_RxMail.start(RxMail); 
00039       
00040     while(1){
00041         wait(3.000);  
00042         L0=!L0;
00043         printf("M     [pid-%d]Main \n\r",osThreadGetId());
00044     }
00045 }
00046 
00047 void TxMail  (){
00048     Mail_Buffer_T * p_buffer;
00049     char c = '!';
00050     int i;
00051     while(1){
00052         p_buffer = (Mail_Buffer_T *) my_mail_box.alloc();
00053         for(i=0;i<MY_MAIL_BUFFER_SIZE-1;i++){
00054             p_buffer->message[i]=c++; 
00055             if (c=='}')c='!'; 
00056         }
00057         p_buffer->message[MY_MAIL_BUFFER_SIZE-1]=0;
00058         printf("   T  [pid-%d]TxMail:<%s>\n\r",osThreadGetId(),p_buffer->message);
00059         my_mail_box.put(p_buffer);
00060         L1 = ! L1;
00061         wait(2.000);
00062     }    
00063 }
00064 
00065 void RxMail (){
00066     Mail_Buffer_T * p_mail_buffer;
00067     Mail_Buffer_T local_buffer;
00068     while(1){
00069         osEvent evt = my_mail_box.get();
00070         L2 = ! L2;
00071         p_mail_buffer = (Mail_Buffer_T *)evt.value.p;      
00072         memcpy ( &local_buffer,p_mail_buffer, MY_MAIL_BUFFER_SIZE); 
00073         my_mail_box.free(p_mail_buffer);
00074         printf("     R[pid-%d]RxMail:<%s>\n\r",osThreadGetId(),local_buffer.message);
00075     }    
00076 }