/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Date Display"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-02 12:10:36

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Counting date starting from 02 June 2024 and */
	/* adding daily one more day. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Wire.h>
#include <Adafruit_SSD1306.h>  //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
#include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
#include <RTClib.h>  // Library for real-time clock

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF I2C PINS *****/
const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
const uint8_t Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;  // I2C address for the display

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET    -1  // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;  // Create an instance of the U8G2_FOR_ADAFRUIT_GFX class
RTC_DS1307 rtc;  // Create an instance of the RTC_DS1307 class

void setup(void) {
  // Initialize the display with the I2C address
  if(!display.begin(SSD1306_SWITCHCAPVCC, Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS)) {
    Serial.begin(9600);
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Initialize the RTC
  if (!rtc.begin()) {
    Serial.println(F("Couldn't find RTC"));
    while (1);
  }

  // Set the RTC to the initial date if it's not running
  if (!rtc.isrunning()) {
    rtc.adjust(DateTime(2024, 6, 2, 0, 0, 0));  // Set the date to 02 June 2024
  }

  display.display();
  delay(2000);  // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Initialize U8G2 for Adafruit GFX
  u8g2_for_adafruit_gfx.begin(display);  // Connect u8g2 procedures to Adafruit GFX

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen
  display.display();
  delay(2000);  // Pause for 2 seconds
}

void loop(void) {
  // Clear the display
  display.clearDisplay();

  // Get the current date from the RTC
  DateTime now = rtc.now();

  // Set the font and draw the date
  u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
  u8g2_for_adafruit_gfx.setCursor(0, 20);
  u8g2_for_adafruit_gfx.print("Date: ");
  u8g2_for_adafruit_gfx.print(now.day());
  u8g2_for_adafruit_gfx.print("/");
  u8g2_for_adafruit_gfx.print(now.month());
  u8g2_for_adafruit_gfx.print("/");
  u8g2_for_adafruit_gfx.print(now.year());

  // Show the display buffer on the screen
  display.display();
  delay(2000);  // Pause for 2 seconds
}
