// Minimalis kode OLED utk centering text
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 
#define SCREEN_ADDRESS 0x3C

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

String formatAngka(String angka) {
 String output = "";
 int ctr=angka.length();
 int ribu = 0;
 
 while (ctr >= 0) {
    output=angka[ctr]+output;
    if (ribu==3 && ctr>1) 
    {
      output=","+output;
      ribu=0;
    }
    ctr=ctr-1;
    ribu=ribu+1;
 }
 return output;
}

void drawCenterString(String text,int Yaxis) {
  int16_t x1;
  int16_t y1;
  uint16_t width;
  uint16_t height;

  display.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
  if (Yaxis<0)
  {
   // display on horizontal and vertical center   
	display.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
  } else
  {
    // display on horizontal center
	display.setCursor((SCREEN_WIDTH - width) / 2, Yaxis);
  }
  
  display.println(text); // text to display
  display.display();
}

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(115200);

  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  
  drawCenterString("SkwaDrone Salatiga",5);
  drawCenterString("by: Rio",15);
  drawCenterString(formatAngka("123456789"),35);
  
}

void loop() {
  // put your main code here, to run repeatedly:

}