Stage-1 Students SoCEM / Mbed 2 deprecated Task621

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 DELAY 200
00008 
00009 //Digital outputs
00010 DigitalOut onBoardLED(LED1);
00011 DigitalOut redLED(D7);
00012 DigitalOut yellowLED(D6);
00013 DigitalOut greenLED(D5);
00014 
00015 //Serial Interface
00016 Serial pc(USBTX, USBRX);
00017 
00018 //Digital inputs
00019 DigitalIn  onBoardSwitch(USER_BUTTON);
00020 DigitalIn  SW1(D4);
00021 DigitalIn  SW2(D3);
00022 
00023 //Thread ID for the Main function (CMSIS API)
00024 osThreadId tidMain;
00025 
00026 void thread1( const void* arg ) 
00027 {
00028     pc.printf("Entering thread 1\n");
00029     while (true) {
00030         yellowLED = 1;
00031         Thread::wait(DELAY);   
00032         yellowLED = 0;
00033         Thread::wait(DELAY);               
00034     }
00035 }
00036 
00037 //This thread has higher priority
00038 void thread2( const void* arg ) 
00039 {
00040     pc.printf("Entering thread 2\n");  
00041     while (true) {
00042         redLED = 1;
00043         if (SW1 == 1) {
00044             //wait_ms(osWaitForever);
00045             Thread::wait(osWaitForever); 
00046         } else {
00047             Thread::wait(DELAY);    
00048         }
00049         redLED = 0;
00050         Thread::wait(DELAY);               
00051     }
00052 }
00053 
00054 
00055 //Main thread
00056 int main() {
00057     redLED    = 0;
00058     yellowLED = 0;
00059     greenLED  = 0;
00060                 
00061     //Main thread ID
00062     tidMain = Thread::gettid();  
00063     
00064     //Threads
00065     Thread t1(thread1, NULL, osPriorityNormal);
00066     //Thread t2(thread2, NULL, osPriorityNormal);
00067     Thread t2(thread2, NULL, osPriorityAboveNormal);
00068     
00069     pc.printf("Main Thread\n");
00070     while (true) {
00071         Thread::wait(osWaitForever);
00072     }
00073 
00074 }
00075 
00076