#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "image.h"
#define lebar 128
#define tinggi 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define enter 13
#define kiri 12
#define atas 11
#define bawah 10
#define kanan 9
Adafruit_SSD1306 oled(lebar, tinggi, &Wire, OLED_RESET);
int kursorPos[] = {59,27,69,37,5,5};
int xoxPos[] = {114,1,126,13,13,13};
int closePos[] = {1,53,10,63,10,10};
int kalkulatorPos[] = {98,1,110,13,13,13};
int papanXox[][6] = {
{32,0,96,64,64,64},
{33,1,51,19,0,0},{54,1,73,19,0,0},{76,1,94,19,0,0},
{33,22,51,41,0,0},{54,22,73,41,0,0},{76,22,94,41,0,0},
{33,44,51,62,0,0},{54,44,73,62,0,0},{76,44,73,63,0,0}
};
int kalkulatorBoard[][6] = {
{41,27,87,64,46,37},
{42,55,49,62,0,0},{51,55,58,62,0,0},{60,55,67,62,0,0},{51,55,58,62,0,0},{69,55,76,62,0,0},{78,55,85,62,0,0},
{42,46,49,53,0,0},{51,46,58,53,0,0},{60,46,67,53,0,0},{51,46,58,53,0,0},{69,46,76,53,0,0},{78,46,85,53,0,0},
{42,37,49,44,0,0},{51,37,58,44,0,0},{60,37,67,44,0,0},{51,37,58,44,0,0},{69,37,76,44,0,0},{78,37,85,44,0,0},
{42,28,49,35,0,0},{51,28,58,35,0,0},{60,28,67,35,0,0},{51,28,58,35,0,0},{69,28,76,35,0,0},{78,28,85,35,0,0},
};
int xA = 0;
int yA = 1;
int xB = 2;
int yB = 3;
int xS = 4;
int yS = 5;
int kotakXox;
int angka1;
int angka2;
char userInput;
String perhitungan = "";
int hasil;
bool tahap = 1;
bool masuk = 0;
bool kalkulatorCon = false;
bool xoxCon = false;
bool xoxSingle = false;
bool xoxMulti = false;
bool windowCon = true;
bool xoxO = false;
bool xoxX = false;
bool mulai = false;
bool play = false;
void printCenter(String text,String mode,int tinggiTeks = 0,int ukuranText = 1,String warna = "putih") {
int16_t x1, y1;
uint16_t w, h;
uint16_t textColor;
if (warna == "putih") {
textColor = SSD1306_WHITE;
} else if (warna == "hitam") {
textColor = SSD1306_BLACK;
} else if (warna == "invert") {
textColor = SSD1306_INVERSE;
}
if (mode == "normal") {
oled.getTextBounds(text, 0, tinggiTeks, &x1, &y1, &w, &h);
while (w > lebar && text.length() > 0) {
text.remove(text.length() - 1);
text.trim();
text += "...";
oled.getTextBounds(text, 0, tinggiTeks, &x1, &y1, &w, &h);
}
int x = (lebar - w) / 2;
oled.setTextSize(ukuranText);
oled.setTextColor(textColor);
oled.setCursor(x, tinggiTeks);
oled.println(text);
} else if (mode == "full") {
oled.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
while (w > lebar && text.length() > 0) {
text.remove(text.length() - 1);
text.trim();
text += "...";
oled.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
}
int x = (lebar - w) / 2;
int y = (tinggi - h) / 2;
oled.setTextSize(ukuranText);
oled.setTextColor(textColor);
oled.setCursor(x, y);
oled.println(text);
}
}
void loading(int lama = 15) {
for (int i = 0; i < lama; i++) {
oled.clearDisplay();
printCenter("Loading. -","full");
oled.display();
delay(60);
oled.clearDisplay();
printCenter("Loading.. /","full");
oled.display();
delay(60);
oled.clearDisplay();
printCenter("Loading... |","full");
oled.display();
delay(60);
oled.clearDisplay();
printCenter("Loading.. \\","full");
oled.display();
delay(60);
}
}
void window() {
oled.clearDisplay();
oled.drawBitmap(xoxPos[xA],xoxPos[yA],xoxIcon,xoxPos[xS],xoxPos[yS],SSD1306_WHITE);
oled.drawBitmap(kalkulatorPos[xA],kalkulatorPos[yA],kalkulatorIcon,kalkulatorPos[xS],kalkulatorPos[yS],SSD1306_WHITE);
oled.drawBitmap(kursorPos[xA],kursorPos[yA],kursorIcon,kursorPos[xS],kursorPos[yS],SSD1306_WHITE);
oled.display();
}
void xox() {
if (xoxCon) {
oled.clearDisplay();
oled.drawBitmap(closePos[xA],closePos[yA],closeIcon,closePos[xS],closePos[yS],SSD1306_WHITE);
oled.drawBitmap(kursorPos[xA],kursorPos[yA],kursorIcon,kursorPos[xS],kursorPos[yS],SSD1306_WHITE);
printCenter("TicTacToe","normal",10);
printCenter("SinglePlayer","normal",30);
printCenter("MultiPlayer","normal",44);
oled.display();
} else if (xoxSingle) {
oled.clearDisplay();
oled.drawBitmap(closePos[xA],closePos[yA],panahKiri,closePos[xS],closePos[yS],SSD1306_WHITE);
oled.drawBitmap(kursorPos[xA],kursorPos[yA],kursorIcon,kursorPos[xS],kursorPos[yS],SSD1306_WHITE);
if (play) {
oled.drawBitmap(papanXox[0][xA],papanXox[0][yA],papan,papanXox[0][xS],papanXox[0][yS],SSD1306_WHITE);
oled.drawBitmap(closePos[xA],closePos[yA],panahKiri,closePos[xS],closePos[yS],SSD1306_WHITE);
oled.drawBitmap(kursorPos[xA],kursorPos[yA],kursorIcon,kursorPos[xS],kursorPos[yS],SSD1306_WHITE);
} else {
printCenter("SinglePlayer","normal",10);
printCenter("Play","normal",30);
}
oled.display();
} else if (xoxMulti) {
oled.clearDisplay();
oled.drawBitmap(closePos[xA],closePos[yA],panahKiri,closePos[xS],closePos[yS],SSD1306_WHITE);
oled.drawBitmap(kursorPos[xA],kursorPos[yA],kursorIcon,kursorPos[xS],kursorPos[yS],SSD1306_WHITE);
printCenter("Masih Kosong","full");
oled.display();
}
}
void kalkulatorFun() {
oled.clearDisplay();
printCenter("Kalkulator","normal");
if (masuk) {
printCenter(String(angka1),"normal",18);
}
oled.drawBitmap(closePos[xA],closePos[yA],closeIcon,closePos[xS],closePos[yS],SSD1306_WHITE);
oled.drawBitmap(kursorPos[xA],kursorPos[yA],kursorIcon,kursorPos[xS],kursorPos[yS],SSD1306_WHITE);
oled.drawBitmap(kalkulatorBoard[0][xA],kalkulatorBoard[0][yA],kalkulatorUi,kalkulatorBoard[0][xS],kalkulatorBoard[0][yS],SSD1306_WHITE);
oled.display();
}
void setup() {
Serial.begin(115200);
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("OLED gagal dimulai"));
while (true);
}
pinMode(atas, INPUT_PULLUP);
pinMode(bawah, INPUT_PULLUP);
pinMode(kanan, INPUT_PULLUP);
pinMode(kiri, INPUT_PULLUP);
pinMode(enter, INPUT_PULLUP);
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.drawBitmap(0,0,start2,lebar,tinggi,1);
oled.display();
delay(5000);
loading();
window();
}
void loop() {
if (digitalRead(kanan) == LOW && kursorPos[xA] < lebar - 6) {
kursorPos[xA] += 3;
}
if (digitalRead(kiri) == LOW && kursorPos[xA] > 0) {
kursorPos[xA] -= 3;
}
if (digitalRead(bawah) == LOW && kursorPos[yA] < tinggi - 6) {
kursorPos[yA] += 3;
}
if (digitalRead(atas) == LOW && kursorPos[yA] > 0) {
kursorPos[yA] -= 3;
}
if (windowCon) {
if (digitalRead(enter) == LOW) {
if (kursorPos[xA]>=xoxPos[xA]&&kursorPos[xA]<=xoxPos[xB]&&kursorPos[yA]>=xoxPos[yA]&&kursorPos[yA]<=xoxPos[yB]) {
windowCon = false;
loading();
xoxCon = true;
} else if (kursorPos[xA]>=kalkulatorPos[xA]&&kursorPos[xA]<=kalkulatorPos[xB]&&kursorPos[yA]>=kalkulatorPos[yA]&&kursorPos[yA]<=kalkulatorPos[yB]) {
windowCon = 0;
loading();
kalkulatorCon = 1;
}
}
window();
} else if (xoxCon || xoxSingle || xoxMulti) {
if (digitalRead(enter) == LOW) {
if (xoxCon) {
if (kursorPos[xA]>=closePos[xA]&&kursorPos[xA]<=closePos[xB]&&kursorPos[yA]>=closePos[yA]&&kursorPos[yA]<=closePos[yB]) {
xoxCon = false;
xoxSingle = false;
xoxMulti = false;
loading();
windowCon = true;
} else if (kursorPos[yA]>=30&&kursorPos[yA]<=36) {
xoxCon = false;
loading();
xoxSingle = true;
} else if (kursorPos[yA]>=44&&kursorPos[yA]<=50) {
xoxCon = false;
loading();
xoxMulti = true;
}
} else if (xoxSingle) {
if (play) {
if (kursorPos[xA]>=closePos[xA]&&kursorPos[xA]<=closePos[xB]&&kursorPos[yA]>=closePos[yA]&&kursorPos[yA]<=closePos[yB]) {
loading();
play = 0;
}
} else {
if (kursorPos[xA]>=closePos[xA]&&kursorPos[xA]<=closePos[xB]&&kursorPos[yA]>=closePos[yA]&&kursorPos[yA]<=closePos[yB]) {
xoxSingle = 0;
loading();
xoxCon = 1;
} else if (kursorPos[yA]>=30&&kursorPos[yA]<=36) {
loading();
play = 1;
}
}
} else if (xoxMulti) {
if (kursorPos[xA]>=closePos[xA]&&kursorPos[xA]<=closePos[xB]&&kursorPos[yA]>=closePos[yA]&&kursorPos[yA]<=closePos[yB]) {
xoxMulti = 0;
loading();
xoxCon = 1;
}
}
}
xox();
} else if (kalkulatorCon) {
if (digitalRead(enter) == LOW) {
if (kursorPos[xA]>=closePos[xA]&&kursorPos[xA]<=closePos[xB]&&kursorPos[yA]>=closePos[yA]&&kursorPos[yA]<=closePos[yB]) {
kalkulatorCon = 0;
masuk = 0;
loading();
windowCon = 1;
}
if (tahap) {
if (kursorPos[xA]>=kalkulatorBoard[1][xA]&&kursorPos[xA]<=kalkulatorBoard[1][xB]&&kursorPos[yA]>=kalkulatorBoard[1][yA]&&kursorPos[yA]<=kalkulatorBoard[1][yB]) {
masuk = 1;
userInput = '0';
} else if (kursorPos[xA]>=kalkulatorBoard[2][xA]&&kursorPos[xA]<=kalkulatorBoard[2][xB]&&kursorPos[yA]>=kalkulatorBoard[2][yA]&&kursorPos[yA]<=kalkulatorBoard[2][yB]) {
masuk = 1;
userInput = '1';
} else if (kursorPos[xA]>=kalkulatorBoard[3][xA]&&kursorPos[xA]<=kalkulatorBoard[3][xB]&&kursorPos[yA]>=kalkulatorBoard[3][yA]&&kursorPos[yA]<=kalkulatorBoard[3][yB]) {
masuk = 1;
userInput = '2';
}
if (isdigit(userInput)) {
angka1 = angka1 * 10 + (userInput - '0');
} else {
tahap = 0;
perhitungan += userInput;
}
}
}
kalkulatorFun();
}
delay(60);
}