University of Plymouth - Stages 1, 2 and 3 / Mbed OS Task330
Committer:
noutram
Date:
Mon Oct 23 12:25:13 2017 +0000
Revision:
2:ca251bdda621
Parent:
1:e84a51c98d75
Child:
3:a39db8aa11e8
Demonstration (crude) of blocking and how it can result in starving hardware of attention

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:397b84c74d17 1 #include "mbed.h"
noutram 0:397b84c74d17 2
noutram 0:397b84c74d17 3 #define N 1000000
noutram 0:397b84c74d17 4 #define RELEASED 0
noutram 0:397b84c74d17 5 #define PRESSED 1
noutram 0:397b84c74d17 6
noutram 0:397b84c74d17 7 //Function prototypes
noutram 2:ca251bdda621 8 void blockOnSwitch1();
noutram 2:ca251bdda621 9 void blockOnSwitch2();
noutram 0:397b84c74d17 10
noutram 0:397b84c74d17 11 //Hardware objects
noutram 0:397b84c74d17 12 DigitalOut red_led(PE_15); //CountUp is in its critical section
noutram 0:397b84c74d17 13 DigitalOut yellow_led(PB_10); //CountDown is in its critical section
noutram 0:397b84c74d17 14 DigitalOut green_led(PB_11); //counter != 0
noutram 0:397b84c74d17 15 DigitalOut onboardLED(LED1);
noutram 0:397b84c74d17 16
noutram 0:397b84c74d17 17 DigitalIn button(USER_BUTTON);
noutram 0:397b84c74d17 18 DigitalIn sw1(PE_12);
noutram 0:397b84c74d17 19 DigitalIn sw2(PE_14);
noutram 0:397b84c74d17 20
noutram 2:ca251bdda621 21 //The code below is hugely flawed and is only to
noutram 2:ca251bdda621 22 //illustrate the problem of blocking hardware
noutram 0:397b84c74d17 23 int main() {
noutram 0:397b84c74d17 24
noutram 2:ca251bdda621 25 //Light up
noutram 2:ca251bdda621 26 red_led = 1;
noutram 0:397b84c74d17 27 yellow_led = 1;
noutram 2:ca251bdda621 28 green_led = 1;
noutram 2:ca251bdda621 29 onboardLED = 1;
noutram 2:ca251bdda621 30
noutram 0:397b84c74d17 31 //Now loop forever
noutram 0:397b84c74d17 32 while(1) {
noutram 0:397b84c74d17 33
noutram 2:ca251bdda621 34 //Wait for user to press and release sw1
noutram 2:ca251bdda621 35 blockOnSwitch1();
noutram 2:ca251bdda621 36
noutram 2:ca251bdda621 37 //Wait for user to press and release sw2
noutram 2:ca251bdda621 38 blockOnSwitch2();
noutram 2:ca251bdda621 39
noutram 2:ca251bdda621 40 //Flash the yellow
noutram 2:ca251bdda621 41 yellow_led = !yellow_led;
noutram 2:ca251bdda621 42 wait(0.5);
noutram 0:397b84c74d17 43 };
noutram 0:397b84c74d17 44 }
noutram 0:397b84c74d17 45
noutram 0:397b84c74d17 46
noutram 2:ca251bdda621 47 //Thread 1 - polling sw1 and controlling the red LED
noutram 2:ca251bdda621 48 void blockOnSwitch1()
noutram 2:ca251bdda621 49 {
noutram 2:ca251bdda621 50 //Spin on sw1
noutram 2:ca251bdda621 51 while (sw1 == RELEASED) {};
noutram 2:ca251bdda621 52 //Allow short delay for switch bounce
noutram 2:ca251bdda621 53 wait(0.2);
noutram 2:ca251bdda621 54 //Spin again on sw1
noutram 2:ca251bdda621 55 while (sw1 == PRESSED) {};
noutram 2:ca251bdda621 56 //Toggle LED
noutram 2:ca251bdda621 57 red_led = !red_led;
noutram 2:ca251bdda621 58 //Again, wait for switch bounce
noutram 2:ca251bdda621 59 wait(0.2);
noutram 2:ca251bdda621 60 }
noutram 2:ca251bdda621 61
noutram 2:ca251bdda621 62 //Thread 2 - polling sw2 and controlling the green LED
noutram 2:ca251bdda621 63 void blockOnSwitch2()
noutram 2:ca251bdda621 64 {
noutram 2:ca251bdda621 65 //Spin on sw2
noutram 2:ca251bdda621 66 while (sw2 == RELEASED) {};
noutram 2:ca251bdda621 67 //Allow short delay for switch bounce
noutram 2:ca251bdda621 68 wait(0.2);
noutram 2:ca251bdda621 69 //Spin again on sw2
noutram 2:ca251bdda621 70 while (sw2 == PRESSED) {};
noutram 2:ca251bdda621 71 //Toggle LED
noutram 2:ca251bdda621 72 green_led = !green_led;
noutram 2:ca251bdda621 73 //Again, wait for switch bounce
noutram 2:ca251bdda621 74 wait(0.2);
noutram 2:ca251bdda621 75 }