/*
Created by [email protected]
Arduino based interface for PT2399 chip, controlling Time and Modulation.
available on wokwi here: https://wokwi.com/projects/377676155691669505
*/

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define pot_Tempo A1
#define button_Tempo 10
#define led_Tempo A0

#define pot_ModSpeed A2
#define pot_ModDepth A3
#define pot_ModShape A6
#define pot_ModRandom A7


int guiState; // 0=Main_Menu 1=Default View 2=Modify View
bool stateChanged;


const char* potName[] = {
  "Delay Time",
  "Mod Speed",
  "Mod Depth",
  "Mod Shape",
  "Mod Randm"
};

int lastTapState = HIGH; //Tap Tempo Switch


// debouncing variables
unsigned long debounceDurationEnc = 25; // millis
unsigned long debounceDuration = 50; // millis
unsigned long lastButtonChangedTime = millis();
unsigned long lastPotChangedTime = millis();
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers


void setup() {
Serial.begin(9600);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
pinMode(pot_Tempo, INPUT);
pinMode(button_Tempo, INPUT_PULLUP);
pinMode(pot_ModSpeed, INPUT);
pinMode(pot_ModDepth, INPUT);
pinMode(pot_ModShape, INPUT);
pinMode(pot_ModRandom, INPUT);
pinMode(led_Tempo, OUTPUT);

display.display();
delay(2000);
display.clearDisplay();
display.drawRect(0, 0, 10, 10, 1);
display.display();
guiState = 1;
stateChanged = true;
}

void loop() {
  //Serial.println("running code");
  //getButtons();
  //getPots();
  //sendLEDs();
  displayRefresh();
}

void displayRefresh() {
  if (stateChanged == true) {
    drawMain();
    stateChanged = false;
  }
}

void drawMain() {
  //Check guiState 0=Main Menu, 1=d=Default, 2=Modify View
  if (guiState == 0) {
    drawMenu();
  } else if (guiState == 1) { 
    drawDefault();
  } else if (guiState == 2) {
    drawModify();
  }
}

void drawMenu() {

}

void drawDefault() {

}

void drawModify() {

}
/*
void getPots() {
  if (millis() - lastPotChangedTime >= debounceDuration) {  
    int newTapState = digitalRead(buttonTempo);    
    if (newTapState != lastTapState) {
      if (newTapState == LOW) {
      Serial.println("Foot-Switch Pressed");
      } else if (newTapState == HIGH) {
      Serial.println("Foot-Switch Released");
      }
        lastTapState = newTapState;
    }
  lastPotChangedTime = millis();
  }
}

void getButtons() {
  if (millis() - lastButtonChangedTime >= debounceDuration) {  
    int newButtonState = digitalRead(button_Tempo);    
    if (newButtonState != lastButtonState) {
      if (newButtonState == LOW) {
      Serial.println("Foot-Switch Pressed");
      } else if (newFSState == HIGH) {
      Serial.println("Foot-Switch Released");
      }
        lastButtonState = newButtonState;
    }
  lastButtonChangedTime = millis();
  }
}
*/