#include <U8glib.h>

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);

//----------------------------INICIO IMAGENES MENU------------------------------------

// 'draw_icon', 32x32px
const unsigned char epd_bitmap_draw_icon [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x60, 0x06, 0x00, 
	0x00, 0x80, 0x01, 0x00, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 
	0x01, 0x07, 0xe0, 0x80, 0x01, 0x08, 0x10, 0x80, 0x01, 0x08, 0x10, 0x80, 0x01, 0x08, 0x10, 0x80, 
	0x00, 0xf0, 0x10, 0x80, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0x10, 0x80, 
	0x00, 0x00, 0xe1, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x08, 0x0c, 0x00, 
	0x00, 0x08, 0x30, 0x00, 0x00, 0x08, 0x40, 0x00, 0x00, 0x08, 0x40, 0x00, 0x00, 0x08, 0x40, 0x00, 
	0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x08, 0x40, 0x00, 
	0x00, 0x08, 0x40, 0x00, 0x00, 0x08, 0x40, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'settings_draw', 32x32px
const unsigned char epd_bitmap_settings_draw [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 
	0x00, 0x04, 0x40, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0xc4, 0x46, 0x00, 0x01, 0x24, 0x49, 0x00, 
	0x02, 0x18, 0x30, 0x80, 0x02, 0x00, 0x00, 0x80, 0x01, 0x03, 0x81, 0x00, 0x00, 0x8c, 0x62, 0x00, 
	0x00, 0x90, 0x12, 0x00, 0x1f, 0x20, 0x09, 0xf0, 0x10, 0x20, 0x08, 0x10, 0x10, 0x20, 0x08, 0x10, 
	0x10, 0x20, 0x08, 0x10, 0x1f, 0x20, 0x09, 0xf0, 0x00, 0x90, 0x12, 0x00, 0x00, 0x8c, 0x62, 0x00, 
	0x01, 0x03, 0x81, 0x00, 0x02, 0x00, 0x00, 0x80, 0x02, 0x18, 0x30, 0x80, 0x01, 0x24, 0x49, 0x00, 
	0x00, 0xc4, 0x46, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x07, 0xc0, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'translate_icon', 32x32px
const unsigned char epd_bitmap_translate_icon [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xe0, 0x00, 0x08, 0x00, 0x20, 
	0x00, 0x08, 0x1e, 0x20, 0x00, 0x08, 0x61, 0x20, 0x00, 0x08, 0x82, 0x20, 0x00, 0x09, 0x1c, 0x20, 
	0x00, 0x0a, 0x20, 0x20, 0x00, 0x0a, 0x20, 0x20, 0x00, 0x0a, 0x20, 0x20, 0x00, 0x0a, 0x23, 0x20, 
	0x00, 0x09, 0x14, 0xa0, 0x07, 0xff, 0xf8, 0xa0, 0x04, 0x00, 0x11, 0x20, 0x04, 0x00, 0x12, 0x20, 
	0x04, 0x00, 0x15, 0xe0, 0x04, 0x1c, 0x19, 0x20, 0x04, 0x22, 0x11, 0x48, 0x04, 0x41, 0x1f, 0x88, 
	0x04, 0x41, 0x10, 0x08, 0x04, 0x7f, 0x10, 0x08, 0x04, 0x41, 0x10, 0x08, 0x04, 0x41, 0x10, 0xc8, 
	0x04, 0x41, 0x11, 0xc8, 0x04, 0x00, 0x13, 0xf8, 0x04, 0x00, 0xf3, 0xf8, 0x04, 0x00, 0x91, 0xc0, 
	0x04, 0x00, 0xa0, 0xc0, 0x07, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 432)
const unsigned char* icons_bitmap[3] = {
	epd_bitmap_draw_icon,
	epd_bitmap_settings_draw,
	epd_bitmap_translate_icon
};

// 'handle_bar', 24x3px
const unsigned char epd_bitmap_handle_bar [] PROGMEM = {
	0xff, 0xff, 0x80, 0xff, 0xff, 0x80, 0xff, 0xff, 0x80
};
// 'handle_lines', 128x5px
const unsigned char epd_bitmap_handle_lines [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'outline_select', 40x54px
const unsigned char epd_bitmap_outline_select [] PROGMEM = {
	0x0f, 0xff, 0xff, 0xff, 0xf8, 0x10, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x02, 0x40, 
	0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 
	0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 
	0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 
	0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 
	0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 
	0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 
	0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 
	0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 
	0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 
	0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 
	0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 
	0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 
	0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 
	0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 
	0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x03, 0x40, 
	0x00, 0x00, 0x00, 0x03, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xfc
};

// 'logo_oisba', 112x38px
const unsigned char epd_bitmap_logo_oisba [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 
	0x00, 0x00, 0x3f, 0xc0, 0x18, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x40, 0x04, 0x00, 0x00, 0x00, 0x0f, 
	0xc0, 0x30, 0x21, 0xf0, 0x1c, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x70, 0x1f, 0x08, 
	0x46, 0x0f, 0xc3, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x87, 0xe0, 0xc4, 0x98, 0x01, 
	0x38, 0x38, 0x00, 0x1f, 0xf0, 0x1f, 0xf0, 0x00, 0x38, 0x39, 0x00, 0x32, 0xa0, 0x00, 0x87, 0x86, 
	0x01, 0x3f, 0xd8, 0x37, 0xf9, 0x00, 0xc3, 0xc2, 0x00, 0x0a, 0x90, 0x01, 0x38, 0x71, 0x82, 0x7f, 
	0xdc, 0x77, 0xfc, 0x83, 0x1c, 0x39, 0x00, 0x12, 0x48, 0x07, 0xc0, 0x0c, 0x7c, 0xff, 0xbe, 0xfb, 
	0xfe, 0x7c, 0x60, 0x07, 0xc0, 0x24, 0x24, 0x78, 0x0c, 0x02, 0x05, 0xff, 0x7e, 0xfd, 0xff, 0x40, 
	0x80, 0x60, 0x3c, 0x48, 0x13, 0x81, 0xf3, 0x3f, 0xf9, 0xfe, 0xfa, 0xbe, 0xff, 0x3f, 0xf9, 0x9f, 
	0x03, 0x90, 0x08, 0x06, 0x00, 0xc9, 0x0b, 0xfd, 0xfa, 0xbf, 0x7f, 0xa1, 0x26, 0x00, 0xc0, 0x20, 
	0x06, 0x18, 0x20, 0x11, 0x13, 0xfb, 0xf2, 0x9f, 0xbf, 0x91, 0x10, 0x08, 0x30, 0xc0, 0x01, 0xa0, 
	0x70, 0x22, 0x13, 0xfb, 0xe2, 0x8f, 0xbf, 0x90, 0x88, 0x1c, 0x0b, 0x00, 0x00, 0x40, 0xd8, 0x44, 
	0x13, 0xf7, 0xe2, 0x8f, 0xdf, 0x90, 0x44, 0x36, 0x04, 0x00, 0x00, 0x21, 0x8c, 0x44, 0x13, 0xf7, 
	0xc7, 0xc7, 0xdf, 0x90, 0x44, 0x63, 0x08, 0x00, 0x00, 0x10, 0xd8, 0x88, 0x11, 0xef, 0xca, 0xa7, 
	0xef, 0x10, 0x22, 0x36, 0x10, 0x00, 0x00, 0x0c, 0x71, 0x08, 0x09, 0xef, 0x92, 0x93, 0xef, 0x20, 
	0x21, 0x1c, 0x60, 0x00, 0x00, 0x02, 0x21, 0x10, 0x09, 0xef, 0x0c, 0x61, 0xef, 0x20, 0x11, 0x08, 
	0x80, 0x00, 0x00, 0x01, 0x82, 0x10, 0x24, 0xef, 0x00, 0x01, 0xee, 0x48, 0x10, 0x83, 0x00, 0x00, 
	0x00, 0x00, 0x61, 0xa0, 0x44, 0xee, 0x00, 0x00, 0xee, 0x44, 0x0b, 0x0c, 0x00, 0x00, 0x00, 0x00, 
	0x1c, 0x43, 0xa2, 0x6c, 0x00, 0x00, 0x6c, 0x8b, 0x84, 0x70, 0x00, 0x00, 0x00, 0x00, 0x03, 0x1c, 
	0x11, 0x2c, 0x00, 0x00, 0x69, 0x10, 0x71, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x0e, 0x18, 
	0x00, 0x00, 0x30, 0xe0, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 
	0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x40, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x60, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x18, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00
};




//----------------------------FINAL IMAGENES MENU-----------------------------------------------

//----------------------------INICIO VARIABLES MENU------------------------------------------------

const int num_of_icons = 3;

char menu_text[num_of_icons][15] {

  {"Ayuda"},
	{"Ajustes"},
	{"Traducir"}

};

int opcion_seleccionada = 0; //Cual opcion del menu esta seleccionada
int opcion_anterior; //Parte del menu anterior a la seleccionada
int opcion_siguiente; //Parte del menu siguiente a la seleccionada

//---------------------------FIN VARIABLES DE MENU----------------------------------------


#define enter_btn 2
#define back_btn 3
#define right_btn 4
#define left_btn 5


volatile byte estado = HIGH;

const int standbyTime = 10000;

unsigned long previousTime = 0;

int moveBar = 56;

void setup() {
  // Se inicia la comunicación con componentes y Arduino.

  Serial.begin(9600);
  pinMode(right_btn, INPUT_PULLUP);
  pinMode(enter_btn, INPUT_PULLUP);
  pinMode(left_btn, INPUT_PULLUP);
  pinMode(back_btn, INPUT_PULLUP);

  //***************************************************************
  //Se inician las interrupciones del Arduino Nano

  attachInterrupt(digitalPinToInterrupt(enter_btn), blink, FALLING);
  attachInterrupt(digitalPinToInterrupt(back_btn), blink, FALLING);
}

void loop() {
  // put your main code here, to run repeatedly:

	static byte rightBtn_state = 0;
	static byte leftBtn_state = 0;
	static byte enterBtn_state = 0;
	static byte backBtn_state = 0;

	static byte currentMenu = 1;
	static bool isActive = true;

	unsigned long actualTime = millis();

	switch(currentMenu) {
		case 0:
			noStandby(actualTime, currentMenu, isActive);
			drawLogo();
			break;
		case 1:
			noStandby(actualTime, currentMenu, isActive);
			moveMenu(rightBtn_state, leftBtn_state);
  		drawMenu();
			break;
	}
	
}

void drawMenu() { //Esta función dibuja el menú principal.

 u8g.firstPage();
	do {
		
    //Parte anterior
    u8g.setFont(u8g_font_u8glib_4r); 
    u8g.drawBitmapP(5, 16, 32/8, 32, icons_bitmap[opcion_anterior]);
		u8g.drawStr(7, 57, menu_text[opcion_anterior]); //Texto de ajustes

    //Parte del medio
    u8g.setFont(u8g_font_u8glib_4r); 
    u8g.drawBitmapP(48, 16, 32/8, 32, icons_bitmap[opcion_seleccionada]);
		u8g.drawStr(52, 57, menu_text[opcion_seleccionada]); //Texto de traducir

    //Parte siguiente
    u8g.setFont(u8g_font_u8glib_4r); 
    u8g.drawBitmapP(91, 16, 32/8, 32, icons_bitmap[opcion_siguiente]);
		u8g.drawStr(97, 57, menu_text[opcion_siguiente]); //Texto de ayuda

    //Selección
    u8g.drawBitmapP(44, 7, 40/8, 54, epd_bitmap_outline_select);

    //Barra de ubicación
    u8g.drawBitmapP(0, 0, 128/8, 5, epd_bitmap_handle_lines);
    u8g.drawBitmapP(moveBar, 1, 24/8, 3, epd_bitmap_handle_bar);


	} while ( u8g.nextPage() ); 
}

void drawLogo() {
	u8g.firstPage();
	do {
		u8g.setFont(u8g_font_7x14Br);
		u8g.drawBitmapP(8, 3, 112/8, 38, epd_bitmap_logo_oisba);
		u8g.drawStr(30, 52, "O.I.S.B.A"); 
	} while ( u8g.nextPage() );
}

void moveMenu(byte &rbtn, byte &lbtn) { //Esta función mueve el menu principal
	if((digitalRead(right_btn) == LOW) && (rbtn == 0)) {
    opcion_seleccionada += 1;
		rbtn = 1;
		moveBar += 43;
    if(opcion_seleccionada >= num_of_icons) {opcion_seleccionada = 0;}
  }
  if((digitalRead(left_btn) == LOW) && (lbtn == 0)) {
    opcion_seleccionada -= 1;
		lbtn = 1;
		moveBar -= 43;
    if(opcion_seleccionada < 0) {opcion_seleccionada = num_of_icons - 1;}
  }

	if((digitalRead(right_btn) == HIGH) && (rbtn == 1)) {
		rbtn = 0;
  }
  if((digitalRead(left_btn) == HIGH) && (lbtn == 1)) {
		lbtn = 0;
  }

	//Mover iconos del menu
	opcion_anterior = opcion_seleccionada - 1;
  if (opcion_anterior < 0) {
    opcion_anterior = num_of_icons - 1;
  }
  opcion_siguiente = opcion_seleccionada + 1;
  if (opcion_siguiente >= num_of_icons) {
    opcion_siguiente = 0;
  }
	if(moveBar < 0) {
		moveBar = 99;
	}
	if (moveBar > 99) {
		moveBar = 13;
	}
	
}

void noStandby (unsigned long time, byte &menu_option, bool &active) {

	static bool timeLimit = false;
	static byte isPressed = 0;

	if(active == true) { //Si el usuario está activo...

		timeLimit = false;
	
		if(digitalRead(right_btn) == LOW || digitalRead(left_btn) == LOW ||
	 	  digitalRead(enter_btn) == LOW || digitalRead(back_btn) == LOW) {
			previousTime = millis(); 
		}
			
	}

	else if(active == false) { //Si el usuario no está activo, entonces...

		if(digitalRead(right_btn) == LOW) {
			while (active == false) {
				if(digitalRead(right_btn) == HIGH) {
					previousTime = millis();
					menu_option = 1;
					active = true;
				}
			}
		}
		else if(digitalRead(left_btn) == LOW) {
			while (active == false) {
				if(digitalRead(left_btn) == HIGH) {
					previousTime = millis();
					menu_option = 1;
					active = true;
				}
			}
		}
		else if(digitalRead(enter_btn) == LOW) {
			while (active == false) {
				if(digitalRead(enter_btn) == HIGH) {
					previousTime = millis();
					menu_option = 1;
					active = true;
				}
			}
		}
		else if(digitalRead(back_btn) == LOW) {
			while (active == false) {
				if(digitalRead(back_btn) == HIGH) {
					previousTime = millis();
					menu_option = 1;
					active = true;
				}
			}
		}
	}

	if (timeLimit == false) { //Si el tiempo limite no se ha cumplido, entonces...
		if(time - previousTime >= standbyTime) { //Si ocurre el tiempo limite, desactiva esta parte del código.
			Serial.println(F("Pasaron 10 segundos"));
			menu_option = 0; //Mandame a la pantalla de Standby
			active = false; //Significa que el usuario no está activo.
			timeLimit = true; //El tiempo limite ha llegado, por lo que desactiva todo
		}

	}
}

void blink() {
  estado = !estado;
}