Task651 Event Queue

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

main.cpp

Committer:
noutram
Date:
2017-11-23
Revision:
11:f8dde59c750c
Parent:
10:dd9aa289d656

File content as of revision 11:f8dde59c750c:

#include "mbed.h"
#include <iostream>
#include "sample_hardware.hpp"
#include "mbed_events.h"
void do_rising();
void enableRising();
 
DigitalOut led(LED1);
InterruptIn btn(USER_BUTTON);

// creates a queue with the default size
EventQueue queue;
unsigned int buttonCount = 0;

    
int main() {
    
    //Hook up isr
    enableRising();

    // events are simple callbacks
    queue.call(printf, "called immediately on dispatch\n");
    queue.call_in(2000, printf, "called after 2 seconds\n");
    queue.call_every(1000, printf, "called every 1 seconds\n");

    // events are executed by the dispatch method
    queue.dispatch();
    
    //Unreachable code
    printf("This should never appear!\n");
}

//ISR
void do_rising() {
    //ISR response time (fast)
    led = !led;
    
    //Turn off this interrupt (fast)
    btn.rise(NULL);

    //Post the following into the main thread event queue

    //Turn back on after a delay
    queue.call_in(1000, enableRising);  
    
    //Dispatch to the main thread queue
    queue.call(printf, "Button depressed %5d times\n", ++buttonCount);
}

//Enable the rising edge
void enableRising() {
    btn.rise(do_rising);    
}