Demonstration of EventQueues

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

Committer:
noutram
Date:
Tue Nov 21 13:50:04 2017 +0000
Revision:
12:01c7edd1dd19
Parent:
11:67cb33bc247e
Now addressed the issue of switch bounce

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 12:01c7edd1dd19 6 void buttonReleasedISR();
noutram 10:3ab2495f24d9 7 void addSample(float t);
noutram 10:3ab2495f24d9 8 void heartBeat();
noutram 10:3ab2495f24d9 9
noutram 10:3ab2495f24d9 10 InterruptIn btn(USER_BUTTON);
noutram 8:b28defacd894 11
noutram 10:3ab2495f24d9 12 //Creates a queue with the default size
noutram 11:67cb33bc247e 13 EventQueue mainQueue;
noutram 0:f916cefba2f4 14
noutram 10:3ab2495f24d9 15 Timer tmr1;
noutram 10:3ab2495f24d9 16 Thread t1;
noutram 0:f916cefba2f4 17
noutram 10:3ab2495f24d9 18 //Buffer
noutram 10:3ab2495f24d9 19 float arrayOfTimes[10];
noutram 8:b28defacd894 20
noutram 12:01c7edd1dd19 21 //Set up rising edge detection
noutram 12:01c7edd1dd19 22 void rising() {
noutram 12:01c7edd1dd19 23 btn.rise(buttonPressedISR);
noutram 12:01c7edd1dd19 24 }
noutram 12:01c7edd1dd19 25 //Set up falling edge detection
noutram 12:01c7edd1dd19 26 void falling() {
noutram 12:01c7edd1dd19 27 btn.fall(buttonReleasedISR);
noutram 12:01c7edd1dd19 28 }
noutram 12:01c7edd1dd19 29
noutram 10:3ab2495f24d9 30 int main() {
noutram 10:3ab2495f24d9 31 //Power on self test
noutram 10:3ab2495f24d9 32 post();
noutram 10:3ab2495f24d9 33
noutram 11:67cb33bc247e 34 puts("Press the BLUE button as close to once a second as you can");
noutram 11:67cb33bc247e 35
noutram 10:3ab2495f24d9 36 //Set up ISR
noutram 11:67cb33bc247e 37 btn.rise(buttonPressedISR);
noutram 10:3ab2495f24d9 38
noutram 10:3ab2495f24d9 39 //Start timer
noutram 10:3ab2495f24d9 40 tmr1.start();
noutram 10:3ab2495f24d9 41
noutram 10:3ab2495f24d9 42 //Background flash (backed by a Ticker)
noutram 10:3ab2495f24d9 43 mainQueue.call_every(500, heartBeat);
noutram 10:3ab2495f24d9 44
noutram 10:3ab2495f24d9 45 // events are executed by the dispatch method
noutram 10:3ab2495f24d9 46 mainQueue.dispatch();
noutram 10:3ab2495f24d9 47
noutram 10:3ab2495f24d9 48 //Unreachable code
noutram 10:3ab2495f24d9 49 printf("This should never appear!\n");
noutram 8:b28defacd894 50 }
noutram 8:b28defacd894 51
noutram 10:3ab2495f24d9 52 void buttonPressedISR() {
noutram 10:3ab2495f24d9 53 float t = tmr1.read(); //Immediately read time in seconds
noutram 12:01c7edd1dd19 54 btn.rise(NULL); //Turn off interrupt
noutram 12:01c7edd1dd19 55 mainQueue.call(addSample, t); //Add sample to buffer and print
noutram 12:01c7edd1dd19 56 mainQueue.call_in(200, falling); //After 200ms, look for falling edge
noutram 12:01c7edd1dd19 57 }
noutram 12:01c7edd1dd19 58
noutram 12:01c7edd1dd19 59 void buttonReleasedISR() {
noutram 12:01c7edd1dd19 60 btn.fall(NULL); //Turn off interrupt
noutram 12:01c7edd1dd19 61 mainQueue.call_in(200, rising); //After 200ms, look for rising edge again
noutram 0:f916cefba2f4 62 }
noutram 0:f916cefba2f4 63
noutram 10:3ab2495f24d9 64 // Add sample to buffer
noutram 10:3ab2495f24d9 65 // Single parameter t, copy of latest switch time
noutram 10:3ab2495f24d9 66 void addSample(float t) {
noutram 10:3ab2495f24d9 67
noutram 10:3ab2495f24d9 68 //Manually shuffle the buffer
noutram 10:3ab2495f24d9 69 for (unsigned int n=9; n>0; n--) {
noutram 10:3ab2495f24d9 70 arrayOfTimes[n] = arrayOfTimes[n-1];
noutram 10:3ab2495f24d9 71 }
noutram 7:cd015e83995a 72
noutram 10:3ab2495f24d9 73 //Add new sample
noutram 10:3ab2495f24d9 74 arrayOfTimes[0] = t;
noutram 10:3ab2495f24d9 75
noutram 10:3ab2495f24d9 76 //Display deltas
noutram 10:3ab2495f24d9 77 for (unsigned int n=9; n>0; n--) {
noutram 11:67cb33bc247e 78 float delta = (arrayOfTimes[n-1] - arrayOfTimes[n]);
noutram 10:3ab2495f24d9 79 printf("%5.1f ", delta);
noutram 0:f916cefba2f4 80 }
noutram 10:3ab2495f24d9 81 printf("\n");
noutram 12:01c7edd1dd19 82
noutram 0:f916cefba2f4 83 }
noutram 2:70084af839d3 84
noutram 10:3ab2495f24d9 85 //Flashing LED
noutram 10:3ab2495f24d9 86 void heartBeat() {
noutram 11:67cb33bc247e 87 redLED = !redLED;
noutram 10:3ab2495f24d9 88 }