from matrix_8x8 import matrix_8x8
from graphics import *
from machine import Pin
import rp2
import utime
import random
import adc


# codage de 8 etats possibles sur les 3 switches
sw1=Pin(6,Pin.IN,Pin.PULL_UP)
sw2=Pin(7,Pin.IN,Pin.PULL_UP)
sw3=Pin(8,Pin.IN,Pin.PULL_UP)
def etat_switches():
    return sw1.value()+sw2.value()*2 +sw3.value()*4

# affiche les 6 matrices
def show_cube():
    sm0.put(matrix0.array() ,8)
    sm1.put(matrix1.array() ,8)
    sm2.put(matrix2.array() ,8)
    sm3.put(matrix3.array() ,8)
    sm4.put(matrix4.array() ,8)
    sm5.put(matrix5.array() ,8)
    utime.sleep_ms(100)
    
def show_alea() :
#     print('show♥ alea')
    for i in range(0,10):
        xyz=random.randint(10,61)
        matrix0.set_color(BLACK)
        matrix0.set_alea(num=xyz % 60)
        matrix1.set_color(BLACK)
        matrix1.set_alea(num=xyz % 60)
        matrix2.set_color(BLACK)
        matrix2.set_alea(num=xyz % 60)
        matrix3.set_color(BLACK)
        matrix3.set_alea(num=xyz % 60)
        matrix4.set_color(BLACK)
        matrix4.set_alea(num=xyz % 60)
        matrix5.set_color(BLACK)
        matrix5.set_alea(num=xyz % 60)
        show_cube()
        utime.sleep_ms(200)
    return

def show_spirale():
#     print('show spi♥rale')
    for i in range(1,64,3):
        matrix0.set_spirale(random.choice(COLORS),max=i)
        matrix1.set_spirale(random.choice(COLORS),max=i)
        matrix2.set_spirale(random.choice(COLORS),max=i)
        matrix3.set_spirale(random.choice(COLORS),max=i)
        matrix4.set_spirale(random.choice(COLORS),max=i)
        matrix5.set_spirale(random.choice(COLORS),max=i)
        show_cube()
    for i in range(1,64,4):
        matrix0.set_spirale(random.choice(COLORS),max=i,invert=True)
        matrix1.set_spirale(random.choice(COLORS),max=i,invert=True)
        matrix2.set_spirale(random.choice(COLORS),max=i,invert=True)
        matrix3.set_spirale(random.choice(COLORS),max=i,invert=True)
        matrix4.set_spirale(random.choice(COLORS),max=i,invert=True)
        matrix5.set_spirale(random.choice(COLORS),max=i,invert=True)
        show_cube()    
    return

def show_nombre():
#     print('show no♥mbre')
    for i in range(1,10):
        matrix0.set_nombre(accel.x()%100, color=random.choice(COLORS),background=BLACK)
        matrix1.set_nombre(accel.x()%100, color=random.choice(COLORS),background=BLACK)
        matrix2.set_nombre(accel.x()%100, color=random.choice(COLORS),background=BLACK)
        matrix3.set_nombre(accel.x()%100, color=random.choice(COLORS),background=BLACK)
        matrix4.set_nombre(accel.x()%100, color=random.choice(COLORS),background=BLACK)
        matrix5.set_nombre(accel.x()%100, color=random.choice(COLORS),background=BLACK)
        show_cube()
        utime.sleep_ms(200)
    return

def show_de():
#     print('show♥ de')
    for i in range(1,10):
        matrix0.set_de(0,random.choice(COLORS))
        matrix1.set_de(1,random.choice(COLORS))
        matrix2.set_de(2,random.choice(COLORS))
        matrix3.set_de(3,random.choice(COLORS))
        matrix4.set_de(4,random.choice(COLORS))
        matrix5.set_de(5,random.choice(COLORS))
        show_cube()
        utime.sleep_ms(200)
    return

def show_rainbow():
#     print('show ra♥inbow')
    for i in range(1,10):
        shift= accel.x()
        matrix0.set_rainbow(shift)
        matrix1.set_rainbow(shift)
        matrix2.set_rainbow(shift)
        matrix3.set_rainbow(shift)
        matrix4.set_rainbow(shift)
        matrix5.set_rainbow(shift)
        show_cube()
        utime.sleep_ms(200)
    return
