Permalink
Cannot retrieve contributors at this time
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?
7148_Labs_2223/Session4/ThreadSignals/src/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (46 sloc)
1.01 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mbed.h> | |
DigitalOut led1(LED1); | |
DigitalOut led2(LED2); | |
DigitalOut led3(LED3); | |
// Interrupt Handler | |
InterruptIn theButton(BUTTON1); | |
//Create an empty thread object ready to attach a function | |
Thread thread; | |
Thread thread2; | |
//Function to deal with blinking the second LED | |
void led2_thread() { | |
//Loop forever | |
while (true) { | |
led2 = !led2; | |
thread_sleep_for(5000); | |
// Set a Signal to blink LED3 | |
thread2.flags_set(0x42); | |
} | |
} | |
void led3_thread() { | |
//Loop forever. | |
while (1){ | |
//With for the Signal before continuing | |
ThisThread::flags_wait_any(0x42); | |
for (int x=0; x<5; x++) { | |
led3 = !led3; | |
thread_sleep_for(100); | |
} | |
} | |
} | |
void buttonHandler(){ | |
// Button handler interrupt, also sets flags. | |
thread2.flags_set(0x42); | |
} | |
//Main Program | |
int main() { | |
//Bind the interupt to a function | |
theButton.rise(&buttonHandler); | |
//Attach the function to the Thread then Start it | |
thread.start(led2_thread); | |
thread2.start(led3_thread); | |
while (true) { | |
led1 = !led1; | |
thread_sleep_for(500); | |
} | |
} |