////////////////////////////////////////////////////////////////////////////////
// Name:       Gamepad                                                        //
// Platform:   Arduino Mega 2560 / 1Sheeld                                    //
// Created by: HARB                                                           //
// 1Sheeld Gamepad sketch.                                                    //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************
// 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                                     //Needed for 1Sheeld
#define INCLUDE_SKYPE_SHIELD                                //Needed for 1Sheeld
#define INCLUDE_GAMEPAD_SHIELD                              //Needed for 1Sheeld

///Define the needed header files for the precompiler, no charge if not used ---
#include <OneSheeld.h>                                 //Include 1Sheeld library
bool    ledBinVal = LOW;                      //You can chose HIGH-on or LOW-off
bool    didWeCall = false;                       //1Sheeld Define a boolean flag
bool    flagActive = false;  //Needed to be able to sent every command just once
bool    flagSent   = false;         //Needed to sent halt just once after relief
char    cmd1  = '0';                                  //Set halt command to send
unsigned int inByte = 0;                                        //Serial Monitor
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() {
  jtag_disable();              //Disable jtag to free port C, enabled by default
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  Serial3.begin(9600);          //Nothing more needed for serial ttl to function
  OneSheeld.begin();
  delay(100);
} //End of setup



void loop() {                                //KEEP ON RUNNING THIS LOOP FOREVER
  if (flagSent){
  	cmd1 = '0';
  	flagActive = true;
  	flagSent   = false;
  }
  if (GamePad.isUpPressed()) {                 //Send the g = Go command = Green
    cmd1 = 'g';
    flagActive = true;
    flagSent = true;
  }
  if (GamePad.isDownPressed()) {              //Send the b = Back command = Blue
    cmd1 = 'b';
    flagActive = true;
    flagSent = true;    
  }
  if (GamePad.isRightPressed()) {             //Send the r = Right command = Red
    cmd1 = 'r';
    flagActive = true;
    flagSent = true;    
  }
  if (GamePad.isLeftPressed()) {             //Send the l = Left command = Lemon
    cmd1 = 'l';
    flagActive = true;
    flagSent = true;    
  }
  
  if (flagActive){
     Serial3.print(cmd1);                                    //Command is 1 byte
     flagActive = false;
     delay(80);                     //Needed for GamePad.isUpPressed() min is 80
  }
  toggle_ledBin();                  //Toggles the default on-board LED on or off
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


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_ledBin(void) { //Toggles the default on-board LED_BUILT_IN on or off
  ledBinVal = !ledBinVal;                                         //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------

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