Fork of the official mbed C/C++ SDK provides the software platform and libraries to build your applications. The fork has the documentation converted to Doxygen format

Dependents:   NervousPuppySprintOne NervousPuppySprint2602 Robot WarehouseBot1 ... more

Fork of mbed by mbed official

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 - Ticker
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_TICKER_H
simon.ford@mbed.co.uk 3:aefd12a1f1c5 6 #define MBED_TICKER_H
simon.ford@mbed.co.uk 3:aefd12a1f1c5 7
simon.ford@mbed.co.uk 3:aefd12a1f1c5 8 #include "TimerEvent.h"
simon.ford@mbed.co.uk 3:aefd12a1f1c5 9 #include "FunctionPointer.h"
simon.ford@mbed.co.uk 3:aefd12a1f1c5 10
simon.ford@mbed.co.uk 3:aefd12a1f1c5 11 namespace mbed {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 12
simon.ford@mbed.co.uk 3:aefd12a1f1c5 13 /* Class: Ticker
simon.ford@mbed.co.uk 3:aefd12a1f1c5 14 * A Ticker is used to call a function at a recurring interval
simon.ford@mbed.co.uk 3:aefd12a1f1c5 15 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 16 * You can use as many seperate Ticker objects as you require.
simon.ford@mbed.co.uk 3:aefd12a1f1c5 17 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 18 class Ticker : public TimerEvent {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 19
simon.ford@mbed.co.uk 3:aefd12a1f1c5 20 public:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 21
simon.ford@mbed.co.uk 3:aefd12a1f1c5 22 /* Function: attach
simon.ford@mbed.co.uk 3:aefd12a1f1c5 23 * Attach a function to be called by the Ticker, specifiying the interval in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 24 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 25 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 26 * fptr - pointer to the function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 27 * t - the time between calls in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 28 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 29 void attach(void (*fptr)(void), float t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 30 attach_us(fptr, t * 1000000.0f);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 31 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 32
simon.ford@mbed.co.uk 3:aefd12a1f1c5 33 /* Function: attach
simon.ford@mbed.co.uk 3:aefd12a1f1c5 34 * Attach a member function to be called by the Ticker, specifiying the interval in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 35 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 36 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 37 * tptr - pointer to the object to call the member function on
simon.ford@mbed.co.uk 3:aefd12a1f1c5 38 * mptr - pointer to the member function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 39 * t - the time between calls in seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 40 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 41 template<typename T>
simon.ford@mbed.co.uk 3:aefd12a1f1c5 42 void attach(T* tptr, void (T::*mptr)(void), float t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 43 attach_us(tptr, mptr, t * 1000000.0f);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 44 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 45
simon.ford@mbed.co.uk 3:aefd12a1f1c5 46 /* Function: attach_us
simon.ford@mbed.co.uk 3:aefd12a1f1c5 47 * Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 48 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 49 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 50 * fptr - pointer to the function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 51 * t - the time between calls in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 52 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 53 void attach_us(void (*fptr)(void), unsigned int t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 54 _function.attach(fptr);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 55 setup(t);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 56 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 57
simon.ford@mbed.co.uk 3:aefd12a1f1c5 58 /* Function: attach_us
simon.ford@mbed.co.uk 3:aefd12a1f1c5 59 * Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 60 *
simon.ford@mbed.co.uk 3:aefd12a1f1c5 61 * Variables:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 62 * tptr - pointer to the object to call the member function on
simon.ford@mbed.co.uk 3:aefd12a1f1c5 63 * mptr - pointer to the member function to be called
simon.ford@mbed.co.uk 3:aefd12a1f1c5 64 * t - the time between calls in micro-seconds
simon.ford@mbed.co.uk 3:aefd12a1f1c5 65 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 66 template<typename T>
simon.ford@mbed.co.uk 3:aefd12a1f1c5 67 void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
simon.ford@mbed.co.uk 3:aefd12a1f1c5 68 _function.attach(tptr, mptr);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 69 setup(t);
simon.ford@mbed.co.uk 3:aefd12a1f1c5 70 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 71
simon.ford@mbed.co.uk 3:aefd12a1f1c5 72 /* Function: detach
simon.ford@mbed.co.uk 3:aefd12a1f1c5 73 * Detach the function
simon.ford@mbed.co.uk 3:aefd12a1f1c5 74 */
simon.ford@mbed.co.uk 3:aefd12a1f1c5 75 void detach();
simon.ford@mbed.co.uk 3:aefd12a1f1c5 76
simon.ford@mbed.co.uk 3:aefd12a1f1c5 77 protected:
simon.ford@mbed.co.uk 3:aefd12a1f1c5 78
simon.ford@mbed.co.uk 3:aefd12a1f1c5 79 void setup(unsigned int t);
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 unsigned int _delay;
simon.ford@mbed.co.uk 3:aefd12a1f1c5 83 FunctionPointer _function;
simon.ford@mbed.co.uk 3:aefd12a1f1c5 84
simon.ford@mbed.co.uk 3:aefd12a1f1c5 85 };
simon.ford@mbed.co.uk 3:aefd12a1f1c5 86
simon.ford@mbed.co.uk 3:aefd12a1f1c5 87 }
simon.ford@mbed.co.uk 3:aefd12a1f1c5 88
simon.ford@mbed.co.uk 3:aefd12a1f1c5 89 #endif