import time
import neopixel
from machine import Pin, I2C, PWM
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

GP2 = 2
GP3 = 3
GP4 = 4
GP5 = 5
GP6 = 6
GP7 = 7
GP8 = 8
GP9 = 9
GP10 = 10
GP11 = 11
GP12 = 12
GP13 = 13
buzzer_pin = Pin(28)
pwm = PWM(buzzer_pin)
base_freq = 1000
pwm.freq(base_freq)

gLed1 = Pin(GP2, Pin.OUT)
gLed2 = Pin(GP3, Pin.OUT)
gLed3 = Pin(GP4, Pin.OUT)
rLed1 = Pin(GP5, Pin.OUT)

def emit_tick():
    pwm.duty_u16(32767)  # Augmenter le devoir pour émettre un son
    time.sleep(0.05)  # Durée de l'impulsion (peut nécessiter des ajustements)
    pwm.duty_u16(0)  # Couper le son
    time.sleep(0.05)

def switch1_handler_in(pin):
    gLed1.on()
def switch1_handler_out(pin):
    gLed1.off()

def switch2_handler_in(pin):
    gLed2.on()
def switch2_handler_out(pin):
    gLed2.off()

def switch3_handler_in(pin):
    gLed3.on()
def switch3_handler_out(pin):
    gLed3.off()

def switch4_handler_in(pin):
    rLed1.on()
def switch4_handler_out(pin):
    rLed1.off()


switch_1_IN = machine.Pin(GP6, machine.Pin.IN, machine.Pin.PULL_UP)
switch_1_OUT = machine.Pin(GP10, machine.Pin.IN, machine.Pin.PULL_UP)
switch_2_IN = machine.Pin(GP7, machine.Pin.IN, machine.Pin.PULL_UP)
switch_2_OUT = machine.Pin(GP11, machine.Pin.IN, machine.Pin.PULL_UP)
switch_3_IN = machine.Pin(GP8, machine.Pin.IN, machine.Pin.PULL_UP)
switch_3_OUT = machine.Pin(GP12, machine.Pin.IN, machine.Pin.PULL_UP)
switch_4_IN = machine.Pin(GP9, machine.Pin.IN, machine.Pin.PULL_UP)
switch_4_OUT = machine.Pin(GP13, machine.Pin.IN, machine.Pin.PULL_UP)

switch_1_IN.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch1_handler_in)
switch_1_OUT.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch1_handler_out)
switch_2_IN.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch2_handler_in)
switch_2_OUT.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch2_handler_out)
switch_3_IN.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch3_handler_in)
switch_3_OUT.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch3_handler_out)
switch_4_IN.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch4_handler_in)
switch_4_OUT.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING, handler=switch4_handler_out)

I2C_ADDR = 39
I2C_NUM_ROWS=2
I2C_NUM_COLS=16

sda = Pin(0)
scl = Pin(1)
i2c = I2C(0, sda=sda, scl=scl, freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)

lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Salut les looser")
lcd.move_to(0, 1)
lcd.putstr("GraphicsRCS")

button1 = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_UP)
button2 = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)
button3 = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_UP)
button4 = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_UP)

np = neopixel.NeoPixel(machine.Pin(21), 1)
np[0] = (255, 0, 0)
np.write()


while True:
    emit_tick()
    if not button1.value():
        print('Button 1 pressed!')
    if not button2.value():
        print('Button 2 pressed!')
    if not button3.value():
        print('Button 3 pressed!')
    if not button4.value():
        print('Button 4 pressed!')
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT