#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include "RTClib.h"

RTC_DS1307 rtc;

int address = 0; // var for address start from 0
byte value;

#define SCREEN_WIDTH        128 // OLED display width, in pixels
#define SCREEN_HEIGHT        64 // OLED display height, in pixels
#define OLED_RESET            4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_MPU6050 mpu; //declares the accelerometer as 'mpu'
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);  //declares the glcd as 'display'
sensors_event_t event; //names the variable 'event'

// Global variables
bool analogMode = true;
int second = 0;
int minute = 0;
int hour= 0;
String digitalText = "HH:MM:SS";

String pw = "EEE20003";
String new_psw="";

void setup() {
  Serial.begin(115200); //turns on serial monitor
  pinMode(7, INPUT_PULLUP);

  while ((!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))) //turns on ssd1306 and confirms it has turned on
  {
    Serial.println("SSD1306 not connected!");
    delay(1000);
  }
  Serial.println("SSD1306 ready!");

  while (!mpu.begin()) //turns on mpu and confirms it has turned on
  {
    Serial.println("MPU6050 not connected!");
    delay(1000);
  }
  Serial.println("MPU6050 ready!");
  char Buf[9];
  pw.toCharArray(Buf, 9);
  EEPROM.put(0,Buf);
 // passwordcheck();
 // change_psw();
  delay(2000);
  display.display();
  delay(1000);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  initialposition();
}


void loop() {
  display.clearDisplay(); //clears previous image
  display.setTextColor(SSD1306_WHITE); //makes image drawing white
  drawaxis(); //calls the axis drawing function
  coordinates(); //calls the coordinates function
  display.display(); //ensures everything is displayed


/**
   @brief Prints text to the OLED display with the specified text and coordinates.

   @param text The string to be printed
   @param x horizontal offset of text
   @param y vertical offset of text
   @param textSize size of the text
*/
}

void printText(String text, int x, int y, float textSize) {
  display.setTextColor(WHITE);
  display.setTextSize(textSize);
  display.setCursor(x, y);
  display.println(text);
  return x;
  return y;
}

void drawaxis() {


  display.drawLine(128, 32, 1, 32, SSD1306_WHITE); //draws x axis
  display.drawLine(64, 64, 64, 1, SSD1306_WHITE); //draws y axis
  display.display();

}

void coordinates() {
  mpu.getAccelerometerSensor()->getEvent(&event); //saves the coordinates as event
  int x1, x2, y1, y2, z1, z2;
  int x, y, z;

  x1 = (event.acceleration.x); //saves x coordinate of event as x1

  y1 = (event.acceleration.y); //saves y coordinate of event as y1

  z1 = (event.acceleration.z); //saves z coordinate of event as z1

  x2 = 64 + x1; //maths to centralise coordinate system
  y2 = 32 + y1;
  z2 = z1; //maths to create diameter of cross or circle
  if (z1 > 0) { //will draw a circle if z acceleration is positive
    display.drawCircle(x2, y2, z2, SSD1306_WHITE);
    display.display();
  }
  if (z1 == 0) { //will draw a pixel if z is 0
    display.drawPixel(x2, y2, SSD1306_WHITE);
    display.display();
  }
  if (z1 < 0) { //will draw a cross if z acceleration is negative
    display.drawLine(x2 - z2, y2 - z2, x2 + z2, y2 + z2, SSD1306_WHITE);
    display.drawLine(x2 + z2, y2 - z2, x2 - z2, y2 + z2, SSD1306_WHITE);
    display.display();
  }
  if (z1 == 9) { //if acceleration is downwards at aceleration of gravity
    fall();
  }

return;
}

void fall() {
  mpu.getAccelerometerSensor()->getEvent(&event); //saves the coordinates as event
  int z1 = (event.acceleration.z);
  if (z1 == 9) { //loops if user keeps falling
    fall();
  }
  if (z1 == 0) { //confirms user has fallen
    display.clearDisplay(); //clears previous image
    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(2, 2);
    display.print("User Has  Fallen");
    display.display();
    fall(); //keeps user has fallen on screen until z1 does not =0
  }
  else if (!z1 == 9 || z1 == 0) { //false fall detection sends back to loop function
    loop();
  }
}

  void passwordcheck(){
    for(int i=0;i<=8;i++){
  char pssw=EEPROM.read(i);
  String display_psw= String(pssw);
  int str_len = display_psw.length()+1;
  char display_psw1[str_len];
  display_psw.toCharArray(display_psw1,str_len);
  Serial.print(display_psw);
  address=address+1;
  }
  }


void initialposition(){
  mpu.getAccelerometerSensor()->getEvent(&event); //saves the coordinates as event
  int x1, y1,z1;

  x1 = (event.acceleration.x); //saves x coordinate of event as x1

  y1 = (event.acceleration.y); //saves y coordinate of event as y1

  z1 = (event.acceleration.z); //saves z coordinate of event as z1

  int coord[] = {x1,y1,z1};
  EEPROM.put(21, coord);
  delay(2000);
  display.display();
  delay(1000);
  for(int i=0;i<=5;i++){
    char initpol=EEPROM.read(i);
    String display_initpol= String(initpol);
    int str_len = display_initpol.length()+1;
    char display_initpol1[str_len];
    display_initpol.toCharArray(display_initpol1,str_len);
    Serial.print(display_initpol);
    address=address+1;
  }
  }



void change_psw(){
  Serial.println(" New password (No more of 20 characters): ");
    if (Serial.available()){
    String new_password=Serial.readString();
    Serial.print(new_password);
    //Serial.println("OK");
   int length=sizeof(new_password)-1;
    if(length>=20){
      Serial.println("Incorrect, Enter a password with less than 20 characters");
      }
    else{
      char Buf[20];
      pw.toCharArray(Buf, length);
      Serial.print(pw);
      EEPROM.put(address,Buf);
      for (int i=0;i<=length;i++){
        char new_psw=EEPROM.read(i);
        String display_new_psw= String(new_psw);
        int str_len_new = display_new_psw.length()+1;
        char display_new_psw1[str_len_new];
        display_new_psw.toCharArray(display_new_psw1,str_len_new);
        Serial.print(display_new_psw);
        Serial.println("Password saved");
        address=address+1;
     }
    }
  }
}

GND5VSDASCLSQWRTCDS1307+