import board
import neopixel
import pygame
import time
import os
import RPi.GPIO as GPIO

# Initiera Pygame
pygame.init()


# https://electropeak.com/learn/tutorial-raspberry-pi-gpio-programming-using-python-full-guide/

#initiera LED-strip med 300 led
num_of_pixels = 300
pixels = neopixel.NeoPixel(board.D21, num_of_pixels)


#resetta alla IO
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT) 
GPIO.output(21, 0)
GPIO.setmode(GPIO.BCM)
GPIO.setup(40, GPIO.OUT) 
GPIO.output(40, 0)

#slå på nr 0,1,299 led
pixels.fill((0,0,0))  # stäng av alla LED
pixels.show()         # stäng av alla LED


def setpixel(r,g,b,nr):
    pixels[nr] = (r,g,b)
    pixels.show()


def spela_ljud(filnamn):
    pygame.mixer.init()
    pygame.mixer.music.load(filnamn)
    pygame.mixer.music.set_volume(1.0)
    pygame.mixer.music.play()




pygame.mixer.init()
pygame.mixer.music.load("ljud.mp3")
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.play()


# Initiera joystickar
pygame.joystick.init()

# Kontrollera antalet anslutna joystickar
joystick_count = pygame.joystick.get_count()


if joystick_count < 2:
    print("Mindre än två joystickar anslutna. Kontrollera anslutningarna.")
    pygame.quit()    
    exit()

# Anslut till de första två joystickarna
joystickB = pygame.joystick.Joystick(0)
joystickC = pygame.joystick.Joystick(1)
joystickA = pygame.joystick.Joystick(2)

joystickA.init()
joystickB.init()
joystickC.init()    


spela_ljud("ljud.mp3")

pixels.fill((0,0,0))  # stäng av alla LED
pixels.show()         # stäng av alla LED


#definiera en funktion per knapp
def JB_knapp_0():
    print ("Joystick B, Knapp 1")
    setpixel(255,255,255,10)
    setpixel(255,255,255,11)

def JB_knapp_1():
    print ("Joystick B, Knapp 2")
    setpixel(255,255,255,14)
    setpixel(255,255,255,15)

def JB_knapp_2():
    print ("Joystick B, Knapp 3")
    setpixel(255,255,255,18)
    setpixel(255,255,255,19)

def JB_knapp_3():
    print ("Joystick B, Knapp 4")

def JB_knapp_4():
    print ("Joystick B, Knapp 5")

def JB_knapp_5():
    print ("Joystick B, Knapp 6")

def JB_knapp_6():
    print ("Joystick B, Knapp 7")

def JB_knapp_7():
    print ("Joystick B, Knapp 8")
    pixels.fill((0,0,0))  # stäng av alla LED
    pixels.show()         # stäng av alla LED

def JB_knapp_8():
    print ("Joystick B, Knapp 9")


def JB_knapp_9():
    print ("Joystick B, Knapp 10")

def JB_knapp_10():
    print ("Joystick B, Knapp 11")
    
def JB_knapp_11():
    print ("Joystick B, Knapp 12")
    

def JA_knapp_0():
    print ("Joystick A, Knapp 1")

def JA_knapp_1():
    print ("Joystick A, Knapp 2")

def JA_knapp_2():
    print ("Joystick A, Knapp 3")

def JA_knapp_3():
    print ("Joystick A, Knapp 4")

def JA_knapp_4():
    print ("Joystick A, Knapp 5")

def JA_knapp_5():
    print ("Joystick A, Knapp 6")

def JA_knapp_6():
    print ("Joystick A, Knapp 7")

def JA_knapp_7():
    print ("Joystick A, Knapp 8")

def JA_knapp_8():
    print ("Joystick A, Knapp 9")

def JA_knapp_9():
    print ("Joystick A, Knapp 10")

def JA_knapp_10():
    print ("Joystick A, Knapp 11")
    
def JA_knapp_11():
    print ("Joystick A, Knapp 12")


def JC_knapp_0():
    print ("Joystick C, Knapp 1")

def JC_knapp_1():
    print ("Joystick C, Knapp 2")

def JC_knapp_2():
    print ("Joystick C, Knapp 3")

def JC_knapp_3():
    print ("Joystick C, Knapp 4")

def JC_knapp_4():
    print ("Joystick C, Knapp 5")

def JC_knapp_5():
    print ("Joystick C, Knapp 6")

def JC_knapp_6():
    print ("Joystick C, Knapp 7")

def JC_knapp_7():
    print ("Joystick C, Knapp 8")

def JC_knapp_8():
    print ("Joystick C, Knapp 9")

def JC_knapp_9():
    print ("Joystick C, Knapp 10")

def JC_knapp_10():
    print ("Joystick C, Knapp 11")
    
def JC_knapp_11():
    print ("Joystick C, Knapp 12")




#knappfunktioner

knappfunktioner_JA = [JA_knapp_0,JA_knapp_1,JA_knapp_2,JA_knapp_3,JA_knapp_4,JA_knapp_5,JA_knapp_6,JA_knapp_7,JA_knapp_8,JA_knapp_9,JA_knapp_10,JA_knapp_11]
knappfunktioner_JB = [JB_knapp_0,JB_knapp_1,JB_knapp_2,JB_knapp_3,JB_knapp_4,JB_knapp_5,JB_knapp_6,JB_knapp_7,JB_knapp_8,JB_knapp_9,JB_knapp_10,JB_knapp_11]
knappfunktioner_JC = [JC_knapp_0,JC_knapp_1,JC_knapp_2,JC_knapp_3,JC_knapp_4,JC_knapp_5,JC_knapp_6,JC_knapp_7,JC_knapp_8,JC_knapp_9,JC_knapp_10,JC_knapp_11]


try:
    while True:
        # Hämta alla händelser
        for event in pygame.event.get():
            if event.type == pygame.JOYBUTTONDOWN:
                if event.joy == 0:
                    knapp = event.button
                    print(f"Joystick B ,- Knapp {event.button} trycks ned.")
                    knappfunktioner_JB[knapp]()
            if event.type == pygame.JOYBUTTONDOWN:
                if event.joy == 1:
                    knapp = event.button
                    print(f"Joystick C - Knapp {event.button} trycks ned.")
                    knappfunktioner_JC[knapp]()
            if event.type == pygame.JOYBUTTONDOWN:
                if event.joy == 2:
                    knapp = event.button
                    print(f"Joystick A - Knapp {event.button} trycks ned.")            
                    knappfunktioner_JA[knapp]()
                            
        # Pausa lite för att inte överbelasta processorn
        time.sleep(0.01)        

except KeyboardInterrupt:    
    print("Avslutar programmet.")

finally:
    #Terminering
    pixels.fill((0,0,0))  # stäng av alla LED
    pixels.show()         # stäng av alla LED
    pygame.quit()
