Solution

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task650-mbedos-FZ429ZI-eventloop by University of Plymouth - Stages 1, 2 and 3

Committer:
noutram
Date:
Mon Nov 20 10:51:32 2017 +0000
Revision:
10:3ab2495f24d9
Parent:
9:c46e831f8e4a
Child:
11:67cb33bc247e
Initial demo of EventQueues

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 DigitalOut led(LED1);
noutram 10:3ab2495f24d9 10 InterruptIn btn(USER_BUTTON);
noutram 8:b28defacd894 11
noutram 10:3ab2495f24d9 12 //Creates a queue with the default size
noutram 10:3ab2495f24d9 13 EventQueue mainQueue(20);
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 10:3ab2495f24d9 21 int main() {
noutram 10:3ab2495f24d9 22 //Power on self test
noutram 10:3ab2495f24d9 23 post();
noutram 10:3ab2495f24d9 24
noutram 10:3ab2495f24d9 25 //Set up ISR
noutram 10:3ab2495f24d9 26 btn.fall(buttonPressedISR);
noutram 10:3ab2495f24d9 27
noutram 10:3ab2495f24d9 28 //Start timer
noutram 10:3ab2495f24d9 29 tmr1.start();
noutram 10:3ab2495f24d9 30
noutram 10:3ab2495f24d9 31 //Background flash (backed by a Ticker)
noutram 10:3ab2495f24d9 32 mainQueue.call_every(500, heartBeat);
noutram 10:3ab2495f24d9 33
noutram 10:3ab2495f24d9 34 // events are executed by the dispatch method
noutram 10:3ab2495f24d9 35 mainQueue.dispatch();
noutram 10:3ab2495f24d9 36
noutram 10:3ab2495f24d9 37 //Unreachable code
noutram 10:3ab2495f24d9 38 printf("This should never appear!\n");
noutram 8:b28defacd894 39 }
noutram 8:b28defacd894 40
noutram 10:3ab2495f24d9 41 void buttonPressedISR() {
noutram 10:3ab2495f24d9 42 float t = tmr1.read(); //Immediately read time in seconds
noutram 10:3ab2495f24d9 43 mainQueue.call(addSample, t);
noutram 0:f916cefba2f4 44 }
noutram 0:f916cefba2f4 45
noutram 10:3ab2495f24d9 46 // Add sample to buffer
noutram 10:3ab2495f24d9 47 // Single parameter t, copy of latest switch time
noutram 10:3ab2495f24d9 48 void addSample(float t) {
noutram 10:3ab2495f24d9 49
noutram 10:3ab2495f24d9 50 //Manually shuffle the buffer
noutram 10:3ab2495f24d9 51 for (unsigned int n=9; n>0; n--) {
noutram 10:3ab2495f24d9 52 arrayOfTimes[n] = arrayOfTimes[n-1];
noutram 10:3ab2495f24d9 53 }
noutram 7:cd015e83995a 54
noutram 10:3ab2495f24d9 55 //Add new sample
noutram 10:3ab2495f24d9 56 arrayOfTimes[0] = t;
noutram 10:3ab2495f24d9 57
noutram 10:3ab2495f24d9 58 //Display deltas
noutram 10:3ab2495f24d9 59 for (unsigned int n=9; n>0; n--) {
noutram 10:3ab2495f24d9 60 float delta = (arrayOfTimes[n] - arrayOfTimes[n-1]);
noutram 10:3ab2495f24d9 61 printf("%5.1f ", delta);
noutram 0:f916cefba2f4 62 }
noutram 10:3ab2495f24d9 63 printf("\n");
noutram 0:f916cefba2f4 64 }
noutram 2:70084af839d3 65
noutram 10:3ab2495f24d9 66 //Flashing LED
noutram 10:3ab2495f24d9 67 void heartBeat() {
noutram 10:3ab2495f24d9 68 led = !led;
noutram 10:3ab2495f24d9 69 }