/*
    Rotary Encoder - Interrupt Example
    
    The circuit:
    * encoder pin A to Arduino pin 2
    * encoder pin B to Arduino pin 3
    * encoder ground pin to ground (GND)
*/

#include "Rotary.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Rotary r = Rotary(PIN_PE5, PIN_PE6);

int counter = 0;
const int pushButton = PIN_PE4;

const int DEBOUNCE_DELAY = 50;

// Variables will change:
int lastSteadyState = LOW;       // the previous steady state from the input pin
int lastFlickerableState = LOW;  // the previous flickerable state from the input pin
int currentState;                // the current reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled

void setup() {
  Serial.begin(115200);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();

  
  r.begin();
  pinMode(PIN_PE4, INPUT);

  
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Counter:"));
  display.display();
}

void loop() {
  unsigned char result = r.process();
  if (result == DIR_NONE) {
    // do nothing
  }
  else if (result == DIR_CW) {
    counter++;
    display.clearDisplay(); 
    Serial.print("Count: ");
    Serial.println(counter);
    display.setCursor(0,0);       
    display.setTextSize(2);       
    display.println(F("Counter:")); 
    displayPrint(counter, 'C', 2, 2);
    display.display();
  }
  else if (result == DIR_CCW) {
    counter--;
    display.clearDisplay(); 
    Serial.print("Count: ");
    Serial.println(counter);
    display.setCursor(0,0);      
    display.setTextSize(2);        
    display.println(F("Counter:"));      
    displayPrint(counter, 'C', 2, 2);
    display.display();
  }


  currentState = digitalRead(PIN_PE4);

  if (currentState != lastFlickerableState) {
    lastDebounceTime = millis();
    lastFlickerableState = currentState;
  }

  if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
    if (lastSteadyState == HIGH && currentState == LOW)
      Serial.println("The button is pressed");
    else if (lastSteadyState == LOW && currentState == HIGH)
      Serial.println("The button is released");
    lastSteadyState = currentState;
  }
  
}

void displayPrint(char *slovo, char strana, int riadok, int velkost){
  display.setTextSize(velkost);
  int rdk = 0;

  if(riadok == 0) rdk = 0;
  if(velkost == 1) rdk = riadok * 8;
  else if(velkost == 2) rdk = riadok * 16;
  else if(velkost == 3) rdk = riadok * 24;
  
  if(strana == 'l' || strana == 'L'){
    display.setCursor(0,rdk);
    display.print(slovo);
  }

  if(strana == 'r' || strana == 'R'){
    if(velkost == 1) display.setCursor((128) - strlen(slovo)*6,rdk);
    else if(velkost == 2) display.setCursor((128) - strlen(slovo)*12,rdk);
    else if(velkost == 3) display.setCursor((128) - strlen(slovo)*18,rdk);
    display.print(slovo);
  }

  if(strana == 'c' || strana == 'C'){
    if(velkost == 1) display.setCursor((128/2) - strlen(slovo)*3,rdk);
    else if(velkost == 2) display.setCursor((128/2) - strlen(slovo)*6,rdk);
    else if(velkost == 3) display.setCursor((128/2) - strlen(slovo)*9,rdk);
    display.print(slovo);
  }
  //display.display();
  
}

void displayPrint(int slovo, char strana, int riadok, int velkost){
    char buffer [33];
    displayPrint(itoa(slovo, buffer, 10), strana, riadok, velkost);
}
