#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C                    // OLED i2c address
#define OLED_RESET -1  
#include <ESP32RotaryEncoder.h>

const uint8_t DI_ENCODER_A   = 6;
const uint8_t DI_ENCODER_B   = 14;
const int8_t  DI_ENCODER_SW  = 12;
const int8_t  DO_ENCODER_VCC = -1;
int switchEnterEsc=0;
long lastEncValue = 0;
int myRotaryValue=0;
unsigned long lasttime;
long rotaryValue;
RotaryEncoder rotaryEncoder( DI_ENCODER_A, DI_ENCODER_B, DI_ENCODER_SW, DO_ENCODER_VCC );



Adafruit_SSD1306 oled(128, 64, &Wire, OLED_RESET);
// Change these to the actual pin numbers that
// you've connected your rotary encoder to




String jmena[] {"C3LO", "C3HI", "Cis3LO", "Cis3HI", "D3LO", "D3HI", "Dis3LO",  "Dis3HI", "E3LO", "E3HI"};
int hodnoty[] {0, 250, 0, 350, 0, 250, 0, 350, 0, 660};



void buttonRotate(long rotaryValue) {
	Serial.printf( "RotaryValue: %ld\n", rotaryValue );
	//myRotaryValue=rotaryValue;
	if(rotaryValue >=0 && rotaryValue < 10){
	 oled.clearDisplay();
	 oled.setTextSize(2);
	 oled.setCursor(0,0);
	 oled.setTextColor(WHITE);
	 oled.print(jmena[rotaryValue]); 
	 oled.print(" = ");
	 oled.println(hodnoty[rotaryValue]);
	 oled.display();
	}
	
}

void buttonCallback(long duration) {
	Serial.print( "boop!" );
	if (switchEnterEsc==0){
   switchEnterEsc=1;
	 lastEncValue=rotaryValue;
	 rotaryEncoder.setBoundaries( 0, 1000, false );
   rotaryEncoder.setEncoderValue(hodnoty[rotaryValue]);
	 rotaryEncoder.
	 Serial.printf( "RotaryryryryValue: %ld\n", hodnoty[rotaryValue] );
	}
	else{
   switchEnterEsc=0;
   rotaryEncoder.setBoundaries( 0, 9, true );
   rotaryEncoder.setEncoderValue(lastEncValue);
	 Serial.printf( "Back");
	}
}

void setup()
{
	Serial.begin( 115200 );
	rotaryEncoder.setEncoderType( EncoderType::HAS_PULLUP );
	rotaryEncoder.setBoundaries( 0, 9, true );
	rotaryEncoder.onTurned( &buttonRotate );
	rotaryEncoder.onPressed( &buttonCallback );
	rotaryEncoder.begin();

  if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


}

void loop()
{
delay(200);
}