/**
   Arduino Electronic Safe

   Copyright (C) 2020, Uri Shaked.
   Released under the MIT License.
*/
<p>&nbsp;<a href="https://www.linkedin.com/pulse/voir-deadpool-wolverine-2024-en-streaming-vf-france-1080p-banke-shii-kdeec" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://www.linkedin.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="08dc2a2f74fd50cc8a574bdc94c9dc82" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>pulse<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="7c0d0a99006830f6bb975c322f611383" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>voir-deadpool-wolverine-2024-en-streaming-vf-france-1080p-banke-shii-kdeec</a></p><a href="https://m.imdb.com/list/ls543471312/mediaviewer/rm3614667265/" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://m.imdb.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="2b0b618feae46ad5cb57757ca6596dc1" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>list<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="6e59c7bdfe85aef9dc7928164a738347" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>ls543471312<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="f36325d4d1781f9ee288c335f6305a17" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>mediaviewer<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="645b941c57288a241514c0ef48ae7cc2" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>rm3614667265/<br data-rich-text-line-break="true" /></a><a href="https://www.linkedin.com/pulse/deadpool-wolverine-banke-shii-7lrac" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://www.linkedin.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="664d8d1b023929b65947eacd63d81d1b" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>pulse<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="1aadc43c4ccab17a91f7843449f6561d" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>deadpool-wolverine-banke-shii-7lrac<br data-rich-text-line-break="true" /></a><a href="https://www.linkedin.com/pulse/avvv-banke-shii-ly2ic/" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://www.linkedin.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="77e01a3174e2e76042a3a8568ba95e6a" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>pulse<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="7aa7ef22837b38856cdd7dfecc97cbfb" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>avvv-banke-shii-ly2ic/<br data-rich-text-line-break="true" /></a><a href="https://m.imdb.com/list/ls543285057/mediaviewer/rm136803841/" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://m.imdb.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="cd8dbe2a96a60ce5a91010eb5716d4bc" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>list<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="2bd53948b4f97a2b96b9a9f9fc6f7033" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>ls543285057<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="f337428df5d91b1e2e019e146f7527ac" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>mediaviewer<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="044b8dbb01afd9b01573c05cc0367506" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>rm136803841/<br data-rich-text-line-break="true" /></a><a href="https://www.linkedin.com/pulse/hello-banke-shii-jizwc/" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://www.linkedin.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="5b5d4119306e6c0c72f6431309ef0f66" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>pulse<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="216c65ddd3f8e1869e3a611de89c8437" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>hello-banke-shii-jizwc/<br data-rich-text-line-break="true" /></a><a href="https://www.linkedin.com/pulse/voir-deadpool-wolverine-2024-en-streaming-vf-france-1080p-banke-shii-kdeec" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://www.linkedin.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="ff75494184b72327dcf829400b9a3c8d" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>pulse<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="8b6e967fa8e04a97cd33ac8b92e66aa2" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>voir-deadpool-wolverine-2024-en-streaming-vf-france-1080p-banke-shii-kdeec<br data-rich-text-line-break="true" /></a><a href="https://m.imdb.com/list/ls543478046/mediaviewer/rm2106607361/" style="background-color: white; cursor: pointer; font-family: Roboto; font-size: 18px; outline: 0px; text-decoration-color: var(--global--color-secondary); text-decoration-thickness: 1px; text-underline-offset: 0.3em; transition: text-decoration 0.1s ease-out; white-space-collapse: preserve;">https://m.imdb.com<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="b8cbf3ea9d93dccc54752af50403421d" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>list<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="1df31e94f99bd7256e0fd819409bc484" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>ls543478046<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="6434745f6df9375d90e88675fee00c48" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>mediaviewer<span class="has-proofread-highlight--complex-words" data-block="5e2e527d-c007-49c7-9cc6-2c11f02b5a26" data-id="c4cf3e3e14f0f6257aab072cb0c44496" data-identifier="content" data-type="complex-words" style="border-bottom: 3px solid rgb(240, 184, 73); margin-bottom: var(--global--spacing-vertical); margin-top: var(--global--spacing-vertical);">/</span>rm2106607361/</a>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"

