Toggle running with reset button. You can run and stop mbed without external switch. Push reset button to stop program. Push reset button to run program again. This sample program uses LocalFileSystem to save next running status.
main.cpp@0:9eb1ed79e0a3, 2011-11-21 (annotated)
- Committer:
- Chi7Shi
- Date:
- Mon Nov 21 03:54:46 2011 +0000
- Revision:
- 0:9eb1ed79e0a3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Chi7Shi | 0:9eb1ed79e0a3 | 1 | // |
Chi7Shi | 0:9eb1ed79e0a3 | 2 | // Toggle running with reset button by K.Shibata |
Chi7Shi | 0:9eb1ed79e0a3 | 3 | // |
Chi7Shi | 0:9eb1ed79e0a3 | 4 | // Push reset button to stop running. |
Chi7Shi | 0:9eb1ed79e0a3 | 5 | // Push reset button to run again. |
Chi7Shi | 0:9eb1ed79e0a3 | 6 | // |
Chi7Shi | 0:9eb1ed79e0a3 | 7 | |
Chi7Shi | 0:9eb1ed79e0a3 | 8 | #include "mbed.h" |
Chi7Shi | 0:9eb1ed79e0a3 | 9 | |
Chi7Shi | 0:9eb1ed79e0a3 | 10 | DigitalOut myled(LED1); |
Chi7Shi | 0:9eb1ed79e0a3 | 11 | |
Chi7Shi | 0:9eb1ed79e0a3 | 12 | LocalFileSystem local( "local" ); // Create the local filesystem under the name "local" |
Chi7Shi | 0:9eb1ed79e0a3 | 13 | |
Chi7Shi | 0:9eb1ed79e0a3 | 14 | int main() { |
Chi7Shi | 0:9eb1ed79e0a3 | 15 | FILE *fp; |
Chi7Shi | 0:9eb1ed79e0a3 | 16 | bool is_checked = false; |
Chi7Shi | 0:9eb1ed79e0a3 | 17 | |
Chi7Shi | 0:9eb1ed79e0a3 | 18 | fp = fopen( "/local/toggle.txt", "r" ); // Open "toggle.txt" on the local file system for reading. |
Chi7Shi | 0:9eb1ed79e0a3 | 19 | if ( fp != NULL ) { |
Chi7Shi | 0:9eb1ed79e0a3 | 20 | int c = fgetc( fp ); |
Chi7Shi | 0:9eb1ed79e0a3 | 21 | if ( c == '1' ) { |
Chi7Shi | 0:9eb1ed79e0a3 | 22 | is_checked = true; |
Chi7Shi | 0:9eb1ed79e0a3 | 23 | } |
Chi7Shi | 0:9eb1ed79e0a3 | 24 | fclose( fp ); |
Chi7Shi | 0:9eb1ed79e0a3 | 25 | } |
Chi7Shi | 0:9eb1ed79e0a3 | 26 | |
Chi7Shi | 0:9eb1ed79e0a3 | 27 | fp = fopen( "/local/toggle.txt", "w" ); // Open "toggle.txt" on the local file system for writing. |
Chi7Shi | 0:9eb1ed79e0a3 | 28 | if ( fp != NULL ) { |
Chi7Shi | 0:9eb1ed79e0a3 | 29 | if ( is_checked ) { |
Chi7Shi | 0:9eb1ed79e0a3 | 30 | fputc( '0', fp ); |
Chi7Shi | 0:9eb1ed79e0a3 | 31 | } else { |
Chi7Shi | 0:9eb1ed79e0a3 | 32 | fputc( '1', fp ); |
Chi7Shi | 0:9eb1ed79e0a3 | 33 | } |
Chi7Shi | 0:9eb1ed79e0a3 | 34 | fclose( fp ); |
Chi7Shi | 0:9eb1ed79e0a3 | 35 | } |
Chi7Shi | 0:9eb1ed79e0a3 | 36 | |
Chi7Shi | 0:9eb1ed79e0a3 | 37 | if ( is_checked ) while( 1 ); // Infinite loop. |
Chi7Shi | 0:9eb1ed79e0a3 | 38 | |
Chi7Shi | 0:9eb1ed79e0a3 | 39 | while (1) { |
Chi7Shi | 0:9eb1ed79e0a3 | 40 | myled = 1; |
Chi7Shi | 0:9eb1ed79e0a3 | 41 | wait(0.2); |
Chi7Shi | 0:9eb1ed79e0a3 | 42 | myled = 0; |
Chi7Shi | 0:9eb1ed79e0a3 | 43 | wait(0.2); |
Chi7Shi | 0:9eb1ed79e0a3 | 44 | } |
Chi7Shi | 0:9eb1ed79e0a3 | 45 | } |