#include <fcntl.h>
#include <sys/ioctl.h>

int main() {
	int c, res;
	struct termios tty; // will be used for new port settings
	char buf[255];      // buffer used to store received characters

	// set up serial port
	fd_serial = open("/dev/ttyS0", O_RDWR | O_NOCTTY ); 
	tcgetattr(fd_serial, &oldtty); // save current port settings
	bzero(&tty, sizeof(tty));
	tty.c_iflag = 0; tty.c_lflag = 0; tty.c_oflag = 0; tty.c_cc[VTIME] = 0; tty.c_cc[VMIN] = 1;
	tty.c_cflag = BAUDRATE | CS8 | CLOCAL;
	tcflush(fd_serial, TCIFLUSH);
	tcsetattr(fd_serial, TCSANOW, &tty);
	
	unsigned char out[6];
	out[0] = 0x80; // start byte
	out[1] = 0x01; // deviceid (1=servo)
	out[2] = 0x04; // 2=7bit position
	out[3] = SERVO_ARM;
	// min: 0x0374   max: 0x4a7c
	out[4] = (char)(0x03 + ((0x4a - 0x03) * (float)((float)percent/100.0))); // max 0x03-0x4a
	out[5] = 0x74;

	write(fd_serial, val, sizeof(out));
	return 1;
}

