updated for stage-3

Fork of Task613-mbedos54 by Nicholas Outram

Committer:
noutram
Date:
Fri Nov 08 10:43:32 2019 +0000
Revision:
3:168df8bbd41a
Parent:
2:feb5f54fe1ed
2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:1f4d20070876 1 #include "mbed.h"
noutram 0:1f4d20070876 2
noutram 0:1f4d20070876 3 #define RED_ON 1
noutram 0:1f4d20070876 4 #define YELLOW_ON 2
noutram 0:1f4d20070876 5 #define GREEN_ON 4
noutram 0:1f4d20070876 6 #define ALL_ON 7
noutram 0:1f4d20070876 7
noutram 0:1f4d20070876 8 //Function declarations
noutram 1:cf2510fcf09a 9 void Function1();
noutram 1:cf2510fcf09a 10 void Function2();
noutram 1:cf2510fcf09a 11 void Function3();
noutram 1:cf2510fcf09a 12 void Function4();
noutram 0:1f4d20070876 13
noutram 0:1f4d20070876 14 //I/O
noutram 0:1f4d20070876 15 DigitalOut onBoardLED(LED1);
noutram 2:feb5f54fe1ed 16 DigitalOut redLED(PE_15);
noutram 2:feb5f54fe1ed 17 DigitalOut yellowLED(PB_10);
noutram 2:feb5f54fe1ed 18 DigitalOut greenLED(PB_11);
noutram 0:1f4d20070876 19
noutram 0:1f4d20070876 20 DigitalIn onBoardSwitch(USER_BUTTON);
noutram 2:feb5f54fe1ed 21 DigitalIn SW1(PE_12);
noutram 2:feb5f54fe1ed 22 DigitalIn SW2(PE_14);
noutram 0:1f4d20070876 23
noutram 1:cf2510fcf09a 24 Thread t1;
noutram 1:cf2510fcf09a 25 Thread t2;
noutram 1:cf2510fcf09a 26 Thread t3;
noutram 1:cf2510fcf09a 27 Thread t4;
noutram 0:1f4d20070876 28
noutram 0:1f4d20070876 29 //Thread ID
noutram 0:1f4d20070876 30 osThreadId idMain;
noutram 0:1f4d20070876 31 osThreadId id1;
noutram 0:1f4d20070876 32 osThreadId id2;
noutram 0:1f4d20070876 33 osThreadId id3;
noutram 0:1f4d20070876 34 osThreadId id4;
noutram 0:1f4d20070876 35
noutram 1:cf2510fcf09a 36 void Function1()
noutram 0:1f4d20070876 37 {
noutram 0:1f4d20070876 38 while (true) {
noutram 0:1f4d20070876 39 redLED = !redLED;
noutram 0:1f4d20070876 40 if (redLED == 1) {
noutram 3:168df8bbd41a 41 t4.flags_set(RED_ON);
noutram 0:1f4d20070876 42 }
noutram 0:1f4d20070876 43
noutram 3:168df8bbd41a 44 ThisThread::sleep_for(5100);
noutram 0:1f4d20070876 45 }
noutram 0:1f4d20070876 46 }
noutram 0:1f4d20070876 47
noutram 1:cf2510fcf09a 48 void Function2()
noutram 0:1f4d20070876 49 {
noutram 0:1f4d20070876 50 while (true) {
noutram 0:1f4d20070876 51 yellowLED = !yellowLED;
noutram 0:1f4d20070876 52 if (yellowLED == 1) {
noutram 3:168df8bbd41a 53 t4.flags_set(YELLOW_ON);
noutram 0:1f4d20070876 54 }
noutram 3:168df8bbd41a 55 ThisThread::sleep_for(1900);
noutram 0:1f4d20070876 56 }
noutram 0:1f4d20070876 57 }
noutram 0:1f4d20070876 58
noutram 0:1f4d20070876 59 //Green Flashing
noutram 1:cf2510fcf09a 60 void Function3()
noutram 0:1f4d20070876 61 {
noutram 0:1f4d20070876 62 while (true) {
noutram 0:1f4d20070876 63 greenLED = !greenLED;
noutram 0:1f4d20070876 64 if (greenLED == 1) {
noutram 3:168df8bbd41a 65 t4.flags_set(GREEN_ON);
noutram 0:1f4d20070876 66 }
noutram 3:168df8bbd41a 67 ThisThread::sleep_for(700);
noutram 0:1f4d20070876 68 }
noutram 0:1f4d20070876 69 }
noutram 0:1f4d20070876 70
noutram 0:1f4d20070876 71 //This function waits for signals from all other threads
noutram 1:cf2510fcf09a 72 void Function4()
noutram 0:1f4d20070876 73 {
noutram 0:1f4d20070876 74 while (true) {
noutram 0:1f4d20070876 75
noutram 3:168df8bbd41a 76 // Thread::signal_wait(RED_ON);
noutram 3:168df8bbd41a 77 ThisThread::flags_wait_all(RED_ON);
noutram 0:1f4d20070876 78 printf("Red\n");
noutram 3:168df8bbd41a 79 ThisThread::flags_wait_all(YELLOW_ON);
noutram 0:1f4d20070876 80 printf("Yellow\n");
noutram 3:168df8bbd41a 81 ThisThread::flags_wait_all(GREEN_ON);
noutram 0:1f4d20070876 82 printf("Green\n");
noutram 0:1f4d20070876 83 //Signal main thread
noutram 0:1f4d20070876 84 osSignalSet(idMain, ALL_ON);
noutram 0:1f4d20070876 85 }
noutram 0:1f4d20070876 86 }
noutram 0:1f4d20070876 87
noutram 0:1f4d20070876 88 //Main thread
noutram 0:1f4d20070876 89 int main() {
noutram 0:1f4d20070876 90 redLED = 1;
noutram 0:1f4d20070876 91 yellowLED = 0;
noutram 0:1f4d20070876 92 greenLED = 0;
noutram 0:1f4d20070876 93
noutram 0:1f4d20070876 94 //Main thread ID
noutram 0:1f4d20070876 95 idMain = osThreadGetId(); //CMSIS RTOS call
noutram 0:1f4d20070876 96
noutram 1:cf2510fcf09a 97 //Run threads
noutram 1:cf2510fcf09a 98 t4.start(Function4);
noutram 1:cf2510fcf09a 99 t1.start(Function1);
noutram 1:cf2510fcf09a 100 t2.start(Function2);
noutram 1:cf2510fcf09a 101 t3.start(Function3);
noutram 0:1f4d20070876 102
noutram 0:1f4d20070876 103
noutram 0:1f4d20070876 104 //Thread ID
noutram 3:168df8bbd41a 105 // id1 = t1.gettid();
noutram 3:168df8bbd41a 106 // id2 = t2.gettid();
noutram 3:168df8bbd41a 107 // id3 = t3.gettid();
noutram 3:168df8bbd41a 108 // id4 = t4.gettid();
noutram 0:1f4d20070876 109
noutram 0:1f4d20070876 110 while(1) {
noutram 0:1f4d20070876 111 //Wait for the ALL_ON signal
noutram 0:1f4d20070876 112 osSignalWait(ALL_ON,osWaitForever);
noutram 0:1f4d20070876 113 printf("ALL\n");
noutram 0:1f4d20070876 114 }
noutram 0:1f4d20070876 115 }