import machine, neopixel, time
np=neopixel.NeoPixel(machine.Pin(0), 96)

n=np.n

position=0
brightness=0.5

def fade(np):
    for i in range(0, 4 * 256, 8):
        for j in range(n):
            if (i // 256) % 2 == 0:
                val = i & 0xff
            else:
                val = 255 - (i & 0xff)
            np[j] = (val, val, 0)
        np.write()
        
def bounce(np):
    for i in range(4 * n):
        for j in range(n):
            np[j] = (0, 0, 128)
        if (i // n) % 2 == 0:
            np[i % n] = (0, 128, 128)
        else:
            np[n - 1 - (i % n)] = (0, 128, 128)
        np.write()
        time.sleep_ms(60)

def clear():
    for i in range(n):
        np[i] = (0, 0, 0)
    np.write()
    
def wheel(pos):
    if pos < 85:
        return(pos*3,255-pos*3,0)
    elif pos <170:
        pos-=85
        return(255-pos*3,0,pos*3)
    else:
        pos -=170
        return(0,pos*3,255-pos*3)

def loop():
    global position, brightness
    
    for i in range(np.n):
        hue=int(i*(255/np.n)+position)%256
        color=wheel(hue)
        color=tuple(int(val*brightness) for val in color)
        np[(i+position)%np.n]=color
    np.write()
    position=(position +1)% np.n
    #print(position)
    time.sleep_ms(200)
    
def antiloop():
    global position, brightness
    
    for i in range(np.n):
        hue=int(-i*(255/np.n)-position)%256
        color=wheel(hue)
        color=tuple(int(val*brightness) for val in color)
        np[(i-position)%np.n]=color
    np.write()
    position=(position +1)% np.n
    #print(position,"anti")
    time.sleep_ms(200)
        
    
while True:
    for i in range(np.n):
        loop()
    for i in range(np.n):
        antiloop()