Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Timeout by
Main.cpp@1:50617d33bd51, 2019-01-08 (annotated)
- Committer:
- jimbaud
- Date:
- Tue Jan 08 10:29:04 2019 +0000
- Revision:
- 1:50617d33bd51
- Parent:
- 0:7a0f7fbc736b
Use the Timeout interface to set up an interrupt to call a function after a specified delay.; You can create any number of Timeout objects, allowing multiple outstanding interrupts at the same time.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jimbaud | 0:7a0f7fbc736b | 1 | /* https://os.mbed.com/docs/v5.7/reference/timer.html |
| jimbaud | 0:7a0f7fbc736b | 2 | |
| jimbaud | 0:7a0f7fbc736b | 3 | Use the Timeout interface to set up an interrupt to call a function after a specified delay. |
| jimbaud | 0:7a0f7fbc736b | 4 | You can create any number of Timeout objects, allowing multiple outstanding interrupts at the same time. |
| jimbaud | 0:7a0f7fbc736b | 5 | Warnings and notes |
| jimbaud | 0:7a0f7fbc736b | 6 | Timers are based on 32-bit int microsecond counters, so they can only time up to a maximum of 2^31-1 microseconds (30 minutes). They are designed for times between microseconds and seconds. For longer times, you should consider the time() real time clock. |
| jimbaud | 0:7a0f7fbc736b | 7 | No blocking code in ISR: avoid any call to wait, infinite while loop or blocking calls in general. |
| jimbaud | 0:7a0f7fbc736b | 8 | No printf, malloc or new in ISR: Avoid any call to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR. |
| jimbaud | 0:7a0f7fbc736b | 9 | */ |
| jimbaud | 0:7a0f7fbc736b | 10 | |
| jimbaud | 0:7a0f7fbc736b | 11 | #include "mbed.h" |
| jimbaud | 0:7a0f7fbc736b | 12 | |
| jimbaud | 1:50617d33bd51 | 13 | /* Example 1: |
| jimbaud | 0:7a0f7fbc736b | 14 | Timeout flipper; |
| jimbaud | 0:7a0f7fbc736b | 15 | DigitalOut led1(LED1); |
| jimbaud | 0:7a0f7fbc736b | 16 | |
| jimbaud | 0:7a0f7fbc736b | 17 | void flip() { |
| jimbaud | 0:7a0f7fbc736b | 18 | led1 = !led1; |
| jimbaud | 0:7a0f7fbc736b | 19 | } |
| jimbaud | 0:7a0f7fbc736b | 20 | |
| jimbaud | 0:7a0f7fbc736b | 21 | int main() { |
| jimbaud | 0:7a0f7fbc736b | 22 | led2 = 1; |
| jimbaud | 0:7a0f7fbc736b | 23 | flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds |
| jimbaud | 0:7a0f7fbc736b | 24 | // spin in a main loop. flipper will interrupt it to call flip |
| jimbaud | 0:7a0f7fbc736b | 25 | |
| jimbaud | 0:7a0f7fbc736b | 26 | while(1) { |
| jimbaud | 0:7a0f7fbc736b | 27 | } |
| jimbaud | 1:50617d33bd51 | 28 | } |
| jimbaud | 1:50617d33bd51 | 29 | |
| jimbaud | 1:50617d33bd51 | 30 | */ |
| jimbaud | 1:50617d33bd51 | 31 | |
| jimbaud | 1:50617d33bd51 | 32 | /*Example 2: |
| jimbaud | 1:50617d33bd51 | 33 | |
| jimbaud | 1:50617d33bd51 | 34 | // A class for flip()-ing a DigitalOut |
| jimbaud | 1:50617d33bd51 | 35 | |
| jimbaud | 1:50617d33bd51 | 36 | class Flipper { |
| jimbaud | 1:50617d33bd51 | 37 | public: |
| jimbaud | 1:50617d33bd51 | 38 | Flipper(PinName pin) : _pin(pin) { |
| jimbaud | 1:50617d33bd51 | 39 | _pin = 0; |
| jimbaud | 1:50617d33bd51 | 40 | } |
| jimbaud | 1:50617d33bd51 | 41 | void flip() { |
| jimbaud | 1:50617d33bd51 | 42 | _pin = !_pin; |
| jimbaud | 1:50617d33bd51 | 43 | } |
| jimbaud | 1:50617d33bd51 | 44 | |
| jimbaud | 1:50617d33bd51 | 45 | private: |
| jimbaud | 1:50617d33bd51 | 46 | DigitalOut _pin; |
| jimbaud | 1:50617d33bd51 | 47 | }; |
| jimbaud | 1:50617d33bd51 | 48 | |
| jimbaud | 1:50617d33bd51 | 49 | Flipper LED(LED1); |
| jimbaud | 1:50617d33bd51 | 50 | Timeout t; |
| jimbaud | 1:50617d33bd51 | 51 | |
| jimbaud | 1:50617d33bd51 | 52 | int main() { |
| jimbaud | 1:50617d33bd51 | 53 | // the address of the object, member function, and interval |
| jimbaud | 1:50617d33bd51 | 54 | t.attach(callback(&LED, &Flipper::flip), 2.0); |
| jimbaud | 1:50617d33bd51 | 55 | // spin in a main loop. flipper will interrupt it to call flip |
| jimbaud | 1:50617d33bd51 | 56 | |
| jimbaud | 1:50617d33bd51 | 57 | while(1) { |
| jimbaud | 1:50617d33bd51 | 58 | } |
| jimbaud | 1:50617d33bd51 | 59 | } |
| jimbaud | 1:50617d33bd51 | 60 | |
| jimbaud | 1:50617d33bd51 | 61 | */ |
