from machine import Pin, I2C
import ssd1306
import time
import math

# Setup OLED
i2c = I2C(1, sda=Pin(6), scl=Pin(7))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

WIDTH = 128
HEIGHT = 64

def show(delay=2):
    oled.show()
    time.sleep(delay)

# 1. Concentric rectangles
def rectangles():
    oled.fill(0)
    for i in range(0, 30, 4):
        oled.rect(i, i, WIDTH - 2*i, HEIGHT - 2*i, 1)
    show()

# 2. Crosshatch pattern
def crosshatch():
    oled.fill(0)
    for i in range(0, WIDTH, 8):
        oled.line(i, 0, WIDTH - i, HEIGHT - 1, 1)
        oled.line(i, HEIGHT - 1, WIDTH - i, 0, 1)
    show()

# 3. Checkerboard
def checkerboard():
    oled.fill(0)
    size = 8
    for y in range(0, HEIGHT, size):
        for x in range(0, WIDTH, size):
            if ((x//size + y//size) % 2) == 0:
                oled.fill_rect(x, y, size, size, 1)
    show()

# 4. Radial lines (cool effect)
def radial():
    oled.fill(0)
    cx = WIDTH // 2
    cy = HEIGHT // 2

    for angle in range(0, 360, 15):
        x = int(cx + 60 * math.cos(math.radians(angle)))
        y = int(cy + 30 * math.sin(math.radians(angle)))
        oled.line(cx, cy, x, y, 1)
    show()

# 5. Pixel circle pattern
def circles():
    oled.fill(0)
    for y in range(10, HEIGHT, 18):
        for x in range(10, WIDTH, 18):
            for angle in range(0, 360, 20):
                px = int(x + 6 * math.cos(math.radians(angle)))
                py = int(y + 6 * math.sin(math.radians(angle)))
                oled.pixel(px, py, 1)
    show()

# 6. Moving box (animation test)
def moving_box():
    for x in range(0, WIDTH - 10, 4):
        oled.fill(0)
        oled.rect(0, 0, WIDTH, HEIGHT, 1)
        oled.fill_rect(x, 25, 10, 10, 1)
        oled.show()
        time.sleep(0.05)

# Loop patterns
while True:
    rectangles()
    crosshatch()
    checkerboard()
    radial()
    circles()
    moving_box()