Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 //------------------------------------ 00005 // Serial configuration (Put on hyperterminal or Putty) 00006 // 115200 bauds, 8-bit data, no parity 00007 //------------------------------------ 00008 00009 Serial serialIO(SERIAL_TX, SERIAL_RX); 00010 00011 DigitalOut led2(LED2); 00012 DigitalIn button(USER_BUTTON); 00013 00014 // Some data queue 00015 typedef struct { 00016 float voltage; /* AD result of measured voltage */ 00017 uint32_t counter; /* A counter value */ 00018 } message_t; 00019 00020 MemoryPool<message_t, 16> mpool; 00021 Queue<message_t, 16> queue; 00022 00023 // Mutex shared by threads that want's to use the serial port 00024 Mutex stdio_mutex; 00025 00026 void toogleLed(void const *args) 00027 { 00028 // Toogle led 00029 led2 = !led2; 00030 } 00031 00032 void check_button(void const *args) 00033 { 00034 while (true) { 00035 // Read button 00036 int butVal = button; 00037 00038 // To avoid using the serial port at the same time by two threads use the mutex 00039 stdio_mutex.lock(); 00040 serialIO.printf("Button state is:%d\r\n",butVal); 00041 stdio_mutex.unlock(); 00042 00043 // Put thread in wait state for 100ms 00044 Thread::wait(300); 00045 } 00046 } 00047 00048 void data_producer(void const *args) 00049 { 00050 uint32_t i = 0; 00051 while (true) { 00052 i++; // fake data update 00053 message_t *message = mpool.alloc(); 00054 message->voltage = (i * 0.1) * 33; 00055 message->counter = i; 00056 queue.put(message); 00057 Thread::wait(2000); 00058 } 00059 } 00060 00061 void data_consumer(void const *args) 00062 { 00063 while(true) { 00064 // Block until there is an event on the queue 00065 osEvent evt = queue.get(); 00066 if (evt.status == osEventMessage) { 00067 message_t *message = (message_t*)evt.value.p; 00068 00069 // Grab mutex to print data 00070 stdio_mutex.lock(); 00071 serialIO.printf("\nVoltage: %.2f V\n\r" , message->voltage); 00072 serialIO.printf("Number of cycles: %u\n\r", message->counter); 00073 stdio_mutex.unlock(); 00074 00075 mpool.free(message); 00076 } 00077 } 00078 } 00079 00080 int main() 00081 { 00082 // Configure the serial speed. 00083 serialIO.baud(115200); 00084 serialIO.printf("Learning Microcontrollers with mbed !\r\n"); 00085 00086 // Start a thread to togle the led 00087 Thread thread_2(check_button); 00088 Thread thread_3(data_producer); 00089 Thread thread_4(data_consumer); 00090 00091 // Periodically call a function 00092 RtosTimer timerTogLed(toogleLed, osTimerPeriodic); 00093 timerTogLed.start(1000); 00094 00095 // Lock forever here... 00096 Thread::wait(osWaitForever); 00097 }
Generated on Thu Jul 14 2022 01:47:01 by
1.7.2