#include "ErriezRotaryFullStep.h"
#include <BfButton.h>

// Connect rotary pins to the DIGITAL or ANALOG pins of the Arduino board
// Use A0..A7 when using analog pins
#define ROTARY2_PIN1        5
#define ROTARY2_PIN2        4
#define ROTARY2_BUTTON_PIN  6
#define ROTARY1_PIN1        2
#define ROTARY1_PIN2        3
#define ROTARY1_BUTTON_PIN  13
BfButton btn2(BfButton::STANDALONE_DIGITAL, ROTARY2_BUTTON_PIN, true, LOW);
BfButton btn(BfButton::STANDALONE_DIGITAL, ROTARY1_BUTTON_PIN, true, LOW);
// Configure a number of rotaries
#define NUM_ROTARIES        2

// Initialize full step rotary encoder with internal pull-up pins enabled
// and default sensitivity=100
RotaryFullStep rotaries[NUM_ROTARIES] = {
    RotaryFullStep(ROTARY1_PIN1, ROTARY1_PIN2),
    RotaryFullStep(ROTARY2_PIN1, ROTARY2_PIN2)
};

// Global variables
int pinStateLast[NUM_ROTARIES];

// Forward declarations


void setup()
{
    // Initialize Serial port
    Serial.begin(115200);
    while (!Serial) {
        ;
    }
    Serial.println(F("\nExample multiple polled full step Rotary Encoders"));
    Serial.println(F("Press the rotary button to change sensitivity"));
 btn.onPress(pressHandler)
        .onDoublePress(pressHandler) // default timeout
        .onPressFor(pressHandler, 500); // custom timeout for 1 second

    btn2.onPress(pressHandler2)
        .onDoublePress(pressHandler2) // default timeout
        .onPressFor(pressHandler2, 500); // custom timeout for 1 second
}

void pressHandler2 (BfButton *btn2, BfButton::press_pattern_t pattern) {
  switch (pattern) {
    case BfButton::SINGLE_PRESS:
      Serial.println("delete char");
      break;
      
    case BfButton::DOUBLE_PRESS:
      Serial.println("character special case toggle probably");
      break;
      
    case BfButton::LONG_PRESS:
      Serial.println("multi delete possibly");
      break;
  }
}

void pressHandler (BfButton *btn, BfButton::press_pattern_t pattern) {
switch (pattern) {
    case BfButton::SINGLE_PRESS:
      Serial.println("print char");
        break;
    case BfButton::DOUBLE_PRESS:
      Serial.println("upper case toggle");
        break;

    case BfButton::LONG_PRESS:
      Serial.println("go to menu");  
        break;

}
}

void loop()
{
    btn2.read();
    btn.read();
    // Handle multiple Rotary Encoders
    for (uint8_t i = 0; i < NUM_ROTARIES; i++) {
        // Read rotary state
        int rotaryState = rotaries[i].read();

        // Print count value when rotary changed
        if ((rotaryState < 0)) {
            if(i==0){Serial.println("next char");}
            
            else{Serial.println("cursor forward");}
        }
        else if(rotaryState > 0)
        {
          if(i==0){Serial.println("previous char");}
            
          else{Serial.println("cursor back");}
        }
    }
}