// LIBRERIAS
#include "U8glib.h"

// OBJETOS
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

// BITMAPS DE LOS ICONOS

// 'icon_garra', 16x16px
const unsigned char bitmap_icon_garra [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x3f, 0x00, 0x21, 0x86, 0x00, 0xe7, 0x1c, 0x7e, 0x7f, 0xe6, 
	0x7f, 0xe6, 0x1c, 0x7e, 0x00, 0xe7, 0x21, 0x86, 0x3f, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'icon_stepper', 16x16px
const unsigned char bitmap_icon_stepper_base [] PROGMEM = {
	0x01, 0x80, 0x02, 0x40, 0x14, 0x28, 0x24, 0x25, 0x44, 0x22, 0x44, 0x22, 0x23, 0xc4, 0x50, 0x0b, 
	0x4f, 0xf2, 0x40, 0x02, 0x40, 0x02, 0x40, 0x03, 0x20, 0x04, 0x10, 0x08, 0x0f, 0xf0, 0x00, 0x00
};
// 'icon_stepper_s1', 16x16px
const unsigned char bitmap_icon_stepper_s1 [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x04, 0x20, 0x09, 0x90, 0x0a, 0x50, 0x0a, 0x50, 0x09, 0x90, 
	0x08, 0x10, 0x08, 0x10, 0x08, 0x10, 0x1f, 0xf8, 0x20, 0x04, 0x40, 0x02, 0x7f, 0xfe, 0x00, 0x00
};
// 'icon_stepper_s2', 16x16px
const unsigned char bitmap_icon_stepper_s2 [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x1f, 0x78, 0x20, 0x84, 0x40, 0x82, 0x40, 0x82, 0x20, 0x82, 0x1f, 0x42, 
	0x00, 0x42, 0x00, 0x42, 0x00, 0x42, 0x00, 0x42, 0x00, 0x24, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00
};
// 'icon_stepper_s3', 16x16px
const unsigned char bitmap_icon_stepper_s3 [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x4c, 0x00, 0x43, 0xf0, 0x44, 0x08, 0x48, 0x04, 0x48, 0x22, 
	0x48, 0x12, 0x48, 0x04, 0x44, 0x08, 0x43, 0xf0, 0x4c, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'icon_alerta', 16x16px
const unsigned char bitmap_icon_alerta [] PROGMEM = {
	0x38, 0x1c, 0x40, 0x02, 0x98, 0x19, 0x20, 0x04, 0x48, 0x12, 0x53, 0xca, 0x04, 0x20, 0x08, 0x10, 
	0x09, 0x10, 0x08, 0x90, 0x09, 0x90, 0x0f, 0xf0, 0x10, 0x08, 0x20, 0x04, 0x3f, 0xfc, 0x00, 0x00
};
// 'icon_parksensor', 16x16px
const unsigned char bitmap_icon_carga [] PROGMEM = {
	0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x02, 0x40, 
	0x02, 0x40, 0x01, 0x80, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10, 0x10, 0x08, 0x3f, 0xfc, 0x0f, 0xf0
};
// 'icon_off', 16x16px
const unsigned char bitmap_icon_off [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x22, 0x44, 0x14, 0x28, 0x08, 0x10, 0x14, 0x28, 0x22, 0x44, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x0c, 0x30, 0x10, 0x08, 0x20, 0x04, 0x00, 0x00, 0x00, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 384)
const unsigned char* bitmap_icons[8] = {
  bitmap_icon_garra,
  bitmap_icon_stepper_base,
  bitmap_icon_stepper_s1,
  bitmap_icon_stepper_s2,
  bitmap_icon_stepper_s3,
  bitmap_icon_alerta,
  bitmap_icon_carga,
  bitmap_icon_off
};

// 'screenshot_stepper_base', 128x64px
const unsigned char bitmap_screenshot_stepperb [] 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, 
	0x00, 0x00, 0x00, 0x03, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x02, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x03, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x9c, 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, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x7c, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x07, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x0c, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x38, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x32, 0x4e, 0x94, 0x18, 0x00, 0x00, 
	0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x4b, 0x50, 0x94, 0x24, 0x00, 0x00, 
	0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x4b, 0xd4, 0x94, 0x25, 0x00, 0x00, 
	0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x7a, 0xd2, 0x94, 0x24, 0x00, 0x00, 
	0x00, 0x02, 0x00, 0x03, 0xfc, 0x00, 0x04, 0x00, 0x00, 0x00, 0x4a, 0x52, 0x94, 0x25, 0x00, 0x00, 
	0x00, 0x06, 0x00, 0x0c, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00, 0x4a, 0x4c, 0x67, 0x98, 0x00, 0x00, 
	0x00, 0x0c, 0x00, 0x30, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x08, 0x00, 0xc0, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x18, 0x01, 0x00, 0xf0, 0x18, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x10, 0x02, 0x01, 0x08, 0x04, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x30, 0x02, 0x02, 0x04, 0x04, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x20, 0x04, 0x02, 0x04, 0x02, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x20, 0x04, 0x42, 0x04, 0x02, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x20, 0x08, 0x82, 0x04, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x60, 0x09, 0x81, 0x08, 0x01, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x41, 0xf3, 0x00, 0xf0, 0x00, 0xf8, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x70, 0x42, 0x12, 0x00, 0x00, 0x00, 0x84, 0x20, 0xbb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x50, 0x44, 0x12, 0x00, 0x00, 0x00, 0x82, 0x20, 0xaa, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x50, 0x44, 0x92, 0x00, 0x00, 0x00, 0x92, 0x20, 0xba, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x50, 0xc4, 0x53, 0x00, 0x00, 0x00, 0xa2, 0x30, 0xaa, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x77, 0xc4, 0x11, 0x00, 0x00, 0x00, 0x82, 0x3e, 0xbb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x02, 0x10, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x01, 0xf0, 0x00, 0x00, 0x04, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x08, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x08, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x04, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x04, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x02, 0x00, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x02, 0x00, 0x07, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0x80, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x40, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x30, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x1c, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x1b, 0xfd, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x18, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x19, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x19, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x19, 0x09, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x0c, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x07, 0xfe, 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, 0x02, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x02, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x02, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x01, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x40, 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
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 8320)
const unsigned char* bitmap_screenshots[1] = {
  bitmap_screenshot_stepperb,
};

// 'scrollbar_background', 8x64px
const unsigned char bitmap_scrollbar_background [] PROGMEM = {
  0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 
  0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 
  0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 
  0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00
};

// 'item_sel_outline', 128x21px
const unsigned char bitmap_item_sel_outline [] PROGMEM = {
  0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 
  0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
  0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 
  0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0
};



// ------------------ end generated bitmaps from image2cpp ---------------------------------



const int NUM_ITEMS = 8; // number of items in the list and also the number of screenshots and screenshots with QR codes (other screens)
const int MAX_ITEM_LENGTH = 20; // maximum characters for the item name

char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = {  // array with item names
  { "Garra" }, 
  { "Stepper Base" }, 
  { "Steppers S1" }, 
  { "Stepper S2" }, 
  { "Stepper S3" }, 
  { "Modo: Alerta" },   
  { "Modo: Carga" }, 
  { "Modo: OFF" }
 };

char menu_grados [1][3] = {
  {"45" }
 };
// note - when changing the order of items above, make sure the other arrays referencing bitmaps
// also have the same order, for example array "bitmap_icons" for icons, and other arrays for screenshots and QR codes

#define BUTTON_SELECT_PIN 4 // pin for SELECT button

#define ENCODER_CLK 2
#define ENCODER_DT 3

int ANTERIOR = 0;		// almacena valor anterior de la variable POSICION

volatile int POSICION = 0;	// variable POSICION con valor inicial de 0 y definida
volatile int GRADOS = 0;	// variable GRADOS con valor inicial de 0 y definida

int button_up_clicked = 0; // only perform action when button is clicked, and wait until another press
int button_select_clicked = 0; // same as above
int button_down_clicked = 0; // same as above

int item_selected = 0; // which item in the menu is selected

int item_sel_previous; // previous item - used in the menu screen to draw the item before the selected one
int item_sel_next; // next item - used in the menu screen to draw next item after the selected one

int current_screen = 0;   // 0 = menu, 1 = screenshot, 2 = qr

void setup() {
  u8g.setColorIndex(1);  // set the color to white

  // define pins for buttons
  // INPUT_PULLUP means the button is HIGH when not pressed, and LOW when pressed
  // since it´s connected between some pin and GND
  pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP); // select button
  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);

  attachInterrupt(digitalPinToInterrupt(ENCODER_DT), encoder, LOW);
  
}


