#include "ABlocks_NeoPixel.h"
#include "Wire.h"
#include "ABlocks_LiquidCrystal_I2C.h"
#include "ABlocks_Button.h"
#include "ABLocks_TimerFreeTone.h"

double volumen_x;
double j;
double Play_Stop;
double i_volumen;
boolean b_exit;
Adafruit_NeoPixel neo_pixel = Adafruit_NeoPixel(16,5,NEO_GRB+NEO_KHZ800);
LiquidCrystal_I2C lcd_1(0x27,16,2);
LiquidCrystal_I2C lcd_2(0x27,16,2);
volatile long encoder_2_3_pos=0;
boolean encoder_2_3_A_set =false;
boolean encoder_2_3_B_set =false;
static boolean encoder_2_3_rotating=false;
byte lcd_1_customchar_0[8]={B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};

byte lcd_1_customchar_1[8]={B00000,B01000,B01100,B01110,B01110,B01100,B01000,B00000};

byte lcd_1_customchar_2[8]={B00000,B00000,B01110,B01110,B01110,B01110,B00000,B00000};

byte lcd_1_customchar_3[8]={B00000,B00000,B00100,B01100,B11111,B01100,B00100,B00000};

byte lcd_1_customchar_4[8]={B11111,B11111,B11011,B10011,B00000,B10011,B11011,B11111};

byte lcd_1_customchar_5[8]={B11111,B11111,B10001,B10001,B10001,B10001,B11111,B11111};

byte lcd_1_customchar_6[8]={B11111,B10111,B10011,B10001,B10001,B10011,B10111,B11111};

Button button_debounced_4(4,50);

void fnc_encoder_2_3_intA(){
	if ( encoder_2_3_rotating ) delay (1);
	if( digitalRead(2) != encoder_2_3_A_set ) {
		encoder_2_3_A_set = !encoder_2_3_A_set;
		if ( encoder_2_3_A_set && !encoder_2_3_B_set ) encoder_2_3_pos += 1;
		encoder_2_3_rotating = false;
	}
}

void fnc_encoder_2_3_intB(){
	if ( encoder_2_3_rotating ) delay (1);
	if( digitalRead(3) != encoder_2_3_B_set ) {
		encoder_2_3_B_set = !encoder_2_3_B_set;
		if ( encoder_2_3_B_set && !encoder_2_3_A_set ) encoder_2_3_pos -= 1;
		encoder_2_3_rotating = false;
	}
}

void Animaci_C3_B3n_encendido() {
	delay(200);
	for (j = 0; j <= 16; j=j+1) {
		neo_pixel.setPixelColor(j, neo_pixel.Color(255,255,255));
		neo_pixel.show();
		delay(75);
	}
	delay(200);
	for (j = 16; j >= 0; j=j-1) {
		neo_pixel.setPixelColor(j, neo_pixel.Color(0,0,0));
		neo_pixel.show();
		delay(75);
	}
	lcd_1.clear();
	lcd_1.setCursor(0, 1);
	lcd_1.print(String("(Press to Menu)"));
	lcd_1.setCursor(0, 0);
	lcd_1.print(String("Proyecto Jose D"));
}
void counter() {
	if ((encoder_2_3_pos >= 2)) {
		encoder_2_3_pos = 2;}

	if ((encoder_2_3_pos <= 1)) {
		encoder_2_3_pos = 2;}

}
void Volumen() {
	lcd_1.clear();
	lcd_1.setCursor(0, 0);
	lcd_1.print(String("Volumen set:       "));
	encoder_2_3_pos = volumen_x;int i_volumen_end = (volumen_x - 1);
	for (i_volumen = 0;
	    (0 <= i_volumen_end) ? i_volumen <= i_volumen_end : i_volumen >= i_volumen_end;
	    i_volumen += (0 <= i_volumen_end) ? +1 : -1) {
		lcd_1.setCursor(i_volumen, 1);
		lcd_1.write((byte)0);
		neo_pixel.setPixelColor(i_volumen, neo_pixel.Color(155,155,155));
		neo_pixel.show();
	}
	while ((!button_debounced_4.pressed())) {
		if ((encoder_2_3_pos > 15)) {
			encoder_2_3_pos = 15;}
		else if ((encoder_2_3_pos < 0)) {
			encoder_2_3_pos = 0;}

		Animacion_Ascendente_S_C3_ADmbolo();
		Animacion_Descendente_S_C3_ADmbolo();
		Vol_Buzzer();
	}
	encoder_2_3_pos = 0;}
void Reproductor() {
	lcd_1.clear();
	lcd_1.setCursor(0, 0);
	lcd_1.print(String("Start-Wars"));
	encoder_2_3_pos = 0;if ((Play_Stop == 0)) {
		lcd_1.setCursor(8, 1);
		lcd_1.write((byte)1);
	}
	else if ((Play_Stop == 1)) {
		lcd_1.setCursor(8, 1);
		lcd_1.write((byte)2);
	}

	while (!(((encoder_2_3_pos == 0) && button_debounced_4.pressed()))) {
		if ((encoder_2_3_pos > 1)) {
			encoder_2_3_pos = 1;}
		else if ((encoder_2_3_pos < 0)) {
			encoder_2_3_pos = 0;}

		if ((encoder_2_3_pos == 0)) {
			lcd_1.setCursor(2, 1);
			lcd_1.write((byte)4);
			delay(1000);
			lcd_1.setCursor(2, 1);
			lcd_1.write((byte)3);
			delay(1000);
		}
		else if ((encoder_2_3_pos == 1)) {
			Play_Stop2();
			if ((button_debounced_4.pressed() && (Play_Stop == 0))) {
				delay(2000);
				Play_Stop = (Play_Stop + 1);
			}
			else if ((button_debounced_4.pressed() && (Play_Stop == 1))) {
				TimerFreeToneRtttl(9,(const char *)String("5555# 1111 1111** 2222# 11* 777* 6666* 5555* 4444* 4444** 5555* 6666** 5555").c_str());
				delay(2000);
				Play_Stop = (Play_Stop + 0);
			}

		}

	}
	encoder_2_3_pos = 0;}
