/* Simple serial console I/O test program */ // Copyright (C)2013-2018, Philip Munts, President, Munts AM Corp. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. #define __ASSERT_USE_STDERR #include #include #include #include #include #ifndef BAUDRATE #define BAUDRATE 115200 #endif int main(void) { int i; char buf[256]; int x, y; const char spinner[] = "|/-\\"; wdt_disable(); conio_init(BAUDRATE); #ifdef CONFIG_CONSOLE_USB getch(); #endif puts("\033[H\033[2JAtmel AVR Console I/O Test (" __DATE__ " " __TIME__ ")\n"); printf("\nCPU Freq:%ld Hz Compiler:%s\n\n", F_CPU, __VERSION__); // Test putch() for (i = 'A'; i < 'Z'; i++) putch(i); putch('\n'); putch('\n'); // Test cputs() for (i = 0; i < 10; i++) { sprintf(buf, "The value of the variable i is currently, now, until the next iteration, %d\n", i); cputs(buf); } // Test keypressed() and getch() cputs("\nPress ^C to continue: "); for (i = 0;;) { putch(spinner[i]); putch('\b'); if (++i == sizeof(spinner)-1) i = 0; if (keypressed()) { if (getch() == 3) break; else putch('.'); } _delay_ms(75.0); } putch('\n'); putch('\n'); // Test cputs() and cgets() for (;;) { cputs("[cgets] Enter a string: "); cgets(buf, sizeof(buf)); if (strlen(buf)) if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; if (!strcasecmp(buf, "next")) break; printf("[cgets] You entered %d bytes, '%s'\n\n", strlen(buf), buf); } // Test putchar() putchar('\n'); for (i = 'a'; i < 'z'; i++) putchar(i); putchar('\n'); putchar('\n'); // Test printf() and fgets() for (;;) { printf("[fgets] Enter a string: "); fflush(stdout); fgets(buf, sizeof(buf), stdin); if (strlen(buf)) if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; if (!strcasecmp(buf, "next")) break; printf("[fgets] You entered %d bytes, '%s'\n\n", strlen(buf), buf); } putchar('\n'); // Test scanf() for (;;) { printf("Enter two numbers: "); fflush(stdout); fflush(stdin); scanf("%d %d", &x, &y); printf("You entered %d and %d\n", x, y); if ((x == 0) && (y == 0)) break; } assert(0); exit(0); }