/*
 SerialLCD Library - Hello World Combined with SparkFun RealTime Clock

 Demonstrates the use a 16x2 LCD SerialLCD driver from Seeedstudio and a Real Time Clock from Sparkfun
 Any corrections comments please send to: louiskatz -@- yahoo

*below are credits from code used for each board
Library originally added 16 Dec. 2010
 by Jimbo.we 
 http://www.seeedstudio.com

 * Credit to:
 * Maurice Ribble - http://www.glacialwanderer.com/hobbyrobotics for RTC DS1307 code
 * BB Riley - Underhill Center, VT  <brianbr@wulfden.org> For simplification of the Day of Week and month and updating to Arduino 1.0 
 * peep rada - from Arduino Forum - Found that only 32 registers per I2C connection was possible 
 * 
 * With this code you can set the date/time, retreive the date/time and use the extra memory of an RTC DS1307 chip.  
 * The program also sets all the extra memory space to 0xff.
 * Serial Communication method with the Arduino that utilizes a leading CHAR for each command described below. 
 *
 * Commands:
 * T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) -
 * T - Sets the date of the RTC DS1307 Chip. 
 * Example to set the time for 25-Jan-2012 @ 19:57:11 for the 4 day of the week, use this command - T1157194250112
 * Make sure to set serial recieve speed to 57600 baud
 * Q(1-2) - (Q1) Memory initialization  (Q2) RTC - Memory Dump  
 * R - Read/display the time, day and date
 Clock Chip is plugged in to A4 and A5
 Seeed Studio LCD is pluggedd into Digital D6 and D7
 */
 //Clock STuff
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
// Arduino version compatibility Pre-Compiler Directives
#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
  #define I2C_WRITE Wire.write 
  #define I2C_READ Wire.read
#else                                   // Arduino Prior to v1.0 
  #define I2C_WRITE Wire.send 
  #define I2C_READ Wire.receive
#endif
// Global Variables
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
float fsecond, fminute, fhour, fdayOfWeek, fdayOfMonth, fmonth, fyear;
byte test;
byte zero;
char  *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char  *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

// include the library code for LCD:
#include <SerialLCD.h>//this is a must for LCD
#include <SoftwareSerial.h> //this is a must for LCD

// initialize the library
SerialLCD slcd(6,7);//this is a must, assign soft serial pins for LCD (Digital pins)
//Functions for Clock
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.

void setDateDs1307()                
{

   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
   minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   dayOfWeek = (byte) (Serial.read() - 48);
   dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   I2C_WRITE(zero);
   I2C_WRITE(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
   I2C_WRITE(decToBcd(minute));
   I2C_WRITE(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   I2C_WRITE(decToBcd(dayOfWeek));
   I2C_WRITE(decToBcd(dayOfMonth));
   I2C_WRITE(decToBcd(month));
   I2C_WRITE(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(I2C_READ() & 0x7f);
  minute     = bcdToDec(I2C_READ());
  hour       = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month      = bcdToDec(I2C_READ());
  year       = bcdToDec(I2C_READ());

  if (hour < 10)

   Serial.print("0");
  Serial.print(hour, DEC);
  Serial.print(":");
  if (minute < 10)
    Serial.print("0");
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
    Serial.print("0");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(Day[dayOfWeek]);
  Serial.print(", ");
  Serial.print(dayOfMonth, DEC);
  Serial.print(" ");
  Serial.print(Mon[month]);
  Serial.print(" 20");
  if (year < 10)
    Serial.print("0");
  Serial.println(year, DEC);

}
void setup() {
  // set up
  slcd.begin();
  // Print a message to the LCD.
  //slcd.print("hello, world!");
  Wire.begin();
  Serial.begin(9600); 
  zero=0x00;
}

void loop() {
 //Set Date From Serial Monitor
   if (Serial.available()) {      // Look for char in serial que and process if found
      command = Serial.read();
      if (command == 84) {      //If command = "Tt" Set Date
       setDateDs1307();delay (1000);}}

  slcd.clear(); //clears display
  slcd.setCursor(0, 0);//sets cursor to row 1 space 1
  getDateDs1307();//gets date from clock chip
  fsecond= (float) second;fminute= (float) minute;fhour= (float) hour; fdayOfWeek= (float)dayOfWeek; 
  fdayOfMonth= (float)dayOfMonth; fmonth= (float) month; fyear= (float) year;//converts byte type varailabes to float
 if (hour < 10)
    slcd.print("0");//leading zero for hours 0-9
  slcd.print(fhour, DEC);
  slcd.print(":");
  if (minute < 10)
    slcd.print("0");
  slcd.print(fminute, DEC);
  slcd.print(":");
  if (second < 10)
    slcd.print("0");
  slcd.print(fsecond, DEC);
  //slcd.print("  ");
  //slcd.print(fdayOfWeek,DEC);
  slcd.setCursor(0, 1);
  slcd.print("Y-20");
  if (year < 10)
    slcd.print("0");
  slcd.print(fyear, DEC);
  slcd.print(" M-");
  slcd.print(fmonth, DEC);
  slcd.print(" D-");
  slcd.print(fdayOfMonth, DEC);
 delay (500); //might not be needed.

}
//*****************************************************The End***********************/