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