#include "testApp.h"

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


// in this app, the led is doing cosine first by controling the phase with pot1, then by controlling the increase of ohase with pot1

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

	
	// phase =  (potValue/1024*2*pi); // try these lines one after the other
	// phase +=  (potValue/1024*2*pi); 
	
	printf("phase = %f \n", phase);

	
}

//--------------------------------------------------------------
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_PWM);		// on diecimelia: 11 pwm?
	bSetupArduino = true;						// do this once


}

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



void testApp::updateArduino(){
	

	ard.update();

	
	// update the arduino, get any data or messages: 
	  
	if (ard.getAnalog(switchPin) == 0){
		ofSetFullscreen(true);
	} else {
		ofSetFullscreen(false);
	}

		
	ard.sendPwm(ledPin, cos(phase)*255); 

		
}


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

	ofFill();
	ofSetColor(0xeeeeee);
	ofCircle(100,100,50);
	
	ofNoFill();
	ofSetColor(0,0,0);
	ofCircle(100,100,50);
	
	ofFill();
	ofSetColor(0,0,0);
	ofCircle(100 + 50 * cos(phase), 100 + 50 * sin(phase) * -1, 3);    // we do -1 here because the top point is 0,0....
	
	ofEnableAlphaBlending();
	ofSetColor(255,0,255, 30);
	ofTriangle(100,100,100 + 50 * cos(phase), 100, 100 + 50 * cos(phase), 100 + 50 * sin(phase) * -1);
	ofSetColor(0,130,255, 30);
	ofTriangle(100,100,100, 100 + 50 * sin(phase) * -1, 100 + 50 * cos(phase), 100 + 50 * sin(phase) * -1);
	ofDisableAlphaBlending();
}


//--------------------------------------------------------------
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){
}

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





