from machine import Pin, ADC, I2C, PWM
import ssd1306
import time

# ======================
# OLED SETUP
# ======================
i2c = I2C(0, scl=Pin(5), sda=Pin(4))
oled = ssd1306.SSD1306_I2C(128, 32, i2c)

# ======================
# HARDWARE
# ======================
gas_sensor = ADC(26)
motor = Pin(15, Pin.OUT)

led = PWM(Pin(16))
led.freq(1000)

# ======================
# SENSOR
# ======================
smoothed = 0

def read_gas():
    global smoothed
    raw = gas_sensor.read_u16()

    gas = int((raw - 10000) / 50000 * 100)
    gas = max(0, min(100, gas))

    smoothed = int(smoothed * 0.9 + gas * 0.1)
    return smoothed

# ======================
# LED CONTROL
# ======================
def led_off():
    led.duty_u16(0)

def led_on():
    led.duty_u16(65535)

# FAST BLINK (NON-BLOCKING)
blink_state = False
last_blink = time.ticks_ms()

def handle_led(gas):
    global blink_state, last_blink

    if gas > 70:
        # ⚡ FAST BLINK (every 60ms)
        if time.ticks_diff(time.ticks_ms(), last_blink) > 60:
            blink_state = not blink_state
            last_blink = time.ticks_ms()

        if blink_state:
            led_on()
        else:
            led_off()
    else:
        led_off()

# ======================
# LOADING BAR
# ======================
def loading_bar(text):
    oled.fill(0)
    oled.text(text, 10, 10)
    oled.show()

    for i in range(0, 128, 4):
        oled.fill_rect(0, 25, i, 4, 1)
        oled.show()
        time.sleep(0.01)

# ======================
# AIR SCREEN
# ======================
def air_screen(gas):
    for _ in range(20):  # stay longer + smooth refresh
        handle_led(gas)

        oled.fill(0)
        oled.text("AIR QUALITY", 14, 0)

        # centered value
        val = str(gas) + "%"
        oled.text(val, int((128 - len(val)*6)/2), 12)

        # bar
        width = int(gas * 1.2)
        oled.rect(10, 26, 108, 4, 1)
        oled.fill_rect(12, 27, width, 2, 1)

        oled.show()
        time.sleep(0.05)

# ======================
# GAS SCREEN
# ======================
def gas_screen(gas):
    for _ in range(20):  # smooth + slower
        handle_led(gas)

        oled.fill(0)
        oled.text("LPG STATUS", 20, 0)

        if gas > 70:
            status = "LEAK!"
            motor.value(1)
        else:
            status = "SAFE"
            motor.value(0)

        oled.text(status, int((128 - len(status)*6)/2), 14)

        # animated bar
        width = int(gas * 1.2)
        oled.rect(10, 26, 108, 4, 1)
        oled.fill_rect(12, 27, width, 2, 1)

        oled.show()
        time.sleep(0.05)

# ======================
# BOOT SEQUENCE
# ======================
def boot():
    loading_bar("Starting...")
    loading_bar("Sensors Init...")
    loading_bar("Ready!")

# ======================
# MAIN
# ======================
boot()

while True:
    gas = read_gas()

    air_screen(gas)
    gas_screen(gas)