void loop() {

  if (current_screen == 0) { // MENU SCREEN

      item_selected = POSICION;
      if (item_selected < 0) { // if first item was selected, jump to last item
        POSICION = NUM_ITEMS-1;

      }
      if (item_selected >= NUM_ITEMS) { // last item was selected, jump to first menu item
        POSICION = 0;
      }

  }


  if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) { // select button clicked, jump between screens
     button_select_clicked = 1; // set button to clicked to only perform the action once
     if (current_screen == 0) {current_screen = 1;} // menu items screen --> screenshots screen
     else {current_screen = 0;} // qr codes screen --> menu items screen
  }
  if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) { // unclick 
    button_select_clicked = 0;
  }

  // set correct values for the previous and next items
  item_sel_previous = item_selected - 1;
  if (item_sel_previous < 0) {item_sel_previous = NUM_ITEMS - 1;} // previous item would be below first = make it the last
  item_sel_next = item_selected + 1;  
  if (item_sel_next >= NUM_ITEMS) {item_sel_next = 0;} // next item would be after last = make it the first


  u8g.firstPage(); // required for page drawing mode for u8g library
  do {

    if (current_screen == 0) { // MENU SCREEN

      // selected item background
      u8g.drawBitmapP(0, 22, 128/8, 21, bitmap_item_sel_outline);

      // draw previous item as icon + label
      u8g.setFont(u8g_font_7x14);
      u8g.drawStr(25, 15, menu_items[item_sel_previous]); 
      u8g.drawBitmapP( 4, 2, 16/8, 16, bitmap_icons[item_sel_previous]);          

      // draw selected item as icon + label in bold font
      u8g.setFont(u8g_font_7x14B);    
      u8g.drawStr(25, 15+20+2, menu_items[item_selected]);   
      u8g.drawBitmapP( 4, 24, 16/8, 16, bitmap_icons[item_selected]);     

      // draw next item as icon + label
      u8g.setFont(u8g_font_7x14);     
      u8g.drawStr(25, 15+20+20+2+2, menu_items[item_sel_next]);   
      u8g.drawBitmapP( 4, 46, 16/8, 16, bitmap_icons[item_sel_next]);  

      // draw scrollbar background
      u8g.drawBitmapP(128-8, 0, 8/8, 64, bitmap_scrollbar_background);

      // draw scrollbar handle
      u8g.drawBox(125, 64/NUM_ITEMS * item_selected, 3, 64/NUM_ITEMS);            

    } 
    else if (current_screen == 1) { // SCREENSHOTS SCREEN
        u8g.drawBitmapP( 0, 0, 128/8, 64, bitmap_screenshots[0]); // draw screenshot
        if(item_selected == 1){
          u8g.setFont(u8g_font_7x14); 
          u8g.drawStr(90, 39, menu_grados[0]);
        }
    }

  } while ( u8g.nextPage() ); // required for page drawing mode with u8g library


}

