Demonstration of EventQueues

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

Committer:
noutram
Date:
Tue Nov 21 13:23:58 2017 +0000
Revision:
11:67cb33bc247e
Parent:
10:3ab2495f24d9
Child:
12:01c7edd1dd19
Fixed small bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:f916cefba2f4 1 #include "mbed.h"
noutram 10:3ab2495f24d9 2 #include <iostream>
noutram 10:3ab2495f24d9 3 #include "sample_hardware.hpp"
noutram 10:3ab2495f24d9 4 #include "mbed_events.h"
noutram 10:3ab2495f24d9 5 void buttonPressedISR();
noutram 10:3ab2495f24d9 6 void addSample(float t);
noutram 10:3ab2495f24d9 7 void heartBeat();
noutram 10:3ab2495f24d9 8
noutram 10:3ab2495f24d9 9 InterruptIn btn(USER_BUTTON);
noutram 8:b28defacd894 10
noutram 10:3ab2495f24d9 11 //Creates a queue with the default size
noutram 11:67cb33bc247e 12 EventQueue mainQueue;
noutram 0:f916cefba2f4 13
noutram 10:3ab2495f24d9 14 Timer tmr1;
noutram 10:3ab2495f24d9 15 Thread t1;
noutram 0:f916cefba2f4 16
noutram 10:3ab2495f24d9 17 //Buffer
noutram 10:3ab2495f24d9 18 float arrayOfTimes[10];
noutram 8:b28defacd894 19
noutram 10:3ab2495f24d9 20 int main() {
noutram 10:3ab2495f24d9 21 //Power on self test
noutram 10:3ab2495f24d9 22 post();
noutram 10:3ab2495f24d9 23
noutram 11:67cb33bc247e 24 puts("Press the BLUE button as close to once a second as you can");
noutram 11:67cb33bc247e 25
noutram 10:3ab2495f24d9 26 //Set up ISR
noutram 11:67cb33bc247e 27 btn.rise(buttonPressedISR);
noutram 10:3ab2495f24d9 28
noutram 10:3ab2495f24d9 29 //Start timer
noutram 10:3ab2495f24d9 30 tmr1.start();
noutram 10:3ab2495f24d9 31
noutram 10:3ab2495f24d9 32 //Background flash (backed by a Ticker)
noutram 10:3ab2495f24d9 33 mainQueue.call_every(500, heartBeat);
noutram 10:3ab2495f24d9 34
noutram 10:3ab2495f24d9 35 // events are executed by the dispatch method
noutram 10:3ab2495f24d9 36 mainQueue.dispatch();
noutram 10:3ab2495f24d9 37
noutram 10:3ab2495f24d9 38 //Unreachable code
noutram 10:3ab2495f24d9 39 printf("This should never appear!\n");
noutram 8:b28defacd894 40 }
noutram 8:b28defacd894 41
noutram 10:3ab2495f24d9 42 void buttonPressedISR() {
noutram 10:3ab2495f24d9 43 float t = tmr1.read(); //Immediately read time in seconds
noutram 10:3ab2495f24d9 44 mainQueue.call(addSample, t);
noutram 0:f916cefba2f4 45 }
noutram 0:f916cefba2f4 46
noutram 10:3ab2495f24d9 47 // Add sample to buffer
noutram 10:3ab2495f24d9 48 // Single parameter t, copy of latest switch time
noutram 10:3ab2495f24d9 49 void addSample(float t) {
noutram 10:3ab2495f24d9 50
noutram 10:3ab2495f24d9 51 //Manually shuffle the buffer
noutram 10:3ab2495f24d9 52 for (unsigned int n=9; n>0; n--) {
noutram 10:3ab2495f24d9 53 arrayOfTimes[n] = arrayOfTimes[n-1];
noutram 10:3ab2495f24d9 54 }
noutram 7:cd015e83995a 55
noutram 10:3ab2495f24d9 56 //Add new sample
noutram 10:3ab2495f24d9 57 arrayOfTimes[0] = t;
noutram 10:3ab2495f24d9 58
noutram 10:3ab2495f24d9 59 //Display deltas
noutram 10:3ab2495f24d9 60 for (unsigned int n=9; n>0; n--) {
noutram 11:67cb33bc247e 61 float delta = (arrayOfTimes[n-1] - arrayOfTimes[n]);
noutram 10:3ab2495f24d9 62 printf("%5.1f ", delta);
noutram 0:f916cefba2f4 63 }
noutram 10:3ab2495f24d9 64 printf("\n");
noutram 0:f916cefba2f4 65 }
noutram 2:70084af839d3 66
noutram 10:3ab2495f24d9 67 //Flashing LED
noutram 10:3ab2495f24d9 68 void heartBeat() {
noutram 11:67cb33bc247e 69 redLED = !redLED;
noutram 10:3ab2495f24d9 70 }