#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

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define VOLT_PIN A7 //вход для делителя напряжения

const unsigned char scale [] PROGMEM = {
	// 'scale, 123x13px
	0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0xe0, 
	0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x20, 
	0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x20, 
	0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 
	0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0xe0, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x60, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x01, 0x80, 
	0x60, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x01, 0x80, 
	0x60, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x01, 0x80, 
	0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 
	0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80
};

const unsigned char cursor [] PROGMEM = {
	// 'cursor, 8x4px
	0x18, 0x3c, 0x7e, 0xff
};

const unsigned char v_icon [] PROGMEM = {
	// 'v_icon, 10x10px
	0x7f, 0x80, 0xff, 0xc0, 0xce, 0xc0, 0xc5, 0xc0, 0xe5, 0xc0, 0xe1, 0xc0, 0xf3, 0xc0, 0xf3, 0xc0, 
	0xff, 0xc0, 0x7f, 0x80
};

float volts = 0.0;
String str_volts = "00.00";

void setup() {
  Serial.begin(9600);
  pinMode(VOLT_PIN, INPUT);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println("SSD1306 allocation failed");
    for(;;);
  }
  //delay(2000);
  //display.setFont(&FreeSerif9pt7b);
  //display.clearDisplay();
  //display.setTextSize(1);             
  display.setTextColor(WHITE);        
  //display.setCursor(0,20);             
  //display.println("Hello, world!");
  //display.display();
  //delay(2000); 
}

void loop() {
  //Serial.println(analogRead(A7));
  display.clearDisplay();
  display.drawBitmap(3, 2, scale, 123, 13, WHITE);
  display.drawBitmap(map(analogRead(VOLT_PIN), 0, 1023, 0, 120), 18, cursor, 8, 4, WHITE);
  display.drawBitmap(110, 40, v_icon, 10, 10, WHITE);
  display.setCursor(19,30);
  display.setTextSize(3);
  volts = (analogRead(A7)*50.0)/1023;
  str_volts = String(volts, 2);
  if(volts < 10.0){
    str_volts = "0" + str_volts;
  }
  display.println(str_volts);
  display.display();
  // put your main code here, to run repeatedly:

}