Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
//Create an empty thread object ready to attach a function
Thread threadOne;
Thread threadTwo;
Mutex theLock;
//Left as Exersie to turn into Libaray
class MyCounter{
public:
MyCounter(){
theCount = 0;
}
int getCount(){
return theCount;
}
void setCount(int count){
theCount = count;
}
private:
int theCount;
};
//As We dont have counter in a Libary, Define int the Global Scope here
//Rather than the top of the page (which is normal)
MyCounter theCounter;
//Increment counter by 1
void incOne(){
Timer t;
t.start();
while(1){
//DEbug LED
led2 = !led2;
//Simulate getting data fro somewhere
int count = theCounter.getCount();
t.reset();
while (t.read_ms() < 500){
//Simulate Processing
}
//And Write it back
theCounter.setCount(count+1);
t.reset();
wait(0.5); //Avoid locking the thread
}
}
void incFive(){
Timer t;
t.start();
while(1){
//Debug LED
led3 = !led3;
//Simulate getting data fro somewhere
int count = theCounter.getCount();
t.reset();
while (t.read_ms() < 500){
//Simulate Processing
}
//And Write it back
theCounter.setCount(count+10);
t.reset();
wait(0.5); //Avoid locking the thread
}
}
//Main Program
int main() {
//Attach the function to the Thread then Start it
threadOne.start(incOne);
wait(0.5);
threadTwo.start(incFive);
while (true) {
led1 = !led1;
printf("Main is %d\n", theCounter.getCount());
wait(0.75);
}
}