#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h>
#include <FlexiTimer2.h>
#include <EEPROM.h>

#define SCREEN_ADDRESS 0x3C

#define PPQN 24
#define PULSE_LENGTH 12 //ms (with 12 ms you can't get higher than 208bpm)
#define MAXBPM 200 //250 at 24ppqn with 5ms pulse will be 50/50 square wave
#define MINBPM 20

#define INPUT_PIN 2 //needs to be an interrupt pin
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define ANALOGUE_INPUT_1_PIN A2
#define ANALOGUE_INPUT_2_PIN A1

const int outsPins[6] = {6, 11, 7, 10, 8, 9};

const int clockModes[18] = {-24, -16, -12, -8, -6, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32}; //positive - divide, negative - multiply, 0 - off

unsigned int bpm = 130;

struct channel {
  unsigned int mode;
  unsigned int random;
  bool modulationChannel;   //0 - A1, 1 - A2
  int modulationRange;
};

channel channels[6] = {  //array of channel settings
  { 8, 0, 0, 0 },
  { 8, 0, 0, 0 },
  { 8, 0, 0, 0 },
  { 8, 0, 0, 0 },
  { 8, 0, 0, 0 },
  { 8, 0, 0, 0 }
};

int outsClocksCounts[6];
int playingModes[6]; //actual channel modes array updated from channels each beat

int clockCount = 0;

int pulseClockCount = 0;
int pulseCount = 0;
int pulsePeriod;
bool isPlaying = 0;
int needToResetChannel;
bool beatCounted = false;
bool pulseCounted = false;

int masterClockMode = 0; // 0 - internal, 1 - external 24ppqn
bool externalPulseCounted = true;

int displayTab = 0;
int displayTabOld;
int insideTab = 0;
bool playBtnPushed = false;

int a1Input = 0;
int a2Input = 0;

int encPositionOld = 0;
unsigned long encPressedTime;
unsigned long encReleasedTime;
bool encPressRegistered;

Adafruit_SSD1306 display(128, 64, &Wire, -1);
RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03);

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

  //check last bit in eeprom to know if settings were stored 
  if (EEPROM.read(1023) == 'H') {
    EEPROM.get(0, bpm);
    EEPROM.get(sizeof(int), channels);
  } else {
    saveState();
    EEPROM.write(1023, 'H');
  }

  pinMode(ENC_BTN_PIN, INPUT_PULLUP);
  pinMode(START_STOP_BTN_PIN, INPUT_PULLUP);
  pinMode(START_STOP_BTN_PIN, ANALOGUE_INPUT_1_PIN);
  pinMode(INPUT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INPUT_PIN), externalClock, RISING);

  for (int i=0; i<6; i++) {
    pinMode(outsPins[i], OUTPUT);
  }

  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  updateScreen();
  updateTiming();

  FlexiTimer2::set(1, 1.0/1000, clock); // 1.0/1000 = 1ms period
  FlexiTimer2::start();
}

void loop() {
  checkInputs();
}

