#include "testApp.h"

int switchPin = 0;
int pot1Pin= 1;
int pot2Pin= 2;
int ledPin = 11;

// in this app we control the rectangle in both x and y position with pot1 and pot2

void updateRectangle(rectangle);

//--------------------------------------------------------------
void testApp::setup(){	 
	
	ofSetVerticalSync(true);
	ofBackground(255,0,130);
	
	ofSetFrameRate(60);

	
	ard.connect("/dev/tty.usbserial-A70064TO", 57600);	// AB CHANGED FROM SETUP TO CONNECT
	
	bSetupArduino	= false;							// flag so we setup arduino when its ready, you don't need to touch this :)
	bUseDelay		= true;								// this is for decimila boards.  they reset and take some time to get commands on connection
	
	// set the position of the rectangle:
	
	myRectangle.pos.x = 100;
	myRectangle.pos.y = 50;	
	
	
}

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

	
	if (((bUseDelay == true) && (ofGetElapsedTimef() > 5)) || (bUseDelay == false)){
		
		// 1st: setup the arduino if haven't already:
		if (bSetupArduino == false) setupArduino();
		
		// 2nd do the update of the arduino
		updateArduino();

	}
	
	myRectangle.pos.x = ard.getAnalog(pot1Pin);
	if (myRectangle.pos.x > ofGetWidth()){
	myRectangle.pos.x = 0;
	}

	myRectangle.pos.y = ard.getAnalog(pot2Pin);
	if (myRectangle.pos.y > ofGetHeight()){
	myRectangle.pos.y = 0;
	}


	
}

//--------------------------------------------------------------
void testApp::setupArduino(){
	// this is where you setup all the pins and pin modes, etc
	for (int i = 0; i < 13; i++)
	{
		ard.sendDigitalPinMode(i, ARD_OUTPUT);
	}


	// switch and pot pins have to be set to analog
	ard.sendAnalogPinReporting(switchPin, ARD_ANALOG);	
	ard.sendAnalogPinReporting(pot1Pin, ARD_ANALOG);	
	ard.sendAnalogPinReporting(pot2Pin, ARD_ANALOG);	

	// ledPin is output
	ard.sendDigitalPinMode(ledPin, ARD_OUTPUT);		// on diecimelia: 11 pwm?
	bSetupArduino = true;						// do this once


}

//--------------------------------------------------------------



void testApp::updateArduino(){
	

	ard.update();

	
	if (ard.getAnalog(switchPin) == 0){
		ofSetFullscreen(true);
	} else {
		ofSetFullscreen(false);
	}
}


//--------------------------------------------------------------
void testApp::draw(){
	myRectangle.draw();

}


//--------------------------------------------------------------
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){
	ard.sendDigital(ledPin, ARD_HIGH);
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
	ard.sendDigital(ledPin, ARD_LOW);
}

//--------------------------------------------------------------
void testApp::potTurn(int value){
}



