# esp32 - controlling 8x8 leds through MAX7219 SPI driver FKS0001
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-07-09  initial release

from	machine	import	Pin, SPI
import	time

# ======================== Hardware setup ======================

pin_spi_cs	 = 2
pin_spi_clk  = 0
pin_spi_mosi = 15
pin_spi_miso =  4

leds_cs  = Pin(pin_spi_cs, Pin.OUT, value=1)
leds_spi = SPI(1, 1_000_000, polarity=0, phase=0, sck=Pin(pin_spi_clk),
               mosi=Pin(pin_spi_mosi), miso=Pin(pin_spi_miso))

# ============================ test ============================

def test():
    leds_cs(0); leds_spi.write(b'\x0C\x01'); leds_cs(1)			# mode = normal
    leds_cs(0); leds_spi.write(b'\x0A\x03'); leds_cs(1)			# intensity 3/15
    leds_cs(0); leds_spi.write(b'\x0B\xFF'); leds_cs(1)			# scan limit
    leds_cs(0); leds_spi.write(b'\x01\x01'); leds_cs(1)			# bottom right led
    leds_cs(0); leds_spi.write(b'\x02\x03'); leds_cs(1)
    leds_cs(0); leds_spi.write(b'\x03\x07'); leds_cs(1)
    leds_cs(0); leds_spi.write(b'\x04\x0F'); leds_cs(1)
    leds_cs(0); leds_spi.write(b'\x05\x1F'); leds_cs(1)
    leds_cs(0); leds_spi.write(b'\x06\x3F'); leds_cs(1)
    leds_cs(0); leds_spi.write(b'\x07\x7F'); leds_cs(1)
    leds_cs(0); leds_spi.write(b'\x08\xFF'); leds_cs(1)
    time.sleep(4)
    leds_cs(0); leds_spi.write(b'\x0C\x00'); leds_cs(1)			# mode = off
    

test()
    