#include <Keypad.h>
#include <ezBuzzer.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>

volatile int interruptCounter;
int totalInterruptCounter;
 
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
 
void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux);
  interruptCounter++;
  portEXIT_CRITICAL_ISR(&timerMux);
}

#define BUZZER_PIN  23 // ESP32 pin GIOP23 connected to the buzzer's pin
#define ROW_NUM     4  // four rows
#define COLUMN_NUM  4  // four columns
#define SCREEN_WIDTH 128 // OLED width,  in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
#define Siren_PIN  13
#define Sensor1_PIN  34
#define Sensor2_PIN  35

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {19, 18, 5, 17};    // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins
byte pin_column[COLUMN_NUM] = {16, 4, 2, 15}; // GIOP16, GIOP4, GIOP0, GIOP2 connect to the column pins

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
ezBuzzer buzzer(BUZZER_PIN); // create ezBuzzer object that attach to a pin;
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create an OLED display object connected to I2C

const String Admin_password = "5606"; // change your password here
const String user1pass = "1234"; // change your password here
const String user2pass = "5678"; // change your password here
String input_password;
byte set_Cursor = 40;
byte Err = 0;
byte sensor1, sensor2 = 0;
int sec = 0;


void setup() {
  Serial.begin(9600);
  
  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 500000, true);
  timerAlarmEnable(timer);
  timerAlarmDisable(timer);

  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(Siren_PIN, OUTPUT);
  pinMode(Sensor1_PIN, INPUT);
  pinMode(Sensor2_PIN, INPUT);
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(Siren_PIN, LOW);
  // initialize OLED display with I2C address 0x3C
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("failed to start SSD1306 OLED"));
    while (1);
  }
  oled.clearDisplay(); // clear display
  oled.setTextSize(2);         // set text size
  oled.setTextColor(WHITE);    // set text color
  oled.setCursor(15, 20);       // set position to display
  oled.println("OneTouch"); // set text
  oled.display();              // display on OLED
  delay(2000); // wait two seconds for initializing
  oled.clearDisplay(); // clear display
  oled.setCursor(20, 10);       // set position to display
  oled.println("Dharani"); // set text
  oled.setCursor(5, 35);       // set position to display
  oled.println("Jewellery"); // set text
  oled.display();              // display on OLED
  oled.clearDisplay(); // clear display

  input_password.reserve(8); // maximum input characters is 9, change if needed

  //create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
  xTaskCreatePinnedToCore(
                    loop2,   /* Task function. */
                    "loop2",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    0,           /* priority of the task */
                    &Task1,      /* Task handle to keep track of created task */
                    0);          /* pin task to core 0 */                  
  delay(500); 

void loop(){
  digitalWrite(Siren_PIN, HIGH);
  delay(100);
  digitalWrite(Siren_PIN, LOW);
  delay(100);
}
}

void loop2 (void* pvParameters) {
  while (1) {
    Serial.print ("Hello");
    delay (500); // wait for half a second
    Serial.println (" World");
    delay (500); // wait for half a second
  }
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
bz1:1
bz1:2
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
led1:A
led1:C
pot1:GND
pot1:SIG
pot1:VCC