/*
--- Programme Arduino ---
Copyright Pascal ROMEYER
Date de création : 2017.07.07
http://f4cvm.free.fr 

Version 1.00

  Code sous licence GNU GPL : 
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License,
  or any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  

--- Que fait ce programme ? ---
Acquisition des donnees en I2C d'une manette de jeux (joystick) PC
raccordee via un port USB
Positionnement de 3 servo pour X - Y - Rx - Bouton de tir sur LED D13

--- Fonctionnalites utilisees ---
HobbyTronics USB Host pour joystick
www.hobbytronics.co.uk

--- Circuit / Montage à realiser ---
Raccordement de l'interface en I2C
3 servomoteurs modelisme sur PWM D9-10-11



       \\\|///
      \\ - - //
       ( @ @ )
+---oOOo-(_)-oOOo------------------------------------+
Pascal ROMEYER / F4CVM
SAINT JUST MALMONT (43) JN25DI

Email perso  : pascal.romeyer@free.fr
Email radio  : f4cvm@free.fr
Email Google : pascal.f4cvm@gmail.com

Site radio   : http://f4cvm.free.fr

     ooO  Ooo
+---(  )--(  )---------------------------------------+
    \  (  ) /
     \_) (_/

*/


#include <Wire.h>
#include <Servo.h>

Servo Direction; // Servo pour la direction
Servo Vitesse;   // Servo pour l'acceleration
Servo Etrave;    // Servo pour propulseur etrave

const int  adc_address=41;   // Adresse I2C de USB Host

unsigned char joy_data[23];   // Tableau d'acquisition des donnees Joystick (10 analog, 13 buttons)

// Variables de conversion pour les servo
int AngleDirection;
int AngleVitesse;
int AngleEtrave;

// Debattement des servo
const int AngleServoMin = 45;
const int AngleServoMax = 135;

// Valeurs suivantes a corriger pour votre manette par lecture
// des canaux analogiques sur le moniteur serie
const int JoyDirectionMin = 37;    
const int JoyDirectionMax = 205;
const int JoyVitesseMin = 54;
const int JoyVitesseMax = 218;
const int JoyEtraveMin = 78;
const int JoyEtraveMax = 205;

void setup()
{
  //Send settings to ADC_I2C device (optional)
  Wire.begin();        // join i2c bus (address optional for master)
  
  //Start Serial port
  Serial.begin(9600);  // start serial for output
  
  // Demarrage des servo
  Direction.attach(9); // servo direction PWM pin 9
  Vitesse.attach(10);   // servo vitesse PWM pin 10
  Etrave.attach(11);   // servo etrave PWM pin 11
}


// ----------------------------------------------
// Routine d'acquisition des donnees du JoyStick
//-----------------------------------------------
void get_joystick()
{
  // Get 8-bit ADC data
  // We only want single byte values (0 to 255)
  unsigned char data_values_rcvd=0;     // keep track of how many characters received    
  Wire.beginTransmission(adc_address);  // transmit to device
  Wire.write(0);                         // Start receiving data from register 0
  Wire.endTransmission();               // end transmission
  
  Wire.requestFrom(adc_address, 23);    // request all 23 bytes from slave device 
  data_values_rcvd=0; 
  while(Wire.available())
  { 
    if(data_values_rcvd < 23) joy_data[data_values_rcvd] = Wire.read(); // receive a byte as character
    data_values_rcvd++;
  }
  
}


// -------------------------
// Boucle principale
// -------------------------
void loop()
{
  unsigned char i;

  get_joystick();  // get 8-bit data

/* Affichage des donnees sur le moniteur serie
  for(i=0;i<23;i++)
  {
    Serial.print(joy_data[i], DEC);     // print the character
    Serial.print(" , ");                  // print comma
  }
  Serial.println("");                   // print carriage return  
*/

  // La gachette de tir sur la LED de l'Arduino
  if(joy_data[10]>0)  digitalWrite(13, HIGH); 
  else digitalWrite(13, LOW);
  
  // La commande de direction
  AngleDirection = map(joy_data[0], JoyDirectionMin, JoyDirectionMax, AngleServoMin, AngleServoMax);
  Direction.write(AngleDirection);
  
  // La commande de vitesse
  AngleVitesse = map(joy_data[1], JoyVitesseMin, JoyVitesseMax, AngleServoMin, AngleServoMax);
  Vitesse.write(AngleVitesse);
  
  // La commande du propulseur d'etrave
  AngleEtrave = map(joy_data[5], JoyEtraveMin, JoyEtraveMax, AngleServoMin, AngleServoMax);
  Etrave.write(AngleEtrave);

  
  
}