/*
           /\
          / ^\ DECAY
  ATTACK /  | \_____________
        /   |     SUSTAIN  |\
       /    |VOLUME        | \ RELEASE
      /_____v______________|__\
     |<----- KEYPRESS ---->|
     |<--------- TIME -------->|
*/

#include <Adafruit_SSD1306.h> // https://github.com/adafruit/Adafruit_SSD1306
#define WIDTH 128
#define HEIGHT 64
#define I2C_ADDRESS 0x3C
Adafruit_SSD1306 display(WIDTH, HEIGHT, &Wire, -1);

bool color; // black or white
int minx = 0, miny = 0, maxx = 127, maxy = 63; // OLED

int vol, volx, // volume
    tim, timx, // time
    att, attx, // attack
    dec, decx, // decay
    sus, susx, // sustain
    rel, relx; // release

byte buzpin = 12, ledpin = 13;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, I2C_ADDRESS);
  display.clearDisplay();
  display.display();
  // display.setTextSize(1);
  // display.setTextColor(WHITE);
  // display.setCursor(55, 50);
  // display.print("PRF");
  display.display();
}

void loop() {
  readpots();

  for (int i = 0; i < 2; i++) {
    color = 1 - i;
    drawadsr(color);
    if (color)
      display.display();
  }
}

void readpots() {
  vol = map(analogRead(A6), 0, 1023, maxy, 0); // volume
  tim = map(analogRead(A7), 0, 1023, 0, maxx / 2); // time (PRF)
  att = map(analogRead(A0), 0, 1023, 0, WIDTH / 5); // max width is 1/5 of trace
  dec = map(analogRead(A1), 0, 1023, 0, WIDTH / 5); // max width is 1/5 trace
  sus = map(analogRead(A2), 0, 1023, maxy, vol); // max width is full width
  rel = map(analogRead(A3), 0, 1023, WIDTH / 5, 0); // max width is 1/5 trace
}

void drawadsr(bool col) {
  display.drawLine(maxx / 2 - tim, maxy - 2, maxx / 2 + tim, maxy - 2, col); // time/prf
  display.drawLine(minx, maxy, att, vol, col); // attack
  display.drawLine(att, vol, att + dec, sus, col); // decay
  display.drawLine(att + dec, sus, maxx - rel, sus, col); // suspend
  display.drawLine(maxx - rel, sus, maxx, maxy, col); // release
}
volume
time/PRF
attack
decay
sustain
release