import machine
import sdcard
import uos
import array, time
from machine import Pin
import rp2
from ws2812b import WS2812B

NUM_LEDS = 256

ws = WS2812B(256, 28, 0.1)

# Assign chip select (CS) pin (and start it high)
cs = machine.Pin(13, machine.Pin.OUT)

# Intialize SPI peripheral (start with 1 MHz)
spi = machine.SPI(1,
                  baudrate=1000000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(12))

# Initialize SD card
sd = sdcard.SDCard(spi, cs)

# Mount filesystem
vfs = uos.VfsFat(sd)
uos.mount(vfs, "/sd")
print(uos.listdir("/sd"))


with open("/sd/art.grb", 'rb') as f:
    while True:
        frame = f.read(256*3)
        if not frame:
            print("End of frames")
            break
        
        for i in range(256):
            offset = i * 3
            green, red, blue = frame[offset:offset + 3]
            print(red, green, blue)
            ws.pixels_set(i, (red, green, blue))
        ws.pixels_show()