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 Task633 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 //Mail queue 00051 Mail<message_t, 16> mail_box; 00052 00053 00054 // Call this on precise intervals 00055 void adcISR() { 00056 00057 00058 //Read sample - make a copy 00059 float sample = adcIn; 00060 //Grab switch state 00061 uint32_t switch1State = sw1; 00062 uint32_t switch2State = sw2; 00063 00064 //Allocate a block from the memory pool 00065 message_t *message = mail_box.alloc(); 00066 if (message == NULL) { 00067 //Out of memory 00068 printf("Out of memory\n\r"); 00069 redLED = 1; 00070 return; 00071 } 00072 00073 //Fill in the data 00074 message->adcValue = sample; 00075 message->sw1State = switch1State; 00076 message->sw2State = switch2State; 00077 00078 //Write to queue 00079 osStatus stat = mail_box.put(message); //Note we are sending the "pointer" 00080 00081 //Check if succesful 00082 if (stat == osErrorResource) { 00083 redLED = 1; 00084 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); 00085 mail_box.free(message); 00086 return; 00087 } 00088 00089 00090 } 00091 00092 //Normal priority thread (consumer) 00093 void thread1() 00094 { 00095 while (true) { 00096 //Block on the queue 00097 osEvent evt = mail_box.get(); 00098 00099 //Check status 00100 if (evt.status == osEventMail) { 00101 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address) 00102 //Make a copy 00103 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State); 00104 //We are done with this, so give back the memory to the pool 00105 mail_box.free(pMessage); 00106 00107 //Echo to the terminal 00108 printf("ADC Value: %.2f\t", msg.adcValue); 00109 printf("SW1: %u\t", msg.sw1State); 00110 printf("SW2: %u\n\r", msg.sw2State); 00111 } else { 00112 printf("ERROR: %x\n\r", evt.status); 00113 } 00114 00115 } //end while 00116 } 00117 00118 00119 // Main thread 00120 int main() { 00121 redLED = 0; 00122 yellowLED = 0; 00123 greenLED = 0; 00124 00125 //Start message 00126 printf("Welcome\n"); 00127 00128 //Hook up timer interrupt 00129 Ticker timer; 00130 timer.attach(&adcISR, 0.1); 00131 00132 //Threads 00133 t1 = new Thread(); 00134 t1->start(thread1); 00135 00136 printf("Main Thread\n"); 00137 while (true) { 00138 Thread::wait(5000); 00139 puts("Main Thread Alive"); 00140 } 00141 } 00142 00143
Generated on Thu Jul 14 2022 20:54:44 by
1.7.2