#include <LiquidCrystal.h>
#include "Button.h"
/*
  #include <DS18B20.h>
  #include <MQ2.h>
  #include <EEPROMex.h>
  //#include "Arduino.h"

  //#define ONE_WIRE_BUS 2
  //OneWire oneWire(ONE_WIRE_BUS);
  //DallasTemperature sensors(&oneWire);
  DS18B20 ds(2);

  int pin = A0;
  float lpg, co, smoke;
  MQ2 mq2(pin); */

const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Button cancelBtn(7, 25, A5);
Button okBtn(6, 25, A5);
Button upBtn(5, 25, A5);
Button downBtn(4, 25, A5);

byte page = 0, lastPage, newPage, NumberOfMenu = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("init program");
  lcd.begin(16, 2);
  //  mq2.begin();
  //  sensors.begin();
  lastPage = page;
}

void loop() {
  /*  while (ds.selectNext()) {
      Serial.println(ds.getTempC());
    }
    //  float tempC = sensors.getTempCByIndex(0); */
  /*
     read the values from the sensor, it returns
     an array which contains 3 values.
     0 : LPG in ppm
     1 : CO in ppm
     2 : SMOKE in ppm
  */
  /* float* values = mq2.read(true); //set it false if you don't want to print the values to the Serial

    // lpg = values[0];
    lpg = mq2.readLPG();
    // co = values[1];
    co = mq2.readCO();
    // smoke = values[2];
    smoke = mq2.readSmoke(); */

  cancelBtn.check();
  okBtn.check();
  upBtn.check();
  downBtn.check();

  if (cancelBtn.wasReleased()) {
    if (page == 1) {
      newPage = 0;
      NumberOfMenu = 1;
    }
    else {
      newPage = lastPage;
    }
    if (page != newPage)lastPage = page, page = newPage;
  }

  if (okBtn.wasReleased()) {
    if (page == 1) {
      if (NumberOfMenu == 1) newPage = 11, NumberOfMenu = 1;
      if (NumberOfMenu == 2) newPage = 21, NumberOfMenu = 2;
      if (NumberOfMenu == 3) newPage = 31, NumberOfMenu = 3;
      if (NumberOfMenu == 4) newPage = 41, NumberOfMenu = 4;
      if (NumberOfMenu == 5) newPage = 51, NumberOfMenu = 5;
      if (NumberOfMenu == 6) newPage = 61, NumberOfMenu = 6;
    }
    if (page != newPage)lastPage = page, page = newPage;
  }

  if (upBtn.wasReleased()) {
    if (page == 1) {
      NumberOfMenu++;
      if (NumberOfMenu > 6) NumberOfMenu = 6;
    }
  }

  if (downBtn.wasReleased()) {
    if (page == 1) {
      NumberOfMenu--;
      if (NumberOfMenu < 1) NumberOfMenu = 1;
    }
  }

  if (okBtn.pressedFor(1000) && page == 0) {
    newPage = 1;
  }

  switch (page) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print("  Home  Screen  ");
      lcd.setCursor(0, 1);
      lcd.print(" Sensors  Value ");
      break;

    case 1:
      switch (NumberOfMenu) {
        case 1:
          lcd.setCursor(0, 0);
          lcd.print("    Settings    ");
          lcd.setCursor(0, 1);
          lcd.print("   Gas Sensor   ");
          break;

        case 2:
          lcd.setCursor(0,0);
          lcd.print("    Settings    ");
          lcd.setCursor(0,1);
          lcd.print("  Smoke Sensor  ");
          break;

        case 3:
          lcd.setCursor(0, 0);
          lcd.print("    Settings    ");
          lcd.setCursor(0, 1);
          lcd.print("  Sensor  Temp  ");
          break;

        case 4:
          lcd.setCursor(0, 0);
          lcd.print("    Settings    ");
          lcd.setCursor(0, 1);
          lcd.print("  Flame Sensor  ");
          break;

        case 5:
          lcd.setCursor(0, 0);
          lcd.print(" Enable-Disable ");
          lcd.setCursor(0, 1);
          lcd.print("   Gas Sensor   ");
          break;

        case 6:
          lcd.setCursor(0, 0);
          lcd.print("    Settings    ");
          lcd.setCursor(0, 1);
          lcd.print("  Beep  Sounds  ");
          break;
      }
      break;

    case 11:
      lcd.setCursor(0,0);
      lcd.print("Set Gas Sensor  ");
      lcd.setCursor(0,1);
      lcd.print("999.9"); //valur ref gas
      lcd.setCursor(7,1);
      lcd.print("ppm       ");
      break;

    case 21:
      lcd.setCursor(0,0);
      lcd.print("Set Smoke Sensor");
      lcd.setCursor(0,1);
      lcd.print("222.1");
      lcd.setCursor(7,1);
      lcd.print("ppm       ");
      break;

    case 31:
      lcd.setCursor(0,0);
      lcd.print("Set Sensor Temp ");
      lcd.setCursor(0,1);
      lcd.print("12.3");
      lcd.setCursor(5,1);
      lcd.print("dC         ");
      break;

    case 41:
    lcd.setCursor(0,0);
    lcd.print("Set Flame Sensor  ");
    lcd.setCursor(0,1);
    lcd.print("333.8");
    lcd.setCursor("")
  }


 /* Serial.print("page = ");
  Serial.println(page);
  Serial.print("NumberOfMenu: ");
  Serial.println(NumberOfMenu);
  Serial.println(""); */
}