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.
main.cpp@0:402d16a0d4dd, 2017-03-15 (annotated)
- Committer:
- tulanthoar
- Date:
- Wed Mar 15 20:03:21 2017 +0000
- Revision:
- 0:402d16a0d4dd
- Child:
- 2:aa4afe9a928c
sleep example
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tulanthoar | 0:402d16a0d4dd | 1 | #include "mbed.h" |
| tulanthoar | 0:402d16a0d4dd | 2 | |
| tulanthoar | 0:402d16a0d4dd | 3 | InterruptIn event(USER_BUTTON); |
| tulanthoar | 0:402d16a0d4dd | 4 | DigitalOut myled(LED1); |
| tulanthoar | 0:402d16a0d4dd | 5 | |
| tulanthoar | 0:402d16a0d4dd | 6 | int go_to_sleep = 0; |
| tulanthoar | 0:402d16a0d4dd | 7 | |
| tulanthoar | 0:402d16a0d4dd | 8 | void pressed() |
| tulanthoar | 0:402d16a0d4dd | 9 | { |
| tulanthoar | 0:402d16a0d4dd | 10 | printf("Button pressed\n"); |
| tulanthoar | 0:402d16a0d4dd | 11 | go_to_sleep = go_to_sleep + 1; |
| tulanthoar | 0:402d16a0d4dd | 12 | if (go_to_sleep > 3) go_to_sleep = 0; |
| tulanthoar | 0:402d16a0d4dd | 13 | |
| tulanthoar | 0:402d16a0d4dd | 14 | } |
| tulanthoar | 0:402d16a0d4dd | 15 | |
| tulanthoar | 0:402d16a0d4dd | 16 | int main() |
| tulanthoar | 0:402d16a0d4dd | 17 | { |
| tulanthoar | 0:402d16a0d4dd | 18 | int i = 0; |
| tulanthoar | 0:402d16a0d4dd | 19 | |
| tulanthoar | 0:402d16a0d4dd | 20 | printf("\nPress Button to enter/exit sleep & deepsleep\n"); |
| tulanthoar | 0:402d16a0d4dd | 21 | |
| tulanthoar | 0:402d16a0d4dd | 22 | event.fall(&pressed); |
| tulanthoar | 0:402d16a0d4dd | 23 | |
| tulanthoar | 0:402d16a0d4dd | 24 | while (1) { |
| tulanthoar | 0:402d16a0d4dd | 25 | |
| tulanthoar | 0:402d16a0d4dd | 26 | if ((go_to_sleep == 0) || (go_to_sleep == 2)) { |
| tulanthoar | 0:402d16a0d4dd | 27 | printf("%d: Running\n", i); |
| tulanthoar | 0:402d16a0d4dd | 28 | myled = !myled; |
| tulanthoar | 0:402d16a0d4dd | 29 | wait(1.0); |
| tulanthoar | 0:402d16a0d4dd | 30 | } |
| tulanthoar | 0:402d16a0d4dd | 31 | |
| tulanthoar | 0:402d16a0d4dd | 32 | if (go_to_sleep == 1) { |
| tulanthoar | 0:402d16a0d4dd | 33 | myled = 0; |
| tulanthoar | 0:402d16a0d4dd | 34 | printf("%d: Entering sleep (press user button to resume)\n", i); |
| tulanthoar | 0:402d16a0d4dd | 35 | sleep(); |
| tulanthoar | 0:402d16a0d4dd | 36 | } |
| tulanthoar | 0:402d16a0d4dd | 37 | |
| tulanthoar | 0:402d16a0d4dd | 38 | if (go_to_sleep == 3) { |
| tulanthoar | 0:402d16a0d4dd | 39 | myled = 0; |
| tulanthoar | 0:402d16a0d4dd | 40 | printf("%d: Entering deepsleep (press user button to resume)\n", i); |
| tulanthoar | 0:402d16a0d4dd | 41 | deepsleep(); |
| tulanthoar | 0:402d16a0d4dd | 42 | } |
| tulanthoar | 0:402d16a0d4dd | 43 | |
| tulanthoar | 0:402d16a0d4dd | 44 | i++; |
| tulanthoar | 0:402d16a0d4dd | 45 | } |
| tulanthoar | 0:402d16a0d4dd | 46 | } |