import time
time.sleep(0.1) # Wait for USB to become ready

print("Hello, Pi Pico!")

import utime
from machine import Pin, PWM
import neopixel
import time

# Constants
MAX = 65535

# Logging
class Log:
    @staticmethod
    def i(message):
        print(f"[INFO] {message}")

# StateModel Class
class StateModel:
    def __init__(self):
        self.state = 'idle'  # Possible states: idle, active, alarm, driving, parked

    def set_state(self, state):
        Log.i(f"State changed from {self.state} to {state}")
        self.state = state

# Sensors Class
class Sensors:
    def __init__(self):
        Log.i("Sensors initialized")

    def read_distance(self):
        # Simulated distance reading
        return 50  # Placeholder value for distance

# lcd_api Class
class lcd_api:
    def __init__(self):
        Log.i("LCD API initialized")

    def display_message(self, message):
        Log.i(f"Displaying on LCD: {message}")

# Button Class
class Button:
    def __init__(self, pin):
        self._button = Pin(pin, Pin.IN, Pin.PULL_UP)
        Log.i(f"Button initialized on pin {pin}")

    def is_pressed(self):
        return not self._button.value()

# Buzzer Class
class Buzzer:
    def __init__(self, pin):
        self._buzzer = Pin(pin, Pin.OUT)
        Log.i(f"Buzzer initialized on pin {pin}")

    def buzz(self):
        self._buzzer.on()
        utime.sleep(0.5)
        self._buzzer.off()

# Displays Class
class Displays:
    def __init__(self, lcd):
        self.lcd = lcd

    def show_state(self, state):
        self.lcd.display_message(f"State: {state}")

# Counters Class
class Counters:
    def __init__(self):
        self.trigger_count = 0

    def increment(self):
        self.trigger_count += 1
        Log.i(f"Trigger count: {self.trigger_count}")

# Light Class
class Light:
    def __init__(self, pin, name="Unnamed"):
        Log.i("Light: constructor")
        self._name = name
        self._pin = pin
        self._led = Pin(self._pin, Pin.OUT)

    def on(self):
        Log.i(f"Light: turning on {self._name}")
        self._led.value(1)

    def off(self):
        Log.i(f"Light: turning off {self._name}")
        self._led.value(0)

# LightStrip Class
class LightStrip(Light):
    FILLS = 0
    CHASES = 1
    RAINBOW = 2

    def __init__(self, pin=2, name='Neopixel', numleds=16, brightness=0.5):
        self._name = name
        self._pin = pin
        self._numleds = numleds
        self._brightness = brightness
        self._running = False
        
        Log.i(f'Creating a neopixel {name} on pin {pin} with {numleds} LEDs')
        self._np = neopixel.NeoPixel(machine.Pin(pin), numleds)

    def on(self):
        self._fill((255, 255, 255))  # All LEDs white
        self._np.write()
        Log.i(f'{self._name} ON')

    def off(self):
        self._running = False
        self._clear()
        self._np.write()
        Log.i(f'{self._name} OFF')

    def setColor(self, color, numPixels=-1):
        if numPixels < 0 or numPixels > self._numleds:
            numPixels = self._numleds
        for i in range(numPixels):
            self._np[i] = color
        for i in range(numPixels, self._numleds):
            self._np[i] = (0, 0, 0)
        self._np.write()
        Log.i(f'{self._name} set color to {color}')

    def _clear(self):
        self._np.fill((0, 0, 0))

    def _fill(self, color):
        self._np.fill(color)

# Main Application Logic
def main():
    # Initialize components
    state_model = StateModel()
    sensors = Sensors()
    lcd = lcd_api()
    drive_button = Button(pin=15)  # Button for driving
    park_button = Button(pin=14)    # Button for parking
    buzzer = Buzzer(pin=16)
    displays = Displays(lcd)
    counters = Counters()
    light_strip = LightStrip(pin=2, numleds=16)

    # Main loop
    while True:
        if drive_button.is_pressed():
            state_model.set_state('driving')
            displays.show_state(state_model.state)
            light_strip.on()
            distance = sensors.read_distance()
            if distance < 20:
                state_model.set_state('alarm')
                buzzer.buzz()
                light_strip.setColor((255, 0, 0))  # Set to red for alarm
                displays.show_state(state_model.state)
                counters.increment()
            else:
                light_strip.setColor((0, 255, 0))  # Set to green when safe
                displays.show_state(state_model.state)

        if park_button.is_pressed():
            state_model.set_state('parked')
            light_strip.off()  # Turn off the light strip when parked
            displays.show_state(state_model.state)
            Log.i("Car is parked.")

        utime.sleep(1)

if __name__ == "__main__":
    main()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT