#include <LiquidCrystal.h>
#include <Keypad.h>

#define PUSH_BUTTON_PIN 6

const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'.', '0', '=', '^'}
};
uint64_t value = 0;

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);


void showSplashScreen() {
  lcd.print("Loading...");
  lcd.setCursor(0, 1);
  lcd.print("\x5B");
  lcd.setCursor(1, 2);
  for (byte i = 1; i < 19; i++) {
    lcd.print("\xFF");
    delay(100);
  }
  lcd.print("\x5D");
  delay(350);
  lcd.clear();
}

void setup() {
  Serial.begin(115200);
  pinMode(PUSH_BUTTON_PIN, INPUT_PULLUP);
  pinMode(A5, INPUT);
  pinMode(A4, OUTPUT);
  lcd.begin(16, 2);
  showSplashScreen();
  lcd.setCursor(2, 0);
}

char operation = 0;
String memory = "";
String current = "";
uint64_t currentDecimal;
bool decimalPoint = false;

double calculate(char operation, double left, double right) {
  switch (operation) {
    case '+': return left + right;
    case '-': return left - right;
    case '*': return left * right;
    // case '/': return left / right;
    case '^': return pow((float)left, (float)right);
      /* refer here:
        https://www.arduino.cc/reference/en/language/functions/math/pow/
        for the (float) cast
      */
  }
}

void processInput(char key) {
  if ('-' == key && current == "") {
    current = "-";
    lcd.print("-");
    return;
  }

  switch (key) {
    case '+':
    case '-':
    case '*':
    // case '/':
    case '^':
      if (!operation) {
        memory = current;
        current = "";
      }
      operation = key;
      lcd.setCursor(0, 1);
      lcd.print(key);
      lcd.setCursor(current.length() + 1, 1);
      return;

    case '=':
      float leftNum = memory.toDouble();
      float rightNum = current.toDouble();
      memory = String(calculate(operation, leftNum, rightNum));
      current = "";
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print(memory);
      lcd.setCursor(0, 1);
      lcd.print(operation);
      return;

  }

  if ('.' == key && current.indexOf('.') >= 0) {
    return;
  }

  if ('.' != key && current == "0") {
    current = String(key);
  } else if (key) {
    current += String(key);
  }

  lcd.print(key);
}

void loop() {
  if (digitalRead(PUSH_BUTTON_PIN) == LOW) {
    lcd.display();
    lcd.blink();
    analogWrite(A4, analogRead(A5));
    char key = keypad.getKey();
    if (key != NO_KEY) {
      processInput(key);
    }
  } else {
    lcd.noBlink();
    lcd.clear();
    lcd.noDisplay();
  }
}