#ifdef __AVR__
#include <avr/power.h>
#endif
#include <HID-Project.h>
//#include <Keyboard.h>
//#include <Mouse.h>
#include <Keypad.h>
#include <Encoder.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_NeoPixel.h>
#include "macro_keyboard.h"

void setup() {
  pinMode(KEYBOARD_MODEBUTTON_PIN, INPUT_PULLUP);  // initialize the mode-button pin as a input
  Consumer.begin();                   // initialize the MediaPlayer functions
  Keyboard.begin();                   // initialize the keypad
  pixels.begin();                     // initialize the NeoPixels

  lcd.init();                         // initialize the 4x20 lcd
  lcd.backlight();                    //turn on the backlight
  lcd.begin(LCD_NB_COLUMNS , LCD_NB_ROWS);
  lcd.setCursor(0, 0); lcd.print("Macro KB RC V2.0");
  lcd.setCursor(0, 1); lcd.print("(c) 2020 Ryan Bates");

  positionEncoderA = RotaryEncoderA.read()/ROTARY_SENSITIVITY; 
  positionEncoderB = RotaryEncoderA.read()/ROTARY_SENSITIVITY; 

  for (int i = 0; i <= (KEYBOARD_ROWS * KEYBOARD_COLS); i++) {
    pixels.setPixelColor(i, pixels.Color( 15,  15,  15));
    pixels.show();
    delay(20);
  }
/*  
  for (int i = 0; i <= (KEYBOARD_ROWS * KEYBOARD_COLS); i++) {
    pixels.setPixelColor(i, pixels.Color(127,   0,   0));
    pixels.show();
    delay(25);
  }
  delay(100);
  for (int i = (KEYBOARD_ROWS * KEYBOARD_COLS); i >= 0; i--) {
    pixels.setPixelColor(i, pixels.Color(  0, 127,   0));
    pixels.show();
    delay(25);
  }
  delay(100);
  for (int i = 0; i <= (KEYBOARD_ROWS * KEYBOARD_COLS); i++) {
    pixels.setPixelColor(i, pixels.Color(  0,   0, 127));
    pixels.show();
    delay(25);
  }
  delay(100);
  for (int i = (KEYBOARD_ROWS * KEYBOARD_COLS); i >= 0; i--) {
    pixels.setPixelColor(i, pixels.Color(  0,   0,   0));
    pixels.show();
    delay(25);
  }
  delay(100);
*/
  colorUpdate = 1;
}

void loop() {
  key = keypad.getKey();

  //  mouseMove = (analogRead(KEYBOARD_POT_PIN)); //reading the analog input, pot = pin A1
  //  mouseMove = map(mouseMove, 0,1023, 1,124); //remap the analog pot values fron 1 to 124

  switch (keyboardMode()) {                  // switch between keyboard configurations:
    case KEYBOARD_MODE0:
      keyboard_Mode0();
      encoderA_Mode0();
      encoderB_Mode0();
      colors_Mode0();
      display_Mode0();
      break;
    case KEYBOARD_MODE1:
      keyboard_Mode1();
      encoderA_Mode1();
      encoderB_Mode1();
      colors_Mode1();
      display_Mode1();
      break;
    case KEYBOARD_MODE2:
      keyboard_Mode2();
      encoderA_Mode2();
      encoderB_Mode2();
      colors_Mode2();
      display_Mode2();
      break;
    case KEYBOARD_MODE3:
      keyboard_Mode3();
      encoderA_Mode3();
      encoderB_Mode3();
      colors_Mode3();
      display_Mode3();
      break;
  }
  delay(1);  // delay in between reads for stability
}

char keyboardMode() {
  buttonState = digitalRead(KEYBOARD_MODEBUTTON_PIN);

  if (buttonState != lastButtonState) { // compare the buttonState to its previous state
    if (buttonState == LOW) { // if the state has changed, increment the counter
      // if the current state is LOW then the button cycled:
      keyboardState++;
      colorUpdate = 1;      // set the color change flag ONLY when we know the mode button has been pressed.
      // Saves processor resources from updating the neoPixel colors all the time
    }
    delay(10); // Delay a little bit to avoid bouncing
  }
  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop
  if (keyboardState > KEYBOARD_LAST_MODE) {       //reset the counter after 4 presses CHANGE THIS FOR MORE MODES
    keyboardState = KEYBOARD_MODE0;
  }

  return keyboardState;
}
