/* I2C Light Sensor

 The circuit:
 * analog sensors on analog ins PC0, PC1, and PC2
 * SD card attached to SPI bus as follows:
 ** MOSI/CMD - PB3
 ** MISO/DAT0 - PB4
 ** CLK - PB5
 ** CS/CD/DAT3 - PD4 (4)

 I2C pins PC4, PC5
         
*/

#include  <Wire.h>
#include  <math.h>
#include  <stdlib.h>
#include  <SD.h>

#define chipSelect  4

#define address 0x44 // A0 = L

#define LIGHT_ENABLE  1
#define LIGHT_DISABLE  0

#define LIGHT_MEASUREMENT_ONETIME  0
#define LIGHT_MEASUREMENT_CONTINUOUS  1

#define LIGHT_SENSE_VIS 0
#define LIGHT_SENSE_IR  1

#define LIGHT_TIMING_INTERNAL_16     0b000
#define LIGHT_TIMING_INTERNAL_12     0b001
#define LIGHT_TIMING_INTERNAL_8      0b010
#define LIGHT_TIMING_INTERNAL_4      0b011
#define LIGHT_TIMING_EXTERNAL_ADC    0b100
#define LIGHT_TIMING_EXTERNAL_TIMER  0b101

#define LIGHT_AUTORANGE  0

#define LIGHT_RANGE1  1
#define LIGHT_RANGE2  2
#define LIGHT_RANGE3  3
#define LIGHT_RANGE4  4

char filename[13];
int data = 0;
int light_sensor_setup;

void setup()
{
int count=0;

  Wire.begin(); // join i2c bus (address optional for master)

  pinMode(3, OUTPUT);  // LED pro blikani, aby bylo videt, ze to neco dela
  pinMode(5, OUTPUT);  // LED pro blikani, aby bylo videt, ze to neco dela
  Serial.begin(9600);  // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku
  
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  Serial.print("searching for files..."); 
  
  do
  {
    sprintf(filename,"Lux%d.txt",count);
    count++;
  }
  while(SD.exists(filename));     
  Serial.print("Using ");
  Serial.println(filename);
}

void led_blink()
{
   digitalWrite(3, HIGH);   // set the LED on
   delay(500);
   digitalWrite(3, LOW);    // set the LED off
   delay(500);
}

int set_light_sensor(int en, int mode, int light, int res, int range)
{
int command=0;

  commad= (en << 7 & 0b10000000);
  
  switch (light)
  {
    case LIGHT_SENSE_VIS:
    {
       command=0b11000000;      // setup (eye light sensing; one time measurement; measurement range 1)
       break;
    }

    case LIGHT_SENSE_IR:
    {
       command=0b11100000;      // setup (eye light sensing; measurement range 2 [4000 lx])
       break;
    }
   
    default:
     return 3;   
  }

  // Setup device
  Wire.beginTransmission(address); 
  Wire.send(0x00);            // sends address
  Wire.send(command);      // setup (eye light sensing; one time measurement; measurement range 1)
  Wire.endTransmission();     // stop transmitting
  
  
   //  Connect to device and set register address
  Wire.beginTransmission(address); 
  Wire.send(0x00);            // sends address (command register)
  Wire.endTransmission();     // stop transmitting
  
  //  verify written command byte
  Wire.beginTransmission(address);  
  Wire.requestFrom(address, 1);
  if (command != Wire.receive())
  { 
    Serial.println(data, BIN);
    return 4;  
  }
  Wire.endTransmission();     // stop transmitting
  light_sensor_setup=command;
  return 0;
}

float get_light_measurement()
{
int ret=0;

   //  Connect to device and set register address
   Wire.beginTransmission(address); 
   Wire.send(0x01);            // sends address of LSB reagister 
   Wire.endTransmission();     // stop transmitting
   
   //  Connect to device and request one byte
   Wire.beginTransmission(address);
   Wire.requestFrom(address, 1);
   ret = Wire.receive();
   Wire.endTransmission();     // stop transmitting
   
   //  Connect to device and set register address
   Wire.beginTransmission(address);
   Wire.send(0x02);            // sends address of MSB register
   Wire.endTransmission();     // stop transmitting
   
   //  Connect to device and request one byte
   Wire.beginTransmission(address);
   Wire.requestFrom(address, 1);
   ret +=256 * Wire.receive();
   Wire.endTransmission();     // stop transmitting

   return (1000.0/pow(2.0,16)*ret);
}

void loop()
{
float luxVIS=0;
float luxIR=0;
float time;

   set_light_sensor(LIGHT_SENSE_VIS);  //setup sensor for visible measuring
   led_blink();    // Delay for measurement
   luxVIS=get_light_measurement();

   time=millis()/1000.0;
   
   set_light_sensor(LIGHT_SENSE_IR);  // setup sensor for infrared measuring
   led_blink(); // Delay for measurement
   luxIR=get_light_measurement();
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open(filename, FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print("$LUX0.1 ");
    dataFile.print(time,3);
    dataFile.print(" ");
    dataFile.print(luxVIS,3);
    dataFile.print(" ");
    dataFile.println(luxIR,3);
    dataFile.close();
    
    // print to the serial port too:
    Serial.print("$LUX0.1 ");
    Serial.print(time,3);
    Serial.print(" ");
    Serial.print(luxVIS,3);
    Serial.print(" ");
    Serial.println(luxIR,3);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.print("error opening ");
    Serial.println(filename);
  }    
}