#include #include #include #include #include #include #include #include "serial.h" #define LCDDEV "/dev/ttyS0" #define SPO2DEV "/dev/ttyS3" #define SCALEDEV "/dev/ttyS2" #define BPDEV "/dev/ttyS1" void SendByte(int fd, unsigned char datum) { if(write(fd, &datum, 1) != 1) printf("SendByte() failed\n"); } void SendString(int fd, char *data) { while(*data) SendByte(fd, *data++); } void LCD_Write (int port_fd, int lineno, char* message) { // Input: serial port location, line number, message (20 char max) // Output: textual output to screen char buffer[30]=""; strncpy(buffer,message,20); while (strlen(buffer) < 20) strcat(buffer," "); // Writes message, per specified line number, to screen. SendByte(port_fd,17); SendByte(port_fd,0); SendByte(port_fd,lineno); SendString(port_fd,buffer); } void LCD_Clear (int port_fd) { // Clears the screen by printing four lines of spaces to screen. for (int line=0; line < 4; line++) { SendByte(port_fd,17); SendByte(port_fd,0); SendByte(port_fd,line); SendString(port_fd," "); } } int LCD_Init (void) { int fd; fd = open(LCDDEV, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror("open_port"); _exit(1); } struct termios tio; tcgetattr(fd, &tio); tio.c_cflag = CS8 | B19200 | CLOCAL; tio.c_lflag = 0; tio.c_oflag = 0; tio.c_cc[VMIN] = 1; tio.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &tio); tcflush(fd, TCIOFLUSH); // stops scrolling and wrapping SendByte(fd,20); SendByte(fd,24); return fd; } void SPO2_Get (int port_fd, int* values) { // Input: serial port location, value address // Output: accurate data from the blood oximeter unsigned char buffer[80]; for(int i = 0; i < 3; i++) { int nread = 0; do nread = read(port_fd, buffer, 1); while (nread < 0); while (nread-->0) values[i]=buffer[0]; } } int SPO2_Init (void) { int fd; fd = open(SPO2DEV, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror("open_port"); _exit(1); } struct termios tio; tcgetattr(fd, &tio); tio.c_cflag = CS8 | B9600 | CLOCAL | CREAD; tio.c_lflag = 0; tio.c_oflag = 0; tio.c_cc[VMIN] = 1; tio.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &tio); tcflush(fd, TCIOFLUSH); return fd; } SCALE_Val SCALE_Get (int port_fd) { unsigned char buffer[255]; char* data = new char[255]; SCALE_Val vals; vals.status = new char[2]; vals.weight = new char[6]; vals.units = new char[2]; int i = 0; do { int nread = 0; do nread = read(port_fd, buffer, 1 ); while (nread < 0); while(nread-->0) { data[i]=buffer[0]; if (data[i] < 43 || data[i] > 127) { printf("Non-ASCI value detected!\n"); } else i++; } } while (data[i-1] != 'b'); char* testdata = strchr(data,'S'); if (testdata == NULL) testdata = strchr(data,'U'); if (testdata == NULL) testdata = strchr(data,'O'); if (testdata != NULL) { vals.status[0]=testdata[0]; vals.status[1]=testdata[1]; vals.weight[0]=testdata[4]; vals.weight[1]=testdata[5]; vals.weight[2]=testdata[6]; vals.weight[3]=testdata[7]; vals.weight[4]=testdata[8]; vals.weight[5]=testdata[9]; vals.units[0]=testdata[10]; vals.units[1]=testdata[11]; } else { printf("Something went VERY WRONG.\n"); printf("1=%c 2=%c 3=%c 4=%c 5=%c 6=%c 7=%c 8=%c 9=%c 10=%c 11=%c 12=%c 13=%c\n", data[1],data[2],data[3],data[4],data[5],data[6],data[7], data[8],data[9],data[10],data[11],data[12],data[13]); vals.status[0]='V'; vals.status[1]='W'; } printf("0=%c 1=%c 2=%c 3=%c 4=%c 5=%c 6=%c 7=%c 8=%c 9=%c 10=%c 11=%c 12=%c 13=%c\n", data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7], data[8],data[9],data[10],data[11],data[12],data[13]); return(vals); } int SCALE_Init (void) { int fd; fd = open(SCALEDEV, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror("open_port"); _exit(1); } struct termios tio; tcgetattr(fd, &tio); tio.c_cflag = CS8 | B9600 | CLOCAL | CREAD; tio.c_lflag = 0; tio.c_oflag = 0; tio.c_cc[VMIN] = 1; tio.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &tio); tcflush(fd, TCIOFLUSH); return fd; } int BP_Init (void) { int fd; fd = open(BPDEV, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror("open_port"); _exit(1); } struct termios tio; tcgetattr(fd, &tio); tio.c_cflag = CS8 | B9600 | CLOCAL | CREAD; tio.c_lflag = 0; tio.c_oflag = 0; tio.c_cc[VMIN] = 1; tio.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &tio); tcflush(fd, TCIOFLUSH); return fd; } bool getMouseRaw() { unsigned char buffer[3]; unsigned char values[3]; int port_fd = open("/dev/psaux", O_RDWR | O_NOCTTY | O_NONBLOCK); for(int i = 0; i < 3; i++) { int nread = 0; do nread = read(port_fd, buffer, 1); while (nread < 0); while (nread-->0) values[i]=buffer[0]; } if (int(values[0]) == 9) { // NO Button Pressed return true; } else if (int(values[0]) == 10) { //YES Button Pressed return false; } printf("FAILED getMouseRaw()\n"); }