/*
 * Sketch skeleton for Arduino Manager for iPad / iPhone / Mac
 *
 * A simple test program to show the Arduino Manager
 * features.
 *
 * Author: Fabrizio Boco - fabboco@gmail.com
 *
 * Version: 1.0
 *
 * 11/19/2015
 *
 * All rights reserved
 *  
 */

/*
 *
 * All the IOSController libraries (“The Software”) and the related documentation (“The Documentation”) are supplied to you 
 * by the Author in consideration of your agreement to the following terms, and your use or installation of The Software and the use of The Documentation 
 * constitutes acceptance of these terms.  
 * If you do not agree with these terms, please do not use or install The Software.
 * The Author grants you a personal, non-exclusive license, under author's copyrights in this original software, to use The Software. 
 * Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by the Author, including but not limited to any 
 * patent rights that may be infringed by your derivative works or by other works in which The Software may be incorporated.
 * The Software and the Documentation are provided by the Author on an "AS IS" basis.  THE AUTHOR MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT 
 * LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE SOFTWARE OR ITS USE AND OPERATION 
 * ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
 * REPRODUCTION AND MODIFICATION OF THE SOFTWARE AND OR OF THE DOCUMENTATION, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 
 * STRICT LIABILITY OR OTHERWISE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <SPI.h>
#include "My_Adafruit_BLE_UART.h"
#include "IOSControllerForBluefruit.h"

/*
 *
 * Defines go here
 *
 *
 */
#define YELLOWLEDPIN 6
#define REDLEDPIN    7


/*
 *
 * Prototypes of IOSControllerForBluefruit’s callbacks
 *
 *
 */
void doWork();
void doSync(char *variable);
void processIncomingMessages(char *variable, char *value);
void processOutgoingMessages();
void deviceConnected();
void deviceDisconnected();


/*
 *
 * IOSControllerForForBluefruit Library initialization
 *
*/
IOSControllerForBluefruit iosController(&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,&deviceConnected,&deviceDisconnected); 
 
void setup()
{
  Serial.begin(9600);
  while(!Serial)
    ;
  
  // Your setup goes here
  pinMode(YELLOWLEDPIN, OUTPUT);
  pinMode(REDLEDPIN, OUTPUT);
  
  digitalWrite(REDLEDPIN,LOW);
  analogWrite(YELLOWLEDPIN,10);
}

void loop() {
  
  iosController.loop();
}


void doWork() {
  //Serial.println(digitalRead(REDLEDPIN));
  iosController.writeMessage("Red",digitalRead(REDLEDPIN));
}

void doSync(char *variable) {
  
  //Serial.print("Sync "); Serial.println(variable); 
  if (strcmp(variable,"Red")==0) {
    
    iosController.writeMessage(variable,map(digitalRead(REDLEDPIN),0,255,0,1023));
  }
  
  if (strcmp(variable,"Yellow")==0) {
    
    iosController.writeMessage(variable,analogRead(YELLOWLEDPIN));
  }
  
}

void processIncomingMessages(char *variable, char *value) {
  
  //Serial.print("Variable "); Serial.print(variable); Serial.print(" "); Serial.print(" Value "); Serial.println(value); 
  
  if (strcmp(variable,"Red")==0) {
    
    digitalWrite(REDLEDPIN,atoi(value));
  }
  
  if (strcmp(variable,"Yellow")==0) {
    
    analogWrite(YELLOWLEDPIN,map(atoi(value),0,1023,0,255));
  }

}

void processOutgoingMessages() {
  
  //Serial.println("Outgoing");
}

void deviceConnected() {
  
  Serial.println("Connected");
}

void deviceDisconnected() {
  
  Serial.println("Disconnected");
}

/**
*  
* Auxiliary functions
*
*/
