// howto compile // gcc -lm servoserialdriver.c mini_ssc.o -o servoserialdriver // mkfifo /dev/pwm0 // chmod a+w /dev/pwm0 #include #include #include "mini_ssc.h" #include #include #include #include #include #include #include #include // range used to be 73 to 238 fd_set rfds; // globally defined structure for file descriptor select int openfifo() { int voht_pin_fifo; /* open the fifo for reading */ //voht_pin_fifo = open("/dev/voht_pin", O_RDONLY | O_SYNC); /* RDWR seems to keep it from looping too much */ voht_pin_fifo = open("/dev/pwm0", O_RDWR | O_SYNC); if( voht_pin_fifo == -1 ) { perror("open");exit(1); } FD_ZERO(&rfds); FD_SET(voht_pin_fifo, &rfds); return voht_pin_fifo; } int readfromfifo(int fd) { char buffer[1024]; int security_from_buffer_overflow = 0; while( select( fd+1, &rfds, NULL, NULL, NULL)==1) { int numread; int i, j; numread = read( fd, &(buffer[j++]), 1); printf("got %d:%c\n", numread, buffer[j-1]); if( buffer[j-1] == '\n' ) { // keeps reading till gets to a newline j=0; i=0; break; } security_from_buffer_overflow++; if (security_from_buffer_overflow > 80) exit(1); } return atoi(buffer); } int main(int argc,char **argv) { int port, baudrate, servo, min, max, delay, step; int fd; // file descriptor for serial port char *s; int i,n,z; int x_in, y_in; int x_out, y_out; // defaults port = 0; baudrate = 2400; // connect to the ssc if(ssc_open(port,baudrate)) perror("failed to open serial port"); fd = openfifo(); while (1) { // x_in = atoi(argv[1]); // change this to read from fifo: x_in = readfromfifo(fd); x_in = x_in + 55; if (x_in > 254){ x_in = 254; fprintf(stderr,"input greater than 254 is clipped to 254\n"); fprintf(stderr,"you should enter only numbers from 0 to 199\n"); } if (x_in < 22){ x_in = 22; fprintf(stderr,"input less than 22 damages servo, so clipped to 22\n"); fprintf(stderr,"you should enter only numbers from 0 to 199\n"); } if (x_in < 55){ x_in = 55; fprintf(stderr,"input less than 55 damages mount, so clipped to 55\n"); fprintf(stderr,"you should enter only numbers from 0 to 199\n"); } // enter infinite loop that translates pairs of screen coordinates // to pairs of telepointer coodinates. ssc_move( 0, x_in ); fprintf(stderr,"%d\n", x_in); } }