Lab 1 Program C

Dependents:   Lab1C

Fork of mbed by -deleted-

Committer:
simon.ford@mbed.co.uk
Date:
Fri Nov 14 15:25:20 2008 +0000
Revision:
3:aefd12a1f1c5
Child:
4:5d1359a283bc
Added Ticker and Timeout abstractions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon.ford@mbed.co.uk 3:aefd12a1f1c5 1 /* mbed Microcontroller Library - Timeout
simon.ford@mbed.co.uk 3:aefd12a1f1c5 2 * Copyright (c) 2007-2008, sford
simon.ford@mbed.co.uk 3:aefd12a1f1c5 3 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 4
simon.ford@mbed.co.uk 3:aefd12a1f1c5 5 #ifndef MBED_TIMEOUT_H
simon.ford@mbed.co.uk 3:aefd12a1f1c5 6 #define MBED_TIMEOUT_H
simon.ford@mbed.co.uk 3:aefd12a1f1c5 7
simon.ford@mbed.co.uk 3:aefd12a1f1c5 8 #include "Ticker.h"
simon.ford@mbed.co.uk 3:aefd12a1f1c5 9
simon.ford@mbed.co.uk 3:aefd12a1f1c5 10 namespace mbed {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 11
simon.ford@mbed.co.uk 3:aefd12a1f1c5 12 /* Class: Timeout
simon.ford@mbed.co.uk 3:aefd12a1f1c5 13 * A Timeout is used to call a function at a point in the future
simon.ford@mbed.co.uk 3:aefd12a1f1c5 14 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 15 * You can use as many seperate Timeout objects as you require.
simon.ford@mbed.co.uk 3:aefd12a1f1c5 16 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 17 class Timeout : public Ticker {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 18
simon.ford@mbed.co.uk 3:aefd12a1f1c5 19 #if 0 // For documentation
simon.ford@mbed.co.uk 3:aefd12a1f1c5 20
simon.ford@mbed.co.uk 3:aefd12a1f1c5 21 /* Function: attach
simon.ford@mbed.co.uk 3:aefd12a1f1c5 22 * Attach a function to be called by the Timeout, specifiying the delay in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 23 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 24 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 25 * fptr - pointer to the function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 26 * t - the time before the call in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 27 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 28 void attach(void (*fptr)(void), float t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 29 attach_us(fptr, t * 1000000.0f);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 30 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 31
simon.ford@mbed.co.uk 3:aefd12a1f1c5 32 /* Function: attach
simon.ford@mbed.co.uk 3:aefd12a1f1c5 33 * Attach a member function to be called by the Timeout, specifiying the delay in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 34 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 35 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 36 * tptr - pointer to the object to call the member function on
simon.ford@mbed.co.uk 3:aefd12a1f1c5 37 * mptr - pointer to the member function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 38 * t - the time before the calls in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 39 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 40 template<typename T>
simon.ford@mbed.co.uk 3:aefd12a1f1c5 41 void attach(T* tptr, void (T::*mptr)(void), float t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 42 attach_us(tptr, mptr, t * 1000000.0f);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 43 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 44
simon.ford@mbed.co.uk 3:aefd12a1f1c5 45 /* Function: attach_us
simon.ford@mbed.co.uk 3:aefd12a1f1c5 46 * Attach a function to be called by the Timeout, specifiying the delay in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 47 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 48 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 49 * fptr - pointer to the function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 50 * t - the time before the call in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 51 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 52 void attach_us(void (*fptr)(void), unsigned int t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 53 _function.attach(fptr);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 54 setup(t);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 55 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 56
simon.ford@mbed.co.uk 3:aefd12a1f1c5 57 /* Function: attach_us
simon.ford@mbed.co.uk 3:aefd12a1f1c5 58 * Attach a member function to be called by the Timeout, specifiying the delay in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 59 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 60 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 61 * tptr - pointer to the object to call the member function on
simon.ford@mbed.co.uk 3:aefd12a1f1c5 62 * mptr - pointer to the member function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 63 * t - the time before the call in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 64 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 65 template<typename T>
simon.ford@mbed.co.uk 3:aefd12a1f1c5 66 void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 67 _function.attach(tptr, mptr);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 68 setup(t);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 69 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 70
simon.ford@mbed.co.uk 3:aefd12a1f1c5 71 /* Function: detach
simon.ford@mbed.co.uk 3:aefd12a1f1c5 72 * Detach the function
simon.ford@mbed.co.uk 3:aefd12a1f1c5 73 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 74 void detach();
simon.ford@mbed.co.uk 3:aefd12a1f1c5 75
simon.ford@mbed.co.uk 3:aefd12a1f1c5 76 #endif
simon.ford@mbed.co.uk 3:aefd12a1f1c5 77
simon.ford@mbed.co.uk 3:aefd12a1f1c5 78 protected:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 79
simon.ford@mbed.co.uk 3:aefd12a1f1c5 80 virtual void handler();
simon.ford@mbed.co.uk 3:aefd12a1f1c5 81
simon.ford@mbed.co.uk 3:aefd12a1f1c5 82 };
simon.ford@mbed.co.uk 3:aefd12a1f1c5 83
simon.ford@mbed.co.uk 3:aefd12a1f1c5 84 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 85
simon.ford@mbed.co.uk 3:aefd12a1f1c5 86 #endif