Task 6.1.3 updated for mbed OS 5.4

Fork of Task613Solution-mbeds54 by Stage-1 Students SoCEM

Committer:
noutram
Date:
Thu Mar 30 15:01:54 2017 +0100
Revision:
2:3d65cd8d7089
Parent:
1:948bd552a2a2
Removed unnecessary header

H: Enter commit message. Lines beginning with 'HG:' are removed.

Who changed what in which revision?

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