//#include <avr/io.h>
#include "mark.h" 
#include <util/delay.h>
#define RTC_I2C_ADDRESS 0x68
void i2c_init() {
    TWBR = 32; // also 32 -Set bit rate for ~100 kHz with 16 MHz clock
    TWSR = 0;  // Set prescaler to 1
}

// I2C Start Condition
void i2c_start() {
    TWCR = (1 << TWSTA) | (1 << TWEN) | (1 << TWINT); // Enable START condition
    while (!(TWCR & (1 << TWINT))); // Wait for TWINT flag set
}

// I2C Stop Condition
void i2c_stop() {
    TWCR = (1 << TWSTO) | (1 << TWEN) | (1 << TWINT); // Enable STOP condition
    while (TWCR & (1 << TWSTO)); // Wait for STOP to be executed
}

// I2C Write Byte (address or data) and Wait for ACK
uint8_t i2c_write(uint8_t data) {
    TWDR = data; // Load data to data register
    TWCR = (1 << TWEN) | (1 << TWINT); // Start transmission
    while (!(TWCR & (1 << TWINT))); // Wait for TWINT flag set
    return (TWSR & 0xF8); // Return status
}

// I2C Read Byte and Send ACK/NACK
uint8_t i2c_read(uint8_t ack) {
    TWCR = (1 << TWEN) | (1 << TWINT) | (ack << TWEA); // Enable TWI, clear TWINT, set ACK
    while (!(TWCR & (1 << TWINT))); // Wait for TWINT flag set
    return TWDR; // Return received data
}

// Convert BCD to Decimal
uint8_t bcd_to_dec(uint8_t bcd) {
    return ((bcd / 16 * 10) + (bcd % 16));
}

// Function to Read Time from RTC
void readRTC() {
    i2c_start();
    i2c_write(RTC_I2C_ADDRESS << 1); // RTC address with write bit
    i2c_write(0x00);                 // Point to seconds register
    i2c_start();
    i2c_write((RTC_I2C_ADDRESS << 1) | 1); // RTC address with read bit

    uint8_t seconds = bcd_to_dec(i2c_read(1) & 0x7F); // Read seconds
    uint8_t minutes = bcd_to_dec(i2c_read(1));        // Read minutes
    uint8_t hours = bcd_to_dec(i2c_read(1));// & 0x3F);   // Read hours
    uint8_t day = bcd_to_dec(i2c_read(1));            // Read day
    uint8_t date = bcd_to_dec(i2c_read(1));           // Read date
    uint8_t month = bcd_to_dec(i2c_read(1));          // Read month
    uint8_t year = bcd_to_dec(i2c_read(0));           // Read year with NACK

    i2c_stop();

    // Output Time and Date via Serial (Requires Serial.begin in setup)
    Serial.print("Time: ");
    Serial.print(hours);
    Serial.print(":");
    Serial.print(minutes);
    Serial.print(":");
    Serial.println(seconds);

    Serial.print("Date: ");
    Serial.print(date);
    Serial.print("/");
    Serial.print(month);
    Serial.print("/");
    Serial.println(2000 + year);

    lcd_setCursor(5, 0);
    lcd_print("Time");
    if(hours < 10){
      lcd_setCursor(2, 1);
      lcd_printNumber(0);
      lcd_printNumber(hours);
    }else{
      lcd_setCursor(2, 1);
      lcd_printNumber(hours);
    }
    //lcd_setCursor(2, 1);
    //lcd_printNumber(hours);
    lcd_setCursor(4, 1);
    lcd_print(":");
    if(minutes < 10){
      lcd_setCursor(5, 1);
      lcd_printNumber(0);
      lcd_printNumber(minutes);
    }else{
      lcd_setCursor(5, 1);
      lcd_printNumber(minutes);
    }
    lcd_setCursor(7, 1);
    lcd_print(":");
    if(seconds < 10){
      lcd_setCursor(8, 1);
      lcd_printNumber(0);
      lcd_printNumber(seconds);
    }else{
      lcd_setCursor(8, 1);
      lcd_printNumber(seconds);
    }

    lcd_setCursor(5, 2);
    lcd_print("Date");
    lcd_setCursor(1, 3);
    lcd_printNumber(date);
    lcd_setCursor(3, 3);
    lcd_print("-");
    lcd_setCursor(4, 3);
    lcd_printNumber(month);
    lcd_setCursor(6, 3);
    lcd_print("-");
    lcd_setCursor(7, 3);
    lcd_printNumber(2000 + year);

    //if(seconds == 0)
    //  lcd_sendCommand(0x01);
}

void setup() {
    // Initialize I2C and Serial
    lcd_init();
    lcd_backlight();
    i2c_init();
    Serial.begin(9600);
}

void loop() {
    readRTC();  // Fetch and Display RTC Time
    _delay_ms(1000); // Wait 1 secon
}
GND5VSDASCLSQWRTCDS1307+