#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	bSendSerialMessage = false;
	ofBackground(255,255,255);	
	
	
	serial.enumerateDevices();
			
	
	//----------------------------------- note:
	serial.setup(0, 9600);		// < this should be set
										// to whatever com port
										// your serial device is 
										// connected to.  
										// (ie, COM4 on a pc, dev/tty.... on a mac)
										// arduino users check in arduino app....
	//----------------------------------- 
	font.loadFont("DIN.otf",64);
	nTimesRead = 0;
	nBytesRead = 0;
	readTime = 0;
	memset(bytesReadString, 0, 4);
}

//--------------------------------------------------------------
void testApp::update(){
	
	if (bSendSerialMessage){
		
		// (1) write the letter "a" to serial:
		serial.writeByte('a');
		
		// (2) read
		// now we try to read 3 bytes
		// since we might not get them all the time 3 - but sometimes 0, 6, or something else,
		// we will try to read three bytes, as much as we can
		// otherwise, we may have a "lag" if we don't read fast enough
		// or just read three every time. now, we will be sure to 
		// read as much as we can in groups of three...

		nTimesRead = 0;
		nBytesRead = 0;
		int nRead;  // a temp variable to keep count per read
		memset(bytesReadString, 0, 4);
		
		string bytesReturned 	= "";
		string latestBytes  	= "";
		while((bytesReturned = serial.readBytes(3)).length() > 0){
			nTimesRead++;	
			nBytesRead = bytesReturned.length();
			latestBytes = bytesReturned;
		};
		
		
		sprintf(bytesReadString, "%s", latestBytes.c_str());
		

		bSendSerialMessage = false;
		readTime = ofGetElapsedTimef();
	}
}

//--------------------------------------------------------------
void testApp::draw(){

	char tempStr[1024];
	sprintf(tempStr, "click to test serial:\nnBytes read %i\nnTimes read %i\nread: %s\n(at time %0.3f)", nBytesRead, nTimesRead, bytesReadString, readTime);
	
	if (nBytesRead > 0 && ((ofGetElapsedTimef() - readTime) < 0.5f)){
		ofSetColor(0x000000);
	} else {
		ofSetColor(0xdddddd);
	}
	font.drawString(tempStr, 50,100);

}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	bSendSerialMessage = true;
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
}
