/* 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 SENSE_VIS 0
#define SENSE_IR  1

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

int count=0;

void setup()
{
  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.");
    
}

int data = 0;
int light_sensor_setup;

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 mode)
{
int command;
  
  switch (mode)
  {
    case SENSE_VIS:
    {
       command=0b11000000;      // setup (eye light sensing; one time measurement; measurement range 1)
       break;
    }

    case 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())
  { 
    return 4;  
    Serial.print(data, BIN);
  }
  Wire.endTransmission();     // stop transmitting
  light_sensor_setup=command;
}

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);
}

char *ftoa(char *a, double f, int precision)
{
  long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
  
  char *ret = a;
  long heiltal = (long)f;
  itoa(heiltal, a, 10);
  while (*a != '\0') a++;
  *a++ = '.';
  long desimal = abs((long)((f - heiltal) * p[precision]));
  itoa(desimal, a, 10);
  return ret;
}

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

char dataString[40];
char luxVISch[10];
char luxIRch[10];

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

   set_light_sensor(SENSE_IR);  // setup sensor for infrared measuring
   led_blink(); // Delay for measurement
   luxIR=get_light_measurement();
 
  ftoa(luxVISch,luxVIS,2);
  ftoa(luxIRch,luxIR,2);
  // make a string for assembling the data to log:
  sprintf(dataString,"$LUX01 %d %s %s",count, luxVISch, luxIRch);

  // 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("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.print(luxVIS,3);
    Serial.print(" ");
    Serial.println(luxIR,3);
    Serial.println(dataString);
    count++;
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }    
}