def show_carre():
#     print('show carr♥e')
    for i in range (1,5):
        matrix0.set_carre(random.choice(COLORS),i)
        matrix1.set_carre(random.choice(COLORS),i)
        matrix2.set_carre(random.choice(COLORS),i)
        matrix3.set_carre(random.choice(COLORS),i)
        matrix4.set_carre(random.choice(COLORS),i)
        matrix5.set_carre(random.choice(COLORS),i)
        show_cube()
        utime.sleep_ms(400)
    return
def show_char():
#     print('show c♥har')
    for i in range (1,5):
        matrix0.set_char(random.randint(64,91),random.choice(COLORS))
        matrix1.set_char(random.randint(64,91),random.choice(COLORS))
        matrix2.set_char(random.randint(64,91),random.choice(COLORS))
        matrix3.set_char(random.randint(64,91),random.choice(COLORS))
        matrix4.set_char(random.randint(64,91),random.choice(COLORS))
        matrix5.set_char(random.randint(64,91),random.choice(COLORS))
        show_cube()
        utime.sleep_ms(400)
    return
def show_lines():
#     print('show lines')
    matrix0.set_color(BLACK)
    matrix1.set_color(BLACK)
    matrix2.set_color(BLACK)
    matrix3.set_color(BLACK)
    matrix4.set_color(BLACK)
    matrix5.set_color(BLACK)
    for i in range (0,7):
        matrix0.set_line(0,0,i,7,random.choice(COLORS))
        matrix1.set_line(0,0,i,7,random.choice(COLORS))
        matrix2.set_line(0,0,i,7,random.choice(COLORS))
        matrix3.set_line(0,0,i,7,random.choice(COLORS))
        matrix4.set_line(0,0,i,7,random.choice(COLORS))
        matrix5.set_line(0,0,i,7,random.choice(COLORS))
        show_cube()
        utime.sleep_ms(200)
        
#le fifo RX/TX est groupé en un seul fifo de 8 mots de 32 bits
@rp2.asm_pio(fifo_join=rp2.PIO.JOIN_TX,sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812_asm():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    label("bitloop")
    out(x, 1)               .side(0)    [T3 - 1]
    jmp(not_x, "do_zero")   .side(1)    [T1 - 1]
    jmp("bitloop")          .side(1)    [T2 - 1]
    label("do_zero")
    nop()                   .side(0)    [T2 - 1]
    wrap()
 # une machine a etats sur chacune des 6 broches de commande
 # rp2040 a 8 machines disponibles
sm0 = rp2.StateMachine(0, ws2812_asm, freq=8_000_000, sideset_base=Pin(0, Pin.PULL_DOWN))
sm1 = rp2.StateMachine(1, ws2812_asm, freq=8_000_000, sideset_base=Pin(1, Pin.PULL_DOWN))
sm2 = rp2.StateMachine(2, ws2812_asm, freq=8_000_000, sideset_base=Pin(2, Pin.PULL_DOWN))
sm3 = rp2.StateMachine(3, ws2812_asm, freq=8_000_000, sideset_base=Pin(3, Pin.PULL_DOWN))
sm4 = rp2.StateMachine(4, ws2812_asm, freq=8_000_000, sideset_base=Pin(4, Pin.PULL_DOWN))
sm5 = rp2.StateMachine(5, ws2812_asm, freq=8_000_000, sideset_base=Pin(5, Pin.PULL_DOWN))
sm0.active(1)
sm1.active(1)
sm2.active(1)
sm3.active(1)
sm4.active(1)
sm5.active(1)

matrix0 = matrix_8x8(sm0,0.008) # faible luminosité pour limiter le courant de l'accu
matrix1 = matrix_8x8(sm1,0.008)
matrix2 = matrix_8x8(sm2,0.008)
matrix3 = matrix_8x8(sm3,0.008)
matrix4 = matrix_8x8(sm4,0.008)
matrix5 = matrix_8x8(sm5,0.008)

# liste de focntions pour tirage aleatoire
# fonction repétée pour augmenter sa proba
TIRAGE= (show_alea,show_lines,show_spirale,show_char,show_alea,show_nombre,show_de,show_alea,show_carre,show_rainbow)

if __name__ == "__main__":
    accel= adc.accelero()
    xyz= (accel.x()>>8) -152 # lecture cumulee des 3 axes de l'accellero
    while True:
        while True:
            #color=random.choice(COLORS)
            if etat_switches()  ==0 : show_alea()
            elif etat_switches()==1 : show_spirale()
            elif etat_switches()==2 : show_nombre()
            elif etat_switches()==3 : show_de()
            elif etat_switches()==4 : show_rainbow()
            elif etat_switches()==5 : show_carre()
            elif etat_switches()==6 : show_char()
            else:random.choice(TIRAGE)()

    
    

