#include "testApp.h"

int switchPin = 0;
int pot1Pin= 1;
int servo1Pin = 9;
int servo2Pin = 10;
int ledPin = 11;

void testApp::potTurn(int value);

// in this app, we control the turning of the servo by turning pot1, and also, by dragging the mouse in the x axis.

//--------------------------------------------------------------
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
	
	ofEnableSmoothing();



} 


//--------------------------------------------------------------
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();

	}
	float potValue = ard.getAnalog(pot1Pin);


	
}

//--------------------------------------------------------------
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);
	}
	
	// servo pins have to be set to PWM
	ard.sendDigitalPinMode(9, ARD_PWM); 
	
	// 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_PWM);		// on diecimelia: 11 pwm?
	bSetupArduino = true;						// do this once
	
	// attach servos
	ard.sendServoAttach(servo1Pin) ;

}

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



void testApp::updateArduino(){
	

	ard.update();

	
	// update the arduino, get any data or messages: 

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

	potTurn(ard.getAnalog(pot1Pin));
		
}


//--------------------------------------------------------------
void testApp::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){
		float pct = (float)x / (float) ofGetWidth();
		ard.sendServo(9, (int)(pct*180.0f), true);
}

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

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

//--------------------------------------------------------------
void testApp::potTurn(int value){
	float perc = (float) value/ 1024;
	ard.sendServo(9, (int)(perc*180.0f), true);
}