void clock() {
  if (isPlaying) {

    // Action on each pulse
    if (pulseClockCount == 0 && !pulseCounted) {

      //modulation 
      for (int i = 0; i<6; i++) {
        int mod;
        if (!channels[i].modulationChannel) {
          mod = a1Input;
        } else {
          mod = a2Input;
        }
        mod = map (mod, 0, 1023, 0, channels[i].modulationRange);
        playingModes[i] = clockModes[channels[i].mode - mod]; //subtrackting because the innitiall array is backwards
      }

      //divider      
      if (pulseCount == 0 && !beatCounted) {
        for (int i = 0; i<6; i++) {
          playingModes[i] = clockModes[channels[i].mode]; //updated here to prevent sync problems for multipliers
          if (playingModes[i] > 0) {                    
            if (outsClocksCounts[i] == 0) {         //Pulse on 0
              if (channels[i].random == 0 || random(10) > channels[i].random) { 
                digitalWrite(outsPins[i], HIGH);
              }
            }
            if (outsClocksCounts[i] < (playingModes[i] - 1)) {
              outsClocksCounts[i]++;
            } else {
              outsClocksCounts[i] = 0;        
            }
          }
        }
        beatCounted = true;
      }

      //multiplier
      for (int i = 0; i<6; i++) {
        if (playingModes[i] < 0) {               
          if (outsClocksCounts[i] == 0) {   //Pulse on 0
            if (channels[i].random == 0 || random(10) > channels[i].random) { //random
              digitalWrite(outsPins[i], HIGH);
            }
          }
          if (outsClocksCounts[i] < (PPQN / abs(playingModes[i])) - 1) {
            outsClocksCounts[i]++;
          } else {
            outsClocksCounts[i] = 0;        
          }
        } 
      }
      pulseCounted = true;
    }

    switch (masterClockMode) {
      case 0:  //internal
        if (pulseClockCount == 0) {
          pulseCount++;
          beatCounted = false;
          pulseCounted = false;
        } 
        break;
      case 1: //external 24ppqn
        if (externalPulseCounted == false) {
          pulseCount++;
          externalPulseCounted = true;
          beatCounted = false;
          pulseCounted = false;
        }
        break;
    }
    if (pulseClockCount < pulsePeriod) {  
      pulseClockCount++;
    } else {
      pulseClockCount = 0;
    }
    if (pulseCount >= PPQN) {
      pulseCount = 0;
    }

    // pull low all outputs after set pulse length
    if (pulseClockCount >= PULSE_LENGTH) {
      for (int i = 0; i<6; i++) { 
        digitalWrite(outsPins[i], LOW);
      }
    }
  }
}

void externalClock() {
  externalPulseCounted = false;
}

void updateTiming() {
  pulsePeriod = 60000 / (bpm * PPQN);
}

void resetClocks() {
  for (int i = 0; i<6; i++) {
    outsClocksCounts[i] = 0;
    digitalWrite(outsPins[i], LOW); //to avoid stuck leds
  }
}

void saveState() {
  EEPROM.put(0, bpm);
  EEPROM.put(sizeof(int), channels);
}

void checkInputs() {

  //encoder button
  if (!digitalRead(ENC_BTN_PIN) && !encPressRegistered) {
    encPressRegistered = true;
    encPressedTime = millis();
  } else if (digitalRead(ENC_BTN_PIN) && encPressRegistered) {
    encPressRegistered = false;
    encReleasedTime = millis();

    if (encReleasedTime - encPressedTime < 500) {   // press shorter than .5s switches tabs
      if (insideTab == 0) {
        displayTabOld = displayTab;
        displayTab++;
        if (displayTab>6) {
          displayTab = 0;      
        }
      } else if (insideTab < 2) {
        insideTab ++;
      } else {
        insideTab = 1;
      }
      updateScreen();
    } else if (encReleasedTime - encPressedTime < 2000 && displayTab != 0) { // longer press (<2s) and switches random mode, longer than 2s presses are ignored
      if (insideTab == 0) {
        insideTab = 1;
      } else {
        insideTab = 0;
      }
      updateScreen();
    }
    else if (encReleasedTime - encPressedTime < 2000 && displayTab == 0) { // switch to external clock mode
      if (masterClockMode == 0) {
        masterClockMode = 1;
      } else {
        masterClockMode = 0;
      }
      updateScreen();
    }
  }

  //encoder
  encoder.tick();
  int encPosition = encoder.getPosition();
  if (encPositionOld != encPosition) {
    int change = encPositionOld - encPosition;
    if (displayTab == 0 && masterClockMode == 0) {
      bpm = bpm + change;
      if (bpm > MAXBPM) {
        bpm = MAXBPM;
      } else if (bpm < MINBPM) {
        bpm = MINBPM;
      }
      updateTiming();
    } else if (displayTab != 0 && insideTab == 0) {
      channels[displayTab-1].mode = channels[displayTab-1].mode - change;
      if (channels[displayTab-1].mode == 65535) {   //65535 is 0-1 for unsigned vars
        channels[displayTab-1].mode = 0;
      } else if (channels[displayTab-1].mode > (sizeof(clockModes)/sizeof(int)) - 1) {
        channels[displayTab-1].mode = (sizeof(clockModes)/sizeof(int)) - 1;
      }        
      needToResetChannel = displayTab-1;
    } else if (displayTab != 0 && insideTab == 1) {         //random
      channels[displayTab-1].random = channels[displayTab-1].random + change;
      if (channels[displayTab-1].random > 9 || channels[displayTab-1].random < 0) {
        channels[displayTab-1].random = 0;
      }
    } else if (displayTab != 0 && insideTab == 2) {         //modulation
      channels[displayTab-1].modulationRange = channels[displayTab-1].modulationRange + change;
      if (channels[displayTab-1].modulationRange > 6 || channels[displayTab-1].modulationRange < -6) {
        channels[displayTab-1].modulationChannel = !channels[displayTab-1].modulationChannel;
        channels[displayTab-1].modulationRange = 0;
      }
    }
    updateScreen();
    encPositionOld = encPosition;
  }

  //play button
  if (!digitalRead(START_STOP_BTN_PIN) && !playBtnPushed) {
    isPlaying = !isPlaying;
    resetClocks();
    playBtnPushed = true;
    saveState();
  } else if (digitalRead(START_STOP_BTN_PIN) && playBtnPushed) {
    playBtnPushed = false;
  }

  //modulations
  a1Input = analogRead(ANALOGUE_INPUT_1_PIN);
  a2Input = analogRead(ANALOGUE_INPUT_2_PIN);
}

