#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	bSendSerialMessage = false;
	ofBackground(255,255,255);	
	
	
	serial.enumerateDevices();
			
	
	//----------------------------------- note:
	serial.setup("COM3", 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...


//char		bytesRead[3];				// data from serial, we will be trying to read 3
//char		bytesReadString[4];			// a string needs a null terminator, so we need 3 + 1 bytes
		

		nTimesRead = 0;
		nBytesRead = 0;
		int nRead;  // a temp variable to keep count per read
		memset(bytesReadString, 0, 4);  // set all the chars to '0'
		
		int howMuchDidWeRead = 0;
		
		while((howMuchDidWeRead = serial.readBytes(bytesRead, 3)) > 0){
			nTimesRead++;	
			memcpy(bytesReadString, bytesRead, 3);  // copy these three bytes in...
			nBytesRead = howMuchDidWeRead;
		};
		
		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::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(){
}