void menu() {
	lcd_1.clear();
	encoder_2_3_pos = 1;while ((b_exit == false)) {
		if ((encoder_2_3_pos > 3)) {
			encoder_2_3_pos = 3;}
		else if ((encoder_2_3_pos < 1)) {
			encoder_2_3_pos = 1;}

		if ((encoder_2_3_pos == 1)) {
			lcd_1.setCursor(0, 0);
			lcd_1.print(String("Volumen          "));
			lcd_1.setCursor(0, 1);
			lcd_1.print(String("                                 "));
		}
		else if ((encoder_2_3_pos == 2)) {
			lcd_1.setCursor(0, 0);
			lcd_1.print(String("Reproductor        "));
			lcd_1.setCursor(0, 1);
			lcd_1.print(String("                                 "));
		}
		else if ((encoder_2_3_pos == 3)) {
			lcd_1.setCursor(0, 0);
			lcd_1.print(String("      Exit          "));
			lcd_1.setCursor(0, 1);
			lcd_1.print(String("                                 "));
		}

		if (((encoder_2_3_pos == 1) && button_debounced_4.pressed())) {
			Volumen();
		}
		else if (((encoder_2_3_pos == 2) && button_debounced_4.pressed())) {
			Reproductor();
		}
		else if (((encoder_2_3_pos == 3) && button_debounced_4.pressed())) {
			b_exit = true;
		}

	}
	b_exit = false;
	lcd_1.clear();
	lcd_1.setCursor(0, 0);
	lcd_1.print(String("Proyecto Jose D"));
	lcd_1.setCursor(0, 1);
	lcd_1.print(String("(Press to Menu)"));
	encoder_2_3_pos = 1;}
void Animacion_Ascendente_S_C3_ADmbolo() {
	volumen_x = encoder_2_3_pos;
	lcd_1.setCursor(volumen_x, 1);
	lcd_1.write((byte)0);
	neo_pixel.setPixelColor(volumen_x, neo_pixel.Color(255,255,255));
	neo_pixel.show();
}
void Animacion_Descendente_S_C3_ADmbolo() {
	lcd_1.setCursor(((volumen_x + 1)), 1);
	lcd_1.print(String("                    "));
	neo_pixel.setPixelColor(((volumen_x + 1)), neo_pixel.Color(0,0,0));
	neo_pixel.show();
}

void fnc_dynamic_analogWrite(int _pin, int _e){
	pinMode(_pin,OUTPUT);
	analogWrite(_pin,_e);
}

void Vol_Buzzer() {
	fnc_dynamic_analogWrite(9, volumen_x);
}
void Play_Stop2() {
	if ((Play_Stop == 0)) {
		lcd_1.setCursor(8, 1);
		lcd_1.write((byte)5);
		delay(1000);
		lcd_1.setCursor(8, 1);
		lcd_1.write((byte)2);
		delay(1000);
	}
	else if ((Play_Stop == 1)) {
		lcd_1.setCursor(8, 1);
		lcd_1.write((byte)1);
		delay(1000);
		lcd_1.setCursor(8, 1);
		lcd_1.write((byte)2);
		delay(1000);
	}

}

void setup()
{
  	pinMode(2, INPUT);
	pinMode(3, INPUT);
	pinMode(4, INPUT);
	pinMode(9, OUTPUT);

	digitalWrite(2,HIGH);
	digitalWrite(3,HIGH);
	attachInterrupt(0, fnc_encoder_2_3_intA, CHANGE);
	attachInterrupt(1, fnc_encoder_2_3_intB, CHANGE);

	neo_pixel.begin();
	neo_pixel.clear();
	lcd_1.begin();
	lcd_1.noCursor();
	lcd_1.backlight();
	lcd_2.begin();
	lcd_2.noCursor();
	lcd_2.backlight();
	lcd_1.setCursor(0, 0);
	lcd_1.print(String("Proyecto Jose D"));
	delay(1000);
	Animaci_C3_B3n_encendido();
	encoder_2_3_pos = 1;lcd_1.createChar(0, lcd_1_customchar_0);
	lcd_1.createChar(1, lcd_1_customchar_1);
	lcd_1.createChar(2, lcd_1_customchar_2);
	lcd_1.createChar(3, lcd_1_customchar_3);
	lcd_1.createChar(4, lcd_1_customchar_4);
	lcd_1.createChar(5, lcd_1_customchar_5);
	lcd_1.createChar(6, lcd_1_customchar_6);
	Play_Stop = 0;
	b_exit = false;

}


void loop()
{

  	if (button_debounced_4.pressed()) {
  		menu();
  	}
  	else {
  		counter();
  	}

}