/* keyboard_read.c * * Created on: Sept. 4, 2010 * Author: Jan Axelson * * Demonstrates how to check keyboard input without blocking. * Uses the ncurses library. * This file, led_control.h, and led_control.c are at www.Lvr.com/beagleboard.htm */ // To gain access to the LEDs, run the application with administrative privileges // (From the file's directory, use "sudo ./usb_keyboard_read".) // Or set up a udev rule as described in led_control.c. #include #include #include #include #include #include "../led_control/led_control.h" void led_initialize(); void sigint_handler(int sig); /* * Checks for pending keypresses, retrieves the keys, and takes appropriate action. * The application can perform other tasks in between checking for key presses. * Ctrl+C ends the application. Uses ncurses for non-blocking keyboard input. */ int main() { led_initialize(); // Use ncurses. initscr(); // Assign a handler to reset the terminal on Ctrl+C. signal (SIGINT, ( void* ) sigint_handler); printw("Keyboard read demo.\n"); printw("Press 1 to turn on usr0. Press 0 to turn off usr0\n"); printw("Press Ctrl+C to end the program.\n"); int keypress; // getch returns immediately whether or not a key was pressed. nodelay(stdscr, TRUE); for (;;) { keypress = getch(); if (keypress == '0') { // 0 was pressed. Turn off LED 0. led_control(0, 0); } if (keypress == '1') { // 1 was pressed. Turn on LED 0. led_control(0, 1); } } // The application can perform other tasks here. // sleep(1) is a place holder for these tasks. (OK to delete.) sleep(1); return 0; } /* * This routine executes when the user presses Control + C. * Resets the terminal and exits the program. */ void sigint_handler(int sig) { endwin(); exit (sig); }