/*
   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 <Wire.h>
#include "Chronodot.h"

#define CHRONODOT_ADDRESS 0x68

static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }

// no-cost stream operator as described at
// http://sundial.org/arduino/?page_id=119
    template<class T>
inline Print &operator <<(Print &obj, T arg)
{ obj.print(arg); return obj; }

    template<class T>
inline Print &operator >>(Print &obj, T arg)
{
    for (unsigned int mask = 0x80; mask; mask >>= 1) {
        obj.print(mask&arg?'1':'0');
    }
    return obj;
}

    template<class T>
inline Print &operator +(Print &obj, T arg)
{ obj.print(arg, HEX); return obj; }

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

    Wire.begin();

    Serial.println("BEGIN transmission");

    Wire.beginTransmission(CHRONODOT_ADDRESS);
    Wire.write((byte)0);
    Wire.endTransmission();

    Wire.requestFrom(CHRONODOT_ADDRESS, 7);
    byte blah[8];
    int i;
    for(i=0; i<8; i++) {
        blah[i] = Wire.read();
    }

    Serial.println("END transmission");

    uint8_t ss = bcd2bin(blah[0] & 0x7F);
    uint8_t mm = bcd2bin(blah[1]);
    uint8_t hh = bcd2bin(blah[2]);
    uint8_t d  = bcd2bin(blah[4]);
    uint8_t m  = bcd2bin(blah[5]);
    uint16_t y = bcd2bin(blah[6]) + 2000;

    Serial.println();
    Serial.println("What\tByte\tCorrespondance");
    Serial.println("------------------------------");

    Serial
        << "seconds\t" << blah[0]  << "\t" << ss      << "\r\n"
        << "minutes\t" << blah[1]  << "\t" << mm      << "\r\n"
        << "hours\t"   << blah[2]  << "\t" << hh      << "\r\n"
        << "days\t"    << blah[4]  << "\t" << d       << "\r\n"
        << "months\t"  << blah[5]  << "\t" << m       << "\r\n"
        << "years\t"   << blah[6]  << "\t" << y       << "\r\n";

    Serial.println();
    Serial
        << y  << "-" << m  << "-" << d  << " "
        << hh << ":" << mm << ":" << ss << "\r\n";

    Serial.println();
    Serial.println("Address\tDecimal\tBinary");
    Serial.println("----------------------");
    Serial + CHRONODOT_ADDRESS << "\t" << CHRONODOT_ADDRESS << "\t" >> CHRONODOT_ADDRESS << "\r\n";

    Serial.println();
    Serial.println("Byte\tDecimal\tBinary");
    Serial.println("----------------------");

    Serial
        << "0\t"  << blah[0]  << "\t" >> blah[0]  << "\r\n"
        << "1\t"  << blah[1]  << "\t" >> blah[1]  << "\r\n"
        << "2\t"  << blah[2]  << "\t" >> blah[2]  << "\r\n"
        << "3\t"  << blah[3]  << "\t" >> blah[3]  << "\r\n"
        << "4\t"  << blah[4]  << "\t" >> blah[4]  << "\r\n"
        << "5\t"  << blah[5]  << "\t" >> blah[5]  << "\r\n"
        << "6\t"  << blah[6]  << "\t" >> blah[6]  << "\r\n"
        << "7\t"  << blah[7]  << "\t" >> blah[7]  << "\r\n";
}

void loop() {
    delay(1000);
}
