/******************************************************************************
 * SparkFun_VL6180X_demo.ino
 * Example Sketch for VL6180x time of flight range finder.
 * Casey Kuhns @ SparkFun Electronics
 * 10/29/2014
 * https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library
 *
 * The VL6180x by ST micro is a time of flight range finder that
 * uses pulsed IR light to determine distances from object at close
 * range.  The average range of a sensor is between 0-200mm
 *
 * Resources:
 * This library uses the Arduino Wire.h to complete I2C transactions.
 *
 * Development environment specifics:
 * 	IDE: Arduino 1.0.5
 * 	Hardware Platform: Arduino Pro 3.3V/8MHz
 * 	VL6180x Breakout Version: 1.0
 *  **Updated for Arduino 1.6.4 5/2015**
 *
 * This code is beerware. If you see me (or any other SparkFun employee) at the
 * local pub, and you've found our code helpful, please buy us a round!
 *
 * Distributed as-is; no warranty is given.
 ******************************************************************************/

#define DEBUG_MODULE "SparkFun_VL6180X"

#include "debug.h"
#include "eprintf.h"

#include <i2cdev.h>

#include <stdio.h>

#include <SparkFun_VL6180X.h>

#include <SparkFun_VL6180X_Init.h>

/*const float GAIN_1    = 1.01;  // Actual ALS Gain of 1.01
const float GAIN_1_25 = 1.28;  // Actual ALS Gain of 1.28
const float GAIN_1_67 = 1.72;  // Actual ALS Gain of 1.72
const float GAIN_2_5  = 2.6;   // Actual ALS Gain of 2.60
const float GAIN_5    = 5.21;  // Actual ALS Gain of 5.21
const float GAIN_10   = 10.32; // Actual ALS Gain of 10.32
const float GAIN_20   = 20;    // Actual ALS Gain of 20
const float GAIN_40   = 40;    // Actual ALS Gain of 40
*/
#define VL6180X_ADDRESS 0x29

//void getIdentification(struct VL6180xIdentification *temp);

//#define VL6180xIdentification (identification);//
//#define VL6180x sensor(VL6180X_ADDRESS);//

void setup() {

  //Serial(115200); //Start Serial at 115200bps
  //begin(); //Start I2C library
//  usleep(100); // delay .1s

  getIdentification(); // Retrieve manufacture info from device memory
  printIdentification(); // Helper function to print all the Module information

    if(VL6180xInit() != 0){
    DEBUG_PRINT("FAILED TO INITALIZE"); //Initialize device and check for errors
  };

  VL6180xDefaultSettings(); //Load default settings to get started.

    usleep(1000); // delay 1s


}

void loop() {

  //Get Ambient Light level and report in LUX
  eprintf("Ambient Light Level (Lux) = ");

  //Input GAIN for light levels,
  // GAIN_20     // Actual ALS Gain of 20
  // GAIN_10     // Actual ALS Gain of 10.32
  // GAIN_5      // Actual ALS Gain of 5.21
  // GAIN_2_5    // Actual ALS Gain of 2.60
  // GAIN_1_67   // Actual ALS Gain of 1.72
  // GAIN_1_25   // Actual ALS Gain of 1.28
  // GAIN_1      // Actual ALS Gain of 1.01
  // GAIN_40     // Actual ALS Gain of 40

  eprintf(getAmbientLight(GAIN_1) );

  //Get Distance and report in mm
  DEBUG_PRINT("Distance measured (mm) = ");
  eprintf(getDistance() );

  usleep(500);
};

void printIdentification(struct VL6180xIdentification *temp){
  DEBUG_PRINT("Model ID = ");
  eprintf(temp->idModel);

  DEBUG_PRINT("Model Rev = ");
  eprintf(temp->idModelRevMajor);
  DEBUG_PRINT(".");
  eprintf(temp->idModelRevMinor);

  DEBUG_PRINT("Module Rev = ");
  eprintf(temp->idModuleRevMajor);
  DEBUG_PRINT(".");
  eprintf(temp->idModuleRevMinor);

  DEBUG_PRINT("Manufacture Date = ");
  DEBUG_PRINT((temp->idDate >> 3) & 0x001F);
  DEBUG_PRINT("/");
  DEBUG_PRINT((temp->idDate >> 8) & 0x000F);
  DEBUG_PRINT("/1");
  DEBUG_PRINT((temp->idDate >> 12) & 0x000F);
  DEBUG_PRINT(" Phase: ");
  eprintf(temp->idDate & 0x0007);

  DEBUG_PRINT("Manufacture Time (s)= ");
  eprintf(temp->idTime * 2);
  DEBUG_PRINT();
  //Serial();
}
