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