Demonstration of EventQueues

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

main.cpp

Committer:
noutram
Date:
2017-11-20
Revision:
10:3ab2495f24d9
Parent:
9:c46e831f8e4a
Child:
11:67cb33bc247e

File content as of revision 10:3ab2495f24d9:

#include "mbed.h"
#include <iostream>
#include "sample_hardware.hpp"
#include "mbed_events.h"
void buttonPressedISR();
void addSample(float t);
void heartBeat();
 
DigitalOut led(LED1);
InterruptIn btn(USER_BUTTON);

//Creates a queue with the default size
EventQueue mainQueue(20);

Timer tmr1;
Thread t1;

//Buffer
float arrayOfTimes[10];

int main() {
    //Power on self test
    post();
    
    //Set up ISR
    btn.fall(buttonPressedISR);
    
    //Start timer
    tmr1.start();
    
    //Background flash (backed by a Ticker)
    mainQueue.call_every(500, heartBeat);
    
    // events are executed by the dispatch method
    mainQueue.dispatch();
    
    //Unreachable code
    printf("This should never appear!\n");
}

void buttonPressedISR() { 
    float t = tmr1.read();  //Immediately read time in seconds
    mainQueue.call(addSample, t);  
}

// Add sample to buffer
// Single parameter t, copy of latest switch time
void addSample(float t) {
    
    //Manually shuffle the buffer
    for (unsigned int n=9; n>0; n--) {
        arrayOfTimes[n] = arrayOfTimes[n-1];
    }
    
    //Add new sample
    arrayOfTimes[0] = t;
    
    //Display deltas
    for (unsigned int n=9; n>0; n--) {
        float delta = (arrayOfTimes[n] - arrayOfTimes[n-1]);
        printf("%5.1f ", delta);
    }
    printf("\n");
}

//Flashing LED 
void heartBeat() {
    led = !led;
}