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.
Fork of Task632-mbedos54 by
main.cpp
00001 #include "mbed.h" 00002 #include "string.h" 00003 #include <stdio.h> 00004 #include <ctype.h> 00005 00006 #define SWITCH1_RELEASE 1 00007 00008 void thread1(); 00009 void thread2(); 00010 void switchISR(); 00011 00012 //Analogue inputs 00013 AnalogIn adcIn(A0); 00014 00015 //Digital outputs 00016 DigitalOut onBoardLED(LED1); 00017 DigitalOut redLED(D7); 00018 DigitalOut yellowLED(D6); 00019 DigitalOut greenLED(D5); 00020 00021 //Digital inputs 00022 DigitalIn onBoardSwitch(USER_BUTTON); 00023 DigitalIn sw1(D4); //CONSIDER CHANGING THIS TO AN INTERRUPT 00024 DigitalIn sw2(D3); 00025 00026 //Threads 00027 Thread *t1; 00028 00029 //Class type 00030 class message_t { 00031 public: 00032 float adcValue; 00033 int sw1State; 00034 int sw2State; 00035 00036 //Constructor 00037 message_t(float f, int s1, int s2) { 00038 adcValue = f; 00039 sw1State = s1; 00040 sw2State = s2; 00041 } 00042 }; 00043 00044 //Memory Pool - with capacity for 16 message_t types 00045 MemoryPool<message_t, 16> mpool; 00046 00047 //Message queue - matched to the memory pool 00048 Queue<message_t, 16> queue; 00049 00050 // Call this on precise intervals 00051 void adcISR() { 00052 00053 //Read sample - make a copy 00054 float sample = adcIn; 00055 //Grab switch state 00056 uint32_t switch1State = sw1; 00057 uint32_t switch2State = sw2; 00058 00059 //Allocate a block from the memory pool 00060 message_t *message = mpool.alloc(); 00061 if (message == NULL) { 00062 //Out of memory 00063 redLED = 1; 00064 return; 00065 } 00066 00067 //Fill in the data 00068 message->adcValue = sample; 00069 message->sw1State = switch1State; 00070 message->sw2State = switch2State; 00071 00072 //Write to queue 00073 osStatus stat = queue.put(message); //Note we are sending the "pointer" 00074 00075 //Check if succesful 00076 if (stat == osErrorResource) { 00077 redLED = 1; 00078 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); 00079 mpool.free(message); 00080 return; 00081 } 00082 } 00083 00084 //Normal priority thread (consumer) 00085 void thread1() 00086 { 00087 while (true) { 00088 //Block on the queue 00089 osEvent evt = queue.get(); 00090 00091 //Check status 00092 if (evt.status == osEventMessage) { 00093 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address) 00094 //Make a copy 00095 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State); 00096 //We are done with this, so give back the memory to the pool 00097 mpool.free(pMessage); 00098 00099 //Echo to the terminal 00100 printf("ADC Value: %.2f\t", msg.adcValue); 00101 printf("SW1: %u\t", msg.sw1State); 00102 printf("SW2: %u\n\r", msg.sw2State); 00103 } 00104 00105 00106 } //end while 00107 } 00108 00109 00110 // Main thread 00111 int main() { 00112 redLED = 0; 00113 yellowLED = 0; 00114 greenLED = 0; 00115 00116 //Start message 00117 printf("Welcome\n"); 00118 00119 //Hook up timer interrupt 00120 Ticker timer; 00121 timer.attach(&adcISR, 0.1); 00122 00123 //Threads 00124 t1 = new Thread(); 00125 t1->start(thread1); 00126 00127 printf("Main Thread\n"); 00128 while (true) { 00129 Thread::wait(5000); 00130 puts("Main Thread Alive"); 00131 } 00132 } 00133 00134
Generated on Sat Jul 16 2022 20:35:38 by
1.7.2