#include <U8g2lib.h>

// Define the OLED display object
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ A5, /* data=*/ A4);

// Constants for display dimensions
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;

// Ball properties
const int BALL_RADIUS = 5;
const int BALL_SPEED = 1;

// Initial ball position
int ballX = SCREEN_WIDTH / 2;
int ballY = SCREEN_HEIGHT / 2;

// Ball movement direction
int ballXDirection = 1;
int ballYDirection = 1;

void setup(void) {
  // Initialize the OLED display
  u8g2.begin();
  u8g2.clearBuffer();
}

void loop(void) {
  // Clear the buffer
  u8g2.clearBuffer();

  // Update ball position
  ballX += BALL_SPEED * ballXDirection;
  ballY += BALL_SPEED * ballYDirection;

  // Check for collision with screen edges
  if (ballX - BALL_RADIUS < 0 || ballX + BALL_RADIUS >= SCREEN_WIDTH) {
    ballXDirection *= -1; // Reverse direction
  }
  if (ballY - BALL_RADIUS < 0 || ballY + BALL_RADIUS >= SCREEN_HEIGHT) {
    ballYDirection *= -1; // Reverse direction
  }

  // Draw the ball
  u8g2.drawCircle(ballX, ballY, BALL_RADIUS);

  // Send the buffer to the OLED display
  u8g2.sendBuffer();

  // Delay to control animation speed
  delay(10);
}