#include <Adafruit_SSD1306.h>
#include <Bounce2.h>



Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
float num;
int button1 = 4;
int button2 = 16;
int val1, val2,ii;
Bounce button_1 = Bounce(button1, 10);
Bounce button_2 = Bounce(button2, 10);//設定防彈跳
// 'car-battery', 32x32px
const unsigned char epd_bitmap_car_battery [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xfc, 0x3f, 0xe0, 
	0x0f, 0xfe, 0x7f, 0xf0, 0x0e, 0x0e, 0x70, 0x70, 0x0e, 0x0e, 0x70, 0x70, 0x3e, 0x0f, 0xf0, 0x7c, 
	0x7e, 0x0f, 0xf0, 0x7e, 0x78, 0x00, 0x00, 0x1e, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 
	0xe0, 0x00, 0x03, 0x07, 0xe0, 0x00, 0x07, 0x07, 0xe0, 0x00, 0x07, 0x07, 0xe7, 0xf8, 0x1f, 0xe7, 
	0xe7, 0xf8, 0x1f, 0xe7, 0xe0, 0x00, 0x07, 0x07, 0xe0, 0x00, 0x07, 0x07, 0xe0, 0x00, 0x03, 0x07, 
	0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 
	0xe0, 0x00, 0x00, 0x07, 0x70, 0x00, 0x00, 0x0e, 0x7c, 0x00, 0x00, 0x3e, 0x3f, 0xff, 0xff, 0xfc, 
	0x1f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char epd_bitmap_home [] PROGMEM = {
	0x00, 0x07, 0xe0, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x3c, 0x3c, 0x00, 
	0x00, 0x78, 0x1e, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x01, 0xe0, 0x07, 0x80, 0x03, 0xc0, 0x03, 0xc0, 
	0x07, 0x80, 0x01, 0xe0, 0x0f, 0x00, 0x00, 0xf0, 0x1e, 0x00, 0x00, 0x78, 0x3c, 0x00, 0x00, 0x3c, 
	0x78, 0x00, 0x00, 0x1e, 0xf0, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 
	0xe0, 0x00, 0x00, 0x07, 0xe0, 0x01, 0x80, 0x07, 0xe0, 0x0f, 0xf0, 0x07, 0xe0, 0x1f, 0xf8, 0x07, 
	0xe0, 0x3c, 0x3c, 0x07, 0xe0, 0x38, 0x1c, 0x07, 0xe0, 0x70, 0x0e, 0x07, 0xe0, 0x70, 0x0e, 0x07, 
	0xe0, 0x70, 0x0e, 0x07, 0xe0, 0x70, 0x0e, 0x07, 0xe0, 0x70, 0x0e, 0x07, 0xe0, 0x70, 0x0e, 0x07, 
	0xe0, 0x70, 0x0e, 0x07, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xfc
};
const unsigned char epd_bitmap_clouds_sun [] PROGMEM = {
	0x00, 0x00, 0x70, 0x00, 0x00, 0x08, 0x70, 0x80, 0x00, 0x1c, 0x71, 0xc0, 0x00, 0x1e, 0x71, 0xc0, 
	0x00, 0x0e, 0x73, 0x80, 0x00, 0x07, 0x73, 0x80, 0x00, 0x07, 0xff, 0x0c, 0x00, 0x03, 0xff, 0x3e, 
	0x00, 0x03, 0x87, 0xfc, 0x00, 0xf0, 0x03, 0xf0, 0x03, 0xfc, 0x01, 0xc0, 0x07, 0xfe, 0x00, 0xc0, 
	0x0f, 0x00, 0x00, 0xff, 0x1e, 0x00, 0x00, 0xff, 0x1c, 0x00, 0x00, 0xff, 0x18, 0x0f, 0xe1, 0xc0, 
	0x18, 0x3f, 0xf1, 0xe0, 0x18, 0x3c, 0xf9, 0xf8, 0x3c, 0x70, 0x3c, 0x7c, 0x7c, 0xe0, 0x1c, 0x3e, 
	0xe0, 0xe0, 0x0e, 0x0c, 0xe0, 0xe0, 0x0f, 0x80, 0xe0, 0xe0, 0x0f, 0xc0, 0xe1, 0xe0, 0x01, 0xc0, 
	0x63, 0xe0, 0x00, 0xe0, 0x63, 0x80, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 
	0x07, 0x00, 0x01, 0xc0, 0x03, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0x80, 0x00, 0xff, 0xfe, 0x00
};

void setup() {
  Serial.begin(115200);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setRotation(0);
  //------------------------

  //-----------------------
}

void loop() {
  float voltage = (float)analogRead(36) / 4096 * 4.1 * 28205 / 27000;//電壓計算公式
  
  display.clearDisplay();
  if (button_1.update() == true && button_1.read() == HIGH){
        ii = ii + 4;
    Serial.println(ii);
    if(ii >= 32){
      ii = 0;
    }
  }
  Slider(ii);//滑條
switch(ii){
  case 0:
  home ();

  break;
  case 4:
  Power_conversion(voltage);//電量
  battery();
  break;
  case 8:
  weather ();
  break;

}

  delay(100);
}


















void battery(void){
  int i=50;
  display.drawBitmap(0,0,epd_bitmap_car_battery, 32, 32, 1);
  display.drawRect(i,0, 50,11, 1);
  display.setTextSize(1);
  display.setCursor(i+4, 2);
  display.print("Voltage");
  display.display();

}
void home (void){
  int i=50;
  display.drawBitmap(0,0,epd_bitmap_home, 32, 32, 1);
  display.drawRect(i,0, 50,11, 1);
  display.setTextSize(1);
  display.setCursor(i+14, 2);
  display.print("Main");
  display.display();
}
void weather (void){
  int i=50;
  display.drawBitmap(0,0,epd_bitmap_clouds_sun, 32, 32, 1);
  display.drawRect(i,0, 50,11, 1);
  display.setTextSize(1);
  display.setCursor(i+5, 2);
  display.print("Weather");
  display.display();
}

void Power_conversion(float voltage){
  num = voltage;
  int numAsInt = static_cast<int>(num * 10);
  display.setTextSize(2);    // 設定文字大小
  display.setCursor(40, 16);  // 設定起始座標
  display.print(voltage);
  display.setTextSize(1);
    if (numAsInt < 29){
          display.setCursor(90,22);        // 設定起始座標
          display.print("0%");
          display.setCursor(90,14);        // 設定起始座標
          display.print("NC");//Need charging
  }else if(numAsInt < 30){
          display.setCursor(90,22);        // 設定起始座標
          display.print("20%");
  }else if(numAsInt < 32){
          display.setCursor(90,22);        // 設定起始座標
          display.print("20%");
  }else if(numAsInt < 34){
          display.setCursor(90,22);        // 設定起始座標
          display.print("40%");
  }else if(numAsInt < 36){
          display.setCursor(90,22);        // 設定起始座標
          display.print("60%");
  }else if(numAsInt < 40){
          display.setCursor(90,22);        // 設定起始座標
          display.print("80%");
  }else{
          display.setCursor(90,22);        // 設定起始座標
          display.print("100%");
  }
}

void Slider(int i){//滑條
  display.drawLine(124, 0, 124, 32, 1);//測試

  display.fillRect(122,ii, 5,4, 1);//測試
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
pot1:GND
pot1:SIG
pot1:VCC
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
led1:A
led1:C
r1:1
r1:2