#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <RTClib.h>

// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set address 0x27 for 16 chars and 2-line LCD

// RTC Setup
RTC_DS3231 rtc;

// Keypad Setup
const byte ROW_NUM    = 4; // four rows
const byte COLUMN_NUM = 4; // four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'=','0','.','/'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);

String equation = ""; // To hold the current equation

void setup() {
  // Start the LCD and RTC
  lcd.begin(16, 2);  // Set LCD to 16 columns and 2 rows
  rtc.begin();
  lcd.setBacklight(1);

  // Display Initial Message
  lcd.clear();
  lcd.print("Goose Calc");
  delay(1000); // Show message for 1 second

  // Clear the screen
  lcd.clear();
}

void loop() {
  // Get current time from RTC
  DateTime now = rtc.now();
  String timeString = String(now.hour()) + ":" + String(now.minute());
  String dayString = getHebrewDay(now.dayOfTheWeek());
  
  // Display the time and day
  lcd.setCursor(0, 0);
  lcd.print(timeString + " " + dayString);

  // Process Keypad Input
  char key = keypad.getKey();
  if (key) {
    processKey(key);
  }

  // Display equation on the second line
  lcd.setCursor(0, 1);
  lcd.print(equation);
}

// Map weekday index to Hebrew day names
String getHebrewDay(uint8_t weekday) {
  switch (weekday) {
    case 1: return "יום א";  // Sunday
    case 2: return "יום ב";  // Monday
    case 3: return "יום ג";  // Tuesday
    case 4: return "יום ד";  // Wednesday
    case 5: return "יום ה";  // Thursday
    case 6: return "יום ו";  // Friday
    case 7: return "יום ש";  // Saturday
    default: return "";
  }
}

// Handle Keypad Input for Calculation
void processKey(char key) {
  if (key >= '0' && key <= '9') {
    equation += key; // Append number
  } else if (key == '+' || key == '-' || key == '*' || key == '/') {
    equation += " " + String(key) + " "; // Append operator with spaces
  } else if (key == '=') {
    // Evaluate the equation (simplified for now)
    // A more complex parser can be used to evaluate expressions
    float result = evaluateEquation(equation);
    equation = String(result);
  } else if (key == 'C') {
    // Clear the equation
    equation = "";
  }
}

// Simple evaluator for the equation (just for simple cases)
float evaluateEquation(String eq) {
  // Implement a basic calculator (complex parsing can be added later)
  // For now, we'll use a simple approach
  float result = 0;
  int num1, num2;
  char op;
  
  sscanf(eq.c_str(), "%d %c %d", &num1, &op, &num2);
  
  switch (op) {
    case '+': result = num1 + num2; break;
    case '-': result = num1 - num2; break;
    case '*': result = num1 * num2; break;
    case '/': result = num1 / (float)num2; break;
  }
  return result;
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4