////////////////////////////////////////////////////////////////////////////////
// Official name:     Brains 1Sheeld Skype Test                               //
// Hardware platform: Brains                                                  //
// Pin connections:   Arduino Mega 2560                                       //
// Created:           April 2016                                              //
// Created by:        HARB                                                    //
// implemented. You could use the EEPROM to remember the last settings.       //
////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=1)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS                                                            //
// A14 = 83 = PK6 ADC14/PCINT22    =                                          //
// A15 = 82 = PK7 ADC15/PCINT23    =                                          //
// C00 =  2 = PE0 RXD0/PCINT8      = Serial monitor, also on-board LED    RX0 //
// C01 =  3 = PE1 TXD0             = Serial monitor, also on-board LED    TX0 //
// C18 = 46 = PD2 TXD1/INT3        =                                      TX1 //
// C19 = 45 = PD2 RXD1/INT2        = IR TV remote control receiver        RX1 //
// C20 = 44 = PD1 SDA/INT1         = Speed encoder Left                   TWI //
// C21 = 43 = PD0 SCL/INT0         = Speed encoder Right                  TWI //
// D13 = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low       //
// D44 = 40 = PL5 OC5C/PWM         = 3 color led Blue                         //
// D45 = 39 = PL4 OC5B/PWM         = 3 color led Red                          //
// D46 = 38 = PL3 OC5A/PWM         = 3 color led Green                        //
// D50 = 22 = PB3 MISO/PCINT3      =                                      SPI //
// D51 = 21 = PB2 MOSI/PCINT2      =                                      SPI //
// D52 = 20 = PB1 SCK/PCINT1       =                                      SPI //
// D53 = 19 = PB1 SS/PCINT0        =                                      SPI //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************

//  #define CUSTOM_SETTINGS
// Initialse conditional compiling, uncomment to include, comment to exclude ---
// #define RS232 1                 //Include RS232 sections to output debug info
//  #ifdef RS232       //Only include this part if the variable has been defined


// Define precompiler variables ------------------------------------------------
#define      CUSTOM_SETTINGS
#define    LEDonb        PB7             //Arduino boards contain an onboard LED
#define INCLUDE_SKYPE_SHIELD
// #define F_CPU    16000000                    //Speed of the connected crystal
// #define UART0_BAUD  57600                                   //UART0 baud rate

///Define the needed header files for the precompiler, no charge if not used ---
#include <OneSheeld.h>                                 //Include 1Sheeld library
//#include <IRremote.h>     //Do never use the default by the IDE but replace it
//#include <AFMotor.h>  //Motors shield, Copyright Adafruit Industries LLC, 2009
//#include <TimerOne.h>    //Currently needed for reading wheel speed per second

//DEFINE VARIABLES -------------------------------------------------------------
boolean didWeCall = false;                       //1Sheeld Define a boolean flag
unsigned int inByte = 0;                                        //Serial Monitor
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void jtag_disable(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------

void toggle_led(void) { //Toggles the default on-board LED on or off ***********
  if bit_is_clear(PORTB, LEDonb)                        //Test if the LED is off
    PORTB |= (1 << LEDonb);                        //If LED=off then turn LED on
  else                                                 //Else the LED must be on
    PORTB &= ~(1 << LEDonb);                             //Then turn the LED off
  //Or fe:     state = !state;
} //Exit toggle_led ------------------------------------------------------------


void setup() {
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  DDRB |= (1 << LEDonb);                         //Set onboard LED-pin as output
  PORTB |= (1 << LEDonb);                            //Initally onboard LED = on
  jtag_disable();              //Disable jtag to free port C, enabled by default
  OneSheeld.begin();
} //End of setup



void loop() {                                //KEEP ON RUNNING THIS LOOP FOREVER
  if (Serial.available() > 0) {           //Send data only when you receive data
    inByte = Serial.read();                             //Read the incoming byte
    Serial.print("I received: ");                             //Say what you got
    Serial.println(inByte, DEC);        //Possible translated in DEC HEX OCT BIN
    toggle_led();                   //Toggles the default on-board LED on or off
  }

  if (!didWeCall)     {                       //Call one of your Skypes contacts
    Skype.videoCall("rboek2");
    didWeCall = true;                                             //Set the flag
  } else {
    //didWeCall = false;                                        //Reset the flag
  }

} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898