Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 2:ca251bdda621
- Parent:
- 1:e84a51c98d75
- Child:
- 3:a39db8aa11e8
--- a/main.cpp Mon Oct 23 09:47:16 2017 +0000
+++ b/main.cpp Mon Oct 23 12:25:13 2017 +0000
@@ -1,13 +1,12 @@
#include "mbed.h"
-#include "SWPoll.hpp"
#define N 1000000
#define RELEASED 0
#define PRESSED 1
//Function prototypes
-void task1();
-void task2();
+void blockOnSwitch1();
+void blockOnSwitch2();
//Hardware objects
DigitalOut red_led(PE_15); //CountUp is in its critical section
@@ -19,29 +18,58 @@
DigitalIn sw1(PE_12);
DigitalIn sw2(PE_14);
-SWPoll switch1(sw1, red_led);
-SWPoll switch2(sw2, green_led);
-
-//LOOK AT SWPoll.hpp for the definition of the SWPoll class
-
+//The code below is hugely flawed and is only to
+//illustrate the problem of blocking hardware
int main() {
- //Main uses a Timer
+ //Light up
+ red_led = 1;
yellow_led = 1;
- Timer t;
-
+ green_led = 1;
+ onboardLED = 1;
+
//Now loop forever
- t.start();
while(1) {
- //Flash the yellow on the "main thread"
- if (t.read_ms() >= 500) {
- yellow_led = !yellow_led;
- t.reset();
- }
- switch1.poll();
- switch2.poll();
+ //Wait for user to press and release sw1
+ blockOnSwitch1();
+
+ //Wait for user to press and release sw2
+ blockOnSwitch2();
+
+ //Flash the yellow
+ yellow_led = !yellow_led;
+ wait(0.5);
};
}
+//Thread 1 - polling sw1 and controlling the red LED
+void blockOnSwitch1()
+{
+ //Spin on sw1
+ while (sw1 == RELEASED) {};
+ //Allow short delay for switch bounce
+ wait(0.2);
+ //Spin again on sw1
+ while (sw1 == PRESSED) {};
+ //Toggle LED
+ red_led = !red_led;
+ //Again, wait for switch bounce
+ wait(0.2);
+}
+
+//Thread 2 - polling sw2 and controlling the green LED
+void blockOnSwitch2()
+{
+ //Spin on sw2
+ while (sw2 == RELEASED) {};
+ //Allow short delay for switch bounce
+ wait(0.2);
+ //Spin again on sw2
+ while (sw2 == PRESSED) {};
+ //Toggle LED
+ green_led = !green_led;
+ //Again, wait for switch bounce
+ wait(0.2);
+}