import time
from sensor import ParkingSensor

MAX_CARS = 8
MIN_CARS = 0

def wait_for_release(button_func):
    while button_func():
        time.sleep(0.01)

def main():
    sensor = ParkingSensor()
    car_count = 0
    sensor.set_leds(car_count)
    print("System Ready. Press Entrance or Exit buttons.")

    while True:
        if sensor.entrance_pressed():
            if car_count < MAX_CARS:
                car_count += 1
                sensor.set_leds(car_count)
                print("Car Entered. Count:", car_count)
            wait_for_release(sensor.entrance_pressed)

        if sensor.exit_pressed():
            if car_count > MIN_CARS:
                car_count -= 1
                sensor.set_leds(car_count)
                print("Car Exited. Count:", car_count)
            wait_for_release(sensor.exit_pressed)

        time.sleep(0.05)

if __name__ == "__main__":
    main()