// g++ -std=c++11 -Wall -pthread -ggdb -o comm -lbluetooth main.cpp && ./comm

#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include "virtualslave.hpp"
#include "master.hpp"

int main()
{
	srand(time(0));
	bool seqTest = true;
	//VirtualCommSlave s;
	//CommMaster m("/dev/ttyUSB0", 80, 10);
	CommMaster m("/dev/rfcomm1", 500, 10);
	//CommMaster m("20:15:05:11:28:40", 150, 5);
	//s.connect(&m);
	//m.connect(&s);
	/*unsigned maxtries = 0*10e3;
	unsigned tries = maxtries;
	unsigned retries = 0;
	if (tries) {
		m.bytelost_ppm = 50000;
		m.bytealtered_ppm = 50000;
		fclose(stdout);
		while(tries--) {
			do {
				m.gotoXY(-12,24);
				retries++;
			} while(m.sequence != m.sequenceAck);
		}
		fprintf(stderr, "%1.3f retries\n", (float)(retries-maxtries)/maxtries);
		return 0;
	}*/
	struct timespec dt;
	dt.tv_sec = 1;
	dt.tv_nsec = 0;
	while(nanosleep(&dt,&dt)<0 && errno==EINTR);
	m.setPos(-9, 18);
	if (seqTest && !m.ack()) return __LINE__;
	dt.tv_sec = 1;
	dt.tv_nsec = 0;
	while(nanosleep(&dt,&dt)<0 && errno==EINTR);
	m.movedXdY(-150, 300);
	if (seqTest && !m.ack()) return __LINE__;
	dt.tv_sec = 1;
	dt.tv_nsec = 0;
	while(nanosleep(&dt,&dt)<0 && errno==EINTR);
	return 0;
	m.gotoXY(-12,24);
	if (seqTest && !m.ack()) return __LINE__;
	m.deletePoints();
	if (seqTest && !m.ack()) return __LINE__;
	m.stopMove();
	if (seqTest && !m.ack()) return __LINE__;
	m.startMove();
	if (seqTest && !m.ack()) return __LINE__;
	m.gotoXY(-12,24);
	if (seqTest && !m.ack()) return __LINE__;
	m.movedXdY(-15, 30);
	if (seqTest && !m.ack()) return __LINE__;
	m.gotoNextPoint();
	if (seqTest && !m.ack()) return __LINE__;
	m.setNPoint(66);
	if (seqTest && !m.ack()) return __LINE__;
	m.setPos(-9, 18);
	if (seqTest && !m.ack()) return __LINE__;
	uint8_t xys[34] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 };
	m.addPoints(xys, 0);
	if (seqTest && !m.ack()) return __LINE__;
	m.addPoints(xys, 1);
	if (seqTest && !m.ack()) return __LINE__;
	m.addPoints(xys, 2);
	if (seqTest && !m.ack()) return __LINE__;
	m.addPoints(xys, 15);
	if (seqTest && !m.ack()) return __LINE__;
	m.addPoints(xys, 16);
	if (seqTest && !m.ack()) return __LINE__;
	m.addPoints(xys, 17);
	if (seqTest && !m.ack()) return __LINE__;
	printf("%ld, %ld, %ld\n", m.qosMsg, m.qosSync, m.qosFullSync);
	return 0;
}