void encoder()  {
  if(current_screen == 0){
    static unsigned long ultimaInterrupcion = 0;	// variable static con ultimo valor de
						// tiempo de interrupcion
    unsigned long tiempoInterrupcion = millis();	// variable almacena valor de func. millis

    if (tiempoInterrupcion - ultimaInterrupcion > 5) {	// rutina antirebote desestima
							// pulsos menores a 5 mseg.
      if (digitalRead(ENCODER_CLK) == HIGH)			// si B es HIGH, sentido horario
      {
        POSICION-- ;				// incrementa POSICION en 1
      }
      else {					// si B es LOW, senti anti horario
        POSICION++ ;				// decrementa POSICION en 1
      }

      POSICION = min(100, max(-1, POSICION));	// establece limite inferior de 0 y
						// superior de 100 para POSICION
      ultimaInterrupcion = tiempoInterrupcion;	// guarda valor actualizado del tiempo
    }						// de la interrupcion en variable static
  }

  else if(current_screen == 1){
    static unsigned long ultimaInterrupcion2 = 0;	// variable static con ultimo valor de
						// tiempo de interrupcion
    unsigned long tiempoInterrupcion2 = millis();	// variable almacena valor de func. millis

    if (tiempoInterrupcion2 - ultimaInterrupcion2 > 5) {	// rutina antirebote desestima
							// pulsos menores a 5 mseg.
      if (digitalRead(ENCODER_CLK) == HIGH)			// si B es HIGH, sentido horario
      {
        GRADOS-- ;				// incrementa POSICION en 1
      }
      else {					// si B es LOW, senti anti horario
        GRADOS++ ;				// decrementa POSICION en 1
      }

      GRADOS = min(100, max(0, GRADOS));	// establece limite inferior de 0 y
						// superior de 100 para POSICION
      ultimaInterrupcion2 = tiempoInterrupcion2;	// guarda valor actualizado del tiempo
    }						// de la interrupcion en variable static
  }
}