//File: Xiao.ino
//For OLED:
#include <Wire.h>
#include <U8g2lib.h> 

//Select the option that matches the dimensions of the OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C DISPA(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

//Pins for SDA and SCL
#define SDA 4  
#define SCL 5

//pin numbers for the buttons
int BUTTON1 = D10;
int BUTTON2 = D8;

int page = 0;

void setup() {
  pinMode(BUTTON1, OUTPUT); //Forward
  pinMode(BUTTON2, OUTPUT); //Backward
  //Serial communication
  Serial.begin(115200); //Same baud as in interface
  //OLED:
  //SDA and SCL pinout
  pinMode(SDA, OUTPUT);  
  pinMode(SCL, OUTPUT);
  // Set I2C bus speed on board and both displays
  Wire.setClock(100000);
  DISPA.setBusClock(100000);
  //Set I2C Adress (0x78/0x7A, or 0x3C/0x3D) of the OLED
  DISPA.setI2CAddress(0x78);
  //Turn on OLED and get it ready to accept data
  DISPA.begin();
  //Select a font
  DISPA.setFont(u8g2_font_logisoso16_tf );
  //Make sure the display buffer is clear
  DISPA.clearBuffer(); 
}

void loop() {
    //Serial.write(page);
    Serial.println(page);
    
  //OLED
  DISPA.clearBuffer();
  DISPA.setCursor(15, 20); //(X,Y)
  DISPA.print("Landline <3");
  DISPA.setCursor(12, 40); //(X,Y)
  DISPA.print("Page: ");
  DISPA.setCursor(65, 40); //(X,Y)
  DISPA.print(page);
  DISPA.setCursor(72, 40); //(X,Y)
  DISPA.print(" of 6");
  DISPA.setCursor(58, 60); //(X,Y)
  DISPA.print(":D");
  DISPA.sendBuffer(); //Command to show things on display
  //Logic to send what is wanted into the interface
  if (digitalRead(BUTTON1) == HIGH){
    if (page >= 6){ //Compare the page # to return it to 1st page
      page = 0;
    }else{
      page += 1; //Increase page number when clicking button
    }
  }else if (digitalRead(BUTTON2) == HIGH){
    if (page <= 0){//Compare the page # to return it to last page
      page = 6;
    }
    else{
      page -= 1; //Decrease page number when clicking button
    }
  }
  delay(80); //Otherwise the pages turn TOO fast
}