Demonstration of EventQueues

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

main.cpp

Committer:
noutram
Date:
2017-11-21
Revision:
11:67cb33bc247e
Parent:
10:3ab2495f24d9
Child:
12:01c7edd1dd19

File content as of revision 11:67cb33bc247e:

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

//Creates a queue with the default size
EventQueue mainQueue;

Timer tmr1;
Thread t1;

//Buffer
float arrayOfTimes[10];

int main() {
    //Power on self test
    post();
    
    puts("Press the BLUE button as close to once a second as you can");
    
    //Set up ISR
    btn.rise(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-1] - arrayOfTimes[n]);
        printf("%5.1f ", delta);
    }
    printf("\n");
}

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