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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

  const int potPin = A0;  //for testing only.
  int potVal;
  int value;
  
void setup() {
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
}

void loop() {
  display.clearDisplay();
  ch1_data();
  ch2_data();
  ch3_data();
  ch4_data();
  auxCh1_data();
  auxCh2_data();
  auxCh3_data();
  auxCh4_data();
  display.display();
}

void ch1_data(){
  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,255);
  
  display.setTextSize(1);
  display.setTextColor(1);
  display.setCursor(5, 0);
  display.print(value); 

  display.drawRect(5, 9, 17, 55, WHITE);

  if (value<512){
    int x = map(value,128,0,36,61);
    for (int i=10; i<17; i++){
      display.drawLine(i, 36 , i, x, 1);
    }
    
   }
  else if (value>512){
    int y = map(value,128,255,36,10);
    for (int i=10; i<17; i++){
      display.drawLine(i, 36 , i, y, 1);
      
    }
    
  }
   
}
void ch2_data(){
  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,255);

  display.setTextSize(1);
  display.setTextColor(1);
  display.setCursor(5+20, 0);
  display.print(value); 

  display.drawRect(5+20, 9, 17, 55, WHITE);

  if (value<512){
    int x = map(value,128,0,36,61);
    for (int i=10; i<17; i++){
      display.drawLine(i+20, 36 , i+20, x, 1);
    }
   }
  else if (value>512){
    int y = map(value,128,255,36,10);
    for (int i=10; i<17; i++){
      display.drawLine(i+20, 36 , i+20, y, 1);
    }
  }

}
void ch3_data(){
  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,255);

  display.setTextSize(1);
  display.setTextColor(1);
  display.setCursor(5+40, 0);
  display.print(value); 

  display.drawRect(5+40, 9, 17, 55, WHITE);

  if (value<512){
    int x = map(value,128,0,36,61);
    for (int i=10; i<17; i++){
      display.drawLine(i+40, 36 , i+40, x, 1);
    }
   }
  else if (value>512){
    int y = map(value,128,255,36,10);
    for (int i=10; i<17; i++){
      display.drawLine(i+40, 36 , i+40, y, 1);
    }
  }

}
void ch4_data(){
  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,255);

  display.setTextSize(1);
  display.setTextColor(1);
  display.setCursor(5+60, 0);
  display.print(value); 

  display.drawRect(5+60, 9, 17, 55, WHITE);

  if (value<512){
    int x = map(value,128,0,36,61);
    for (int i=10; i<17; i++){
      display.drawLine(i+60, 36 , i+60, x, 1);
    }
   }
  else if (value>512){
    int y = map(value,128,255,36,10);
    for (int i=10; i<17; i++){
      display.drawLine(i+60, 36 , i+60, y, 1);
    }
  }

}
void auxCh1_data(){

  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,255);
  display.fillRect(90, 0, 38, 22, 1);
  display.setTextSize(1);
  display.setTextColor(0);
  display.setCursor(95, 3);
  display.print("AUX01");
  display.setCursor(100, 12);
  display.print(value); 
  
}
void auxCh2_data(){

  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,255);
  display.fillRect(90, 24, 38, 22, 1);
  display.setTextSize(1);
  display.setTextColor(0);
  display.setCursor(95, 28);
  display.print("AUX02");
  display.setCursor(100, 37);
  display.print(value); 
  
}
void auxCh3_data(){

  potVal = analogRead(potPin);
  value = map(potVal,0,1023,0,1);
  display.fillRect(90, 48, 38, 16, 1);
  display.drawLine(109, 48 , 109, 64, 0);
  display.drawLine(108, 48 , 108, 64, 0);
  display.setTextSize(2);
  display.setTextColor(0);
  display.setCursor(95, 49);
  display.print(value); 
  
}
void auxCh4_data(){

  potVal = analogRead(potPin);
  //display.drawRect(90, 47, 38, 17, 1);
  //display.drawLine(109, 47 , 109, 64, 1);
  display.setTextSize(2);
  display.setTextColor(0);
  display.setCursor(114, 49);
  if (potVal<450){
  display.print("L");
  } 
  else if (potVal>570){
  display.print("H");
  } 
  else
  display.print("M");
  }