Updated for stage-3

Fork of Task613Solution-mbeds54 by Nicholas Outram

Committer:
noutram
Date:
Thu Mar 30 13:58:19 2017 +0000
Revision:
1:948bd552a2a2
Parent:
0:90e393878517
Child:
2:3d65cd8d7089
updated for mbed OS 5.4

Who changed what in which revision?

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