Stage-1 Students SoCEM / Mbed 2 deprecated Task622Solution

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 //Digital outputs
00014 DigitalOut onBoardLED(LED1);
00015 DigitalOut redLED(D7);
00016 DigitalOut yellowLED(D6);
00017 DigitalOut greenLED(D5);
00018 
00019 //Serial Interface
00020 Serial pc(USBTX, USBRX);
00021 
00022 //Digital inputs
00023 DigitalIn  onBoardSwitch(USER_BUTTON);
00024 InterruptIn  sw1(D4);
00025 DigitalIn    sw2(D3);
00026 
00027 //Threads
00028 Thread *t1;
00029 Thread *t2;
00030 
00031 //Thread ID for the Main function (CMSIS API)
00032 osThreadId tidMain;
00033 osThreadId tid1;
00034 osThreadId tid2;
00035 
00036 
00037 //Called on the falling edge of a switch
00038 void switchISR() {
00039      t1->signal_set(SWITCH1_RELEASE);    //Very short
00040 }
00041 
00042 //High priority thread
00043 void thread1( const void* arg ) 
00044 {
00045     redLED = 1;
00046     while (true) {
00047          Thread::signal_wait(SWITCH1_RELEASE);
00048          redLED = !redLED;
00049          Thread::wait(1000);
00050          redLED = !redLED;
00051          Thread::wait(1000);  
00052          t1->signal_clr(SWITCH1_RELEASE);   //Debounce
00053     }
00054 }
00055 
00056 //This thread has normal priority
00057 void thread2( const void* arg ) 
00058 {
00059     greenLED = 1; 
00060     while (true) {
00061         //Thread::wait(2000);    
00062         Thread::wait(500);          // WAIT FOR 0.5 SECONDS
00063         greenLED = !greenLED;
00064     }
00065 }
00066 
00067 
00068 // Main thread
00069 int main() {
00070     redLED    = 0;
00071     yellowLED = 0;
00072     greenLED  = 0;
00073            
00074     //Threads
00075     t1 = new Thread(&thread1, NULL, osPriorityRealtime);
00076     t2 = new Thread(&thread2, NULL, osPriorityNormal);              
00077               
00078     // Thread IDs
00079     tidMain = Thread::gettid();  
00080     tid1    = t1->gettid();
00081     tid2    = t2->gettid();
00082     
00083     //Hook up interrupt
00084     sw1.fall(switchISR);      
00085         
00086     pc.printf("Main Thread\n");
00087     while (true) {
00088         Thread::wait(osWaitForever);
00089     }
00090 
00091 }
00092 
00093