Lab 1 Program C
Fork of mbed by
Timeout.h@44:1c5f591fce58, 2015-09-29 (annotated)
- Committer:
- mattsims12
- Date:
- Tue Sep 29 03:04:58 2015 +0000
- Revision:
- 44:1c5f591fce58
- Parent:
- 43:aff670d0d510
Lab 1 Program C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 1 | /* mbed Microcontroller Library - Timeout |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 4 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 5 | #ifndef MBED_TIMEOUT_H |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 6 | #define MBED_TIMEOUT_H |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 7 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 8 | #include "Ticker.h" |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 9 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 10 | namespace mbed { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 11 | |
screamer | 43:aff670d0d510 | 12 | /** A Timeout is used to call a function at a point in the future |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 13 | * |
screamer | 43:aff670d0d510 | 14 | * You can use as many seperate Timeout objects as you require. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 15 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 16 | * Example: |
screamer | 43:aff670d0d510 | 17 | * @code |
screamer | 43:aff670d0d510 | 18 | * // Blink until timeout. |
screamer | 43:aff670d0d510 | 19 | * |
screamer | 43:aff670d0d510 | 20 | * #include "mbed.h" |
screamer | 43:aff670d0d510 | 21 | * |
screamer | 43:aff670d0d510 | 22 | * Timeout timeout; |
screamer | 43:aff670d0d510 | 23 | * DigitalOut led(LED1); |
screamer | 43:aff670d0d510 | 24 | * |
screamer | 43:aff670d0d510 | 25 | * int on = 1; |
screamer | 43:aff670d0d510 | 26 | * |
screamer | 43:aff670d0d510 | 27 | * void attimeout() { |
screamer | 43:aff670d0d510 | 28 | * on = 0; |
screamer | 43:aff670d0d510 | 29 | * } |
screamer | 43:aff670d0d510 | 30 | * |
screamer | 43:aff670d0d510 | 31 | * int main() { |
screamer | 43:aff670d0d510 | 32 | * timeout.attach(&attimeout, 5); |
screamer | 43:aff670d0d510 | 33 | * while(on) { |
screamer | 43:aff670d0d510 | 34 | * led = !led; |
screamer | 43:aff670d0d510 | 35 | * wait(0.2); |
screamer | 43:aff670d0d510 | 36 | * } |
screamer | 43:aff670d0d510 | 37 | * } |
screamer | 43:aff670d0d510 | 38 | * @endcode |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 39 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 40 | class Timeout : public Ticker { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 41 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 42 | #if 0 // For documentation |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 43 | |
screamer | 43:aff670d0d510 | 44 | /** Attach a function to be called by the Timeout, specifiying the delay in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 45 | * |
screamer | 43:aff670d0d510 | 46 | * @param fptr pointer to the function to be called |
screamer | 43:aff670d0d510 | 47 | * @param t the time before the call in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 48 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 49 | void attach(void (*fptr)(void), float t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 50 | attach_us(fptr, t * 1000000.0f); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 51 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 52 | |
screamer | 43:aff670d0d510 | 53 | /** Attach a member function to be called by the Timeout, specifiying the delay in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 54 | * |
screamer | 43:aff670d0d510 | 55 | * @param tptr pointer to the object to call the member function on |
screamer | 43:aff670d0d510 | 56 | * @param mptr pointer to the member function to be called |
screamer | 43:aff670d0d510 | 57 | * @param t the time before the calls in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 58 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 59 | template<typename T> |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 60 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 61 | attach_us(tptr, mptr, t * 1000000.0f); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 62 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 63 | |
screamer | 43:aff670d0d510 | 64 | /** Attach a function to be called by the Timeout, specifiying the delay in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 65 | * |
screamer | 43:aff670d0d510 | 66 | * @param fptr pointer to the function to be called |
screamer | 43:aff670d0d510 | 67 | * @param t the time before the call in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 68 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 69 | void attach_us(void (*fptr)(void), unsigned int t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 70 | _function.attach(fptr); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 71 | setup(t); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 72 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 73 | |
screamer | 43:aff670d0d510 | 74 | /** Attach a member function to be called by the Timeout, specifiying the delay in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 75 | * |
screamer | 43:aff670d0d510 | 76 | * @param tptr pointer to the object to call the member function on |
screamer | 43:aff670d0d510 | 77 | * @param mptr pointer to the member function to be called |
screamer | 43:aff670d0d510 | 78 | * @param t the time before the call in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 79 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 80 | template<typename T> |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 81 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 82 | _function.attach(tptr, mptr); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 83 | setup(t); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 84 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 85 | |
screamer | 43:aff670d0d510 | 86 | /** Detach the function |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 87 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 88 | void detach(); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 89 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 90 | #endif |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 91 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 92 | protected: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 93 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 94 | virtual void handler(); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 95 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 96 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 97 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 98 | } // namespace mbed |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 99 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 100 | #endif |