#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h>   // time calls
#include <sys/ioctl.h>


int
main()
{
        int nbytes = -1, fd = -1, ret = -1;
        unsigned int status;
        struct termios options;
        char buf[1024];
        fd_set rfds;


        while(1) {
                fd = open("/dev/tty00", O_RDONLY | O_NOCTTY | O_NDELAY);
                if (fd == -1)
                {
                        perror("open(2) on serial port");
                        exit(128);
                } else {
                        fcntl(fd, F_SETFL, 0);
                }
                /* get the current options */
                tcgetattr(fd, &options);

                /* set raw input, 1 second timeout */
                options.c_cflag     |= (CLOCAL | CREAD);
                options.c_lflag     &= ~(ICANON | ECHO | ECHOE | ISIG);
                options.c_oflag     &= ~OPOST;
                options.c_iflag     &= ~(IXON | IXOFF | IXANY);
                options.c_cc[VMIN]  = 0;
                options.c_cc[VTIME] = 10;

                tcsetattr(fd, TCSAFLUSH, &options);
                memset(&status, 0, sizeof(unsigned int));
                ioctl(fd, TIOCMGET, &status);
                if(status & TIOCM_CTS) /* XXX first pedal pressed */
                        printf("First pedal pressed!\n");
                if(status & TIOCM_DSR) /* XXX middle pedal pressed */
                        printf("Middle pedal pressed!\n");
                if(status & TIOCM_CD) /* XXX middle pedal pressed */
                        printf("Third pedal pressed!\n");

                close(fd);
        }

}