Queues
Dependencies: ELEC350-Practicals-FZ429
Fork of Task631-mbedos54 by
main.cpp
00001 #include "mbed.h" 00002 #include "sample_hardware.hpp" 00003 00004 #include "string.h" 00005 #include <stdio.h> 00006 #include <ctype.h> 00007 00008 #define SWITCH1_RELEASE 1 00009 00010 void thread1(); 00011 void thread2(); 00012 void switchISR(); 00013 00014 //Threads 00015 Thread t1; 00016 00017 //Queues - "A message can be a integer or pointer value to a certain type T that is sent to a thread or interrupt service routine." 00018 Queue<uint32_t, 5> queue; 00019 00020 // Call this on precise intervals 00021 void adcISR() { 00022 00023 //Option to starve the queue 00024 if (SW2 == 1) return; 00025 00026 //Read sample - make a copy 00027 uint32_t sample = (uint32_t)(4095*adcIn.read()); 00028 00029 //Write to queue 00030 osStatus stat = queue.put((uint32_t*)sample); 00031 00032 //Check if succesful 00033 if (stat == osErrorResource) { 00034 redLED = 1; 00035 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); 00036 } 00037 00038 } 00039 00040 //Normal priority thread (consumer) 00041 void thread1() 00042 { 00043 while (true) { 00044 //Read queue - block (with timeout) 00045 osEvent evt = queue.get(5000); //With timeout 00046 00047 //Check status of get() 00048 switch (evt.status) { 00049 case osEventMessage: 00050 //Normal status 00051 printf("value = %d\n\r", evt.value.v); 00052 greenLED = !greenLED; 00053 break; 00054 case osEventTimeout: 00055 //Timeout 00056 printf("queue.get() returned %02x status (timeout)\n\r", evt.status); 00057 break; 00058 default: 00059 //All other errors (see cmsis_os.h for meaning of error code) 00060 printf("queue.get() returned %02x status\n\r", evt.status); 00061 break; 00062 } 00063 00064 //Block up consumer if switch is held down 00065 //Will fill the queue if held long enough 00066 while (SW1 == 1); 00067 00068 } //end while 00069 } 00070 00071 00072 // Main thread 00073 int main() { 00074 post(); 00075 00076 //Start message 00077 printf("Welcome\n"); 00078 00079 //Hook up timer interrupt 00080 Ticker timer; 00081 timer.attach(&adcISR, 1.0); 00082 00083 //Threads 00084 t1.start(thread1); 00085 00086 printf("Main Thread\n"); 00087 while (true) { 00088 Thread::wait(5000); 00089 puts("Main Thread Alive"); 00090 } 00091 } 00092 00093
Generated on Tue Jul 12 2022 18:57:18 by
1.7.2