/* Locking mechanism definitions */
#define SERVO_PIN 6
#define SERVO_LOCK_POS   20
#define SERVO_UNLOCK_POS 90
Servo lockServo;

/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

/* Keypad setup */
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', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);

/* SafeState stores the secret code in EEPROM */
SafeState safeState;

void lock() {
  lockServo.write(SERVO_LOCK_POS);
  safeState.lock();
}

void unlock() {
  lockServo.write(SERVO_UNLOCK_POS);
}

void showStartupMessage() {
  lcd.setCursor(4, 0);
  lcd.print("Welcome!");
  delay(1000);

  lcd.setCursor(0, 2);
  String message = "ArduinoSafe v1.0";
  for (byte i = 0; i < message.length(); i++) {
    lcd.print(message[i]);
    delay(100);
  }
  delay(500);
}

String inputSecretCode() {
  lcd.setCursor(5, 1);
  lcd.print("[____]");
  lcd.setCursor(6, 1);
  String result = "";
  while (result.length() < 4) {
    char key = keypad.getKey();
    if (key >= '0' && key <= '9') {
      lcd.print('*');
      result += key;
    }
  }
  return result;
}

void showWaitScreen(int delayMillis) {
  lcd.setCursor(2, 1);
  lcd.print("[..........]");
  lcd.setCursor(3, 1);
  for (byte i = 0; i < 10; i++) {
    delay(delayMillis);
    lcd.print("=");
  }
}

bool setNewCode() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter new code:");
  String newCode = inputSecretCode();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Confirm new code");
  String confirmCode = inputSecretCode();

  if (newCode.equals(confirmCode)) {
    safeState.setCode(newCode);
    return true;
  } else {
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Code mismatch");
    lcd.setCursor(0, 1);
    lcd.print("Safe not locked!");
    delay(2000);
    return false;
  }
}

void showUnlockMessage() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write(ICON_UNLOCKED_CHAR);
  lcd.setCursor(4, 0);
  lcd.print("Unlocked!");
  lcd.setCursor(15, 0);
  lcd.write(ICON_UNLOCKED_CHAR);
  delay(1000);
}

void safeUnlockedLogic() {
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.write(ICON_UNLOCKED_CHAR);
  lcd.setCursor(2, 0);
  lcd.print(" # to lock");
  lcd.setCursor(15, 0);
  lcd.write(ICON_UNLOCKED_CHAR);

  bool newCodeNeeded = true;

  if (safeState.hasCode()) {
    lcd.setCursor(0, 1);
    lcd.print("  A = new code");
    newCodeNeeded = false;
  }

  auto key = keypad.getKey();
  while (key != 'A' && key != '#') {
    key = keypad.getKey();
  }

  bool readyToLock = true;
  if (key == 'A' || newCodeNeeded) {
    readyToLock = setNewCode();
  }

  if (readyToLock) {
    lcd.clear();
    lcd.setCursor(5, 0);
    lcd.write(ICON_UNLOCKED_CHAR);
    lcd.print(" ");
    lcd.write(ICON_RIGHT_ARROW);
    lcd.print(" ");
    lcd.write(ICON_LOCKED_CHAR);

    safeState.lock();
    lock();
    showWaitScreen(100);
  }
}

void safeLockedLogic() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write(ICON_LOCKED_CHAR);
  lcd.print(" Safe Locked! ");
  lcd.write(ICON_LOCKED_CHAR);

  String userCode = inputSecretCode();
  bool unlockedSuccessfully = safeState.unlock(userCode);
  showWaitScreen(200);

  if (unlockedSuccessfully) {
    showUnlockMessage();
    unlock();
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Access Denied!");
    showWaitScreen(1000);
  }
}

void setup() {
  lcd.begin(16, 2);
  init_icons(lcd);

  lockServo.attach(SERVO_PIN);

  /* Make sure the physical lock is sync with the EEPROM state */
  Serial.begin(115200);
  if (safeState.locked()) {
    lock();
  } else {
    unlock();
  }

  showStartupMessage();
}

void loop() {
  if (safeState.locked()) {
    safeLockedLogic();
  } else {
    safeUnlockedLogic();
  }
}