/*
   Copyright (C) 2012 Nathanaël Restori

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without
   restriction, including without limitation the rights to use, copy,
   modify, merge, publish, distribute, sublicense, and/or sell copies
   of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.

*/

#include <SD.h>
#include <Wire.h>
#include "BMP085.h"
#include "Chronodot.h"
#include "DHT.h"
#include "TSL2561.h"

#define TIMING  1000 // milliseconds
#define DHTPIN  2
#define DHTTYPE DHT22
#define SDPIN   4

BMP085    bmp;
DHT       dht(DHTPIN, DHTTYPE);
TSL2561   tsl(TSL2561_ADDR_FLOAT);
Chronodot chronodot;
File      file;

void setup() {
    Serial.begin(9600);

    //----- SD Card
    Serial.println("INIT SD Card");

    if (!SD.begin(4)) {
        Serial.println("FAILED");
        return;
    }
    Serial.println("DONE");

    //----- Sensors
    Serial.println("INIT Sensors");

    bmp.begin();
    chronodot.begin();
    dht.begin();
    tsl.begin();

    //tsl.setGain(TSL2561_GAIN_0X); // bright situtations
    tsl.setGain(TSL2561_GAIN_16X);  // dim situations

    tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS);    // bright light
    //tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium light
    //tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // dim light

    if (!SD.exists("data.tsv")) {
        file = SD.open("data.tsv", FILE_WRITE);

        if (file) {
            Serial.print("Creating data.tsv");
            file.println("#Time\t\t\tT [°C]\tP [Pa]\tAlt [m]\tHr [%]\t"
                    "Lx [lux]");
            file.close();
            Serial.println("DONE");
        } else {
            Serial.println("Error opening data.tsv");
        }
    }
}

void loop() {
    float   temperature = bmp.readTemperature();
    int32_t pressure    = bmp.readPressure();
    float   altitude    = bmp.readAltitude();

    float humidity = dht.readHumidity();

    uint32_t lum = tsl.getFullLuminosity();
    uint16_t lightIr   = lum >> 16;
    uint16_t lightFull = lum & 0xFFFF;

    DateTime time = chronodot.now();

    file = SD.open("data.tsv", FILE_WRITE);

    if (file) {
        Serial.print("Writing to data.tsv...");
        file.print(time.year());   file.print("-");
        file.print(time.month());  file.print("-");
        file.print(time.day());    file.print(" ");
        file.print(time.hour());   file.print(":");
        file.print(time.minute()); file.print(":");
        file.print(time.second()); file.print("\t");

        file.print(temperature); file.print("\t");
        file.print(pressure);    file.print("\t");
        file.print(altitude);    file.print("\t");
        file.print(humidity);    file.print("\t");
        file.println(tsl.calculateLux(lightFull, lightIr));
        file.close();
        Serial.println("DONE");
    } else {
        Serial.println("Error opening data.tsv");
    }

    delay(TIMING);
}