void updateScreen() {
  display.clearDisplay();

  //Tabs
  display.drawRect(0, 0, 128, 2, SSD1306_WHITE);
  display.setCursor(0,2);
  display.setTextSize(1);
  if (displayTab == 0) {
    display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
    display.print(F(" "));
    display.setTextColor(SSD1306_WHITE);
    display.print(F(" bpm "));
  } else {
    display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
    display.print(F(" bpm"));
  }
  
  for (int i = 1; i <= 6; i++) {
    if (displayTab == i) {
      display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
      display.print(" ");
      display.setTextColor(SSD1306_WHITE);
      display.print(" ");
      display.print(i);
      display.print(" ");
    } else {
      display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
      display.print(" ");
      display.print(i);
    }
  }
  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
  display.fillRect(108, 2, 20, 8, SSD1306_WHITE);
  display.println();
  display.println();
  display.fillRect(0, 10, 128, 2, SSD1306_WHITE);
  
  //Content
  display.setCursor(4,16);
  display.setTextSize(3);
  display.setTextColor(SSD1306_WHITE);
  if (displayTab == 0 && masterClockMode == 0) {
    display.print(bpm);
    display.println(F("bpm")); 
  } else if (displayTab == 0 && masterClockMode != 0) {
    display.println(F("EXT")); 
  } else {
      if (clockModes[channels[displayTab-1].mode] == 0) {
        display.print(F("OFF"));
      } else if (clockModes[channels[displayTab-1].mode]>0) {
        display.print(F("/"));
        display.print(abs(clockModes[channels[displayTab-1].mode]));
      } else {
        display.print(F("x"));
        display.print(abs(clockModes[channels[displayTab-1].mode]));
      }
  }
  display.println();
  display.setTextSize(1); 
  display.println();

  //Extra params
  display.setCursor(58,16);
  display.setTextSize(1);
  if (displayTab != 0) {
    if (insideTab == 1) { 
      display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
    } else {
      display.setTextColor(SSD1306_WHITE);
    }
    display.print(F(" RND:"));
    if (channels[displayTab-1].random > 0)  {
      display.print(channels[displayTab-1].random);
      display.print(F("0% "));
    } else {
      display.print(F("Off "));
    }
    if (insideTab == 2) { 
      display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
    } else {
      display.setTextColor(SSD1306_WHITE);
    }
    display.setCursor(58,30);
    display.print(F(" MOD:"));
    if (channels[displayTab-1].modulationChannel && channels[displayTab-1].modulationRange != 0)  {
      display.print(F("A2 "));
      if (channels[displayTab-1].modulationRange > 0) {
        display.print(F("+"));
      }
      display.print(channels[displayTab-1].modulationRange);
      display.print(F(" "));
    } else if (!channels[displayTab-1].modulationChannel && channels[displayTab-1].modulationRange != 0) {
      display.print(F("A1 "));
      if (channels[displayTab-1].modulationRange > 0) {
        display.print(F("+"));
      }
      display.print(channels[displayTab-1].modulationRange);
      display.print(F(" "));
    } else {
      display.print(F("Off "));
    }
  }

  display.display();
}