# Esp32 - display moving text on 8x8 led panel by Max7219
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-07-18  initial release

from	machine	import	Pin, SPI
import	time

# ============================= Leds64 =========================

# Esp32 driver for Max7218 8x8 Led  panel
class Leds64:
    
    def __init__(self, pin_cs=2, pin_sck=0, pin_mosi=15, pin_miso=4):
        self.cs   = Pin(pin_cs, Pin.OUT, value=1)
        self.spi  = SPI(1, 1_000_000, polarity=0, phase=0, sck=Pin(pin_sck),
                        mosi=Pin(pin_mosi), miso=Pin(pin_miso))
        
        
    def on(self, intensity=3):
        self.cs(0); self.spi.write(b'\x0C\x01');              self.cs(1)			# mode = normal
        self.cs(0); self.spi.write(bytes([0x0A, intensity])); self.cs(1)			# intensity 3/15
        self.cs(0); self.spi.write(b'\x0B\xFF');              self.cs(1)			# scan limit
        for idx in range(1, 9):
            self.cs(0); self.spi.write(bytes([idx, 0])); self.cs(1)					# reset 8x8 ram
        

    def off(self):
        self.cs(0); self.spi.write(b'\x0C\x00'); self.cs(1)							# mode = off


    def write(self, data=b'\x01\x80\x80\x80\x80\x80\x80\x02'):
        for idx in range(8):
            word = bytes([idx+1, data[idx]])
            self.cs(0); self.spi.write(word); self.cs(1)			# mode = off

# ======================== rotated character table =============================
    
# rotated character table obtained with copy / paste
ctbl = dict(
    A = b'\x00\x1e\xf0\x90\x90\xfe\x1e\x00',
    B = b'\x00\x1e\xf2\x92\x9e\xfe\x82\x00',
    C = b'\x00\xc6\x82\x82\x82\xfe\x1e\x00',
    D = b'\x00\xfe\x82\x82\x9e\xfe\x82\x00',
    E = b'\x00\x82\x82\x92\x92\xfe\x1e\x00',
    F = b'\x00\x80\x80\x90\x90\xfe\x1e\x00',
    G = b'\x00\xde\x92\x92\x82\xfe\x1e\x00',
    H = b'\x00\x1e\xf0\x10\x10\xfe\x1e\x00',
    I = b'\x00\x82\x82\xfe\x9e\x82\x00\x00',
    J = b'\x00\x00\x9e\xfe\x82\x02\x06\x00',
    K = b'\x00\x02\xc2\x2c\x10\xfe\x1e\x00',
    L = b'\x00\x06\x02\x02\x02\xfe\x1e\x00',
    M = b'\x00\xfe\xc0\x60\xc0\xfe\x1e\x00',
    N = b'\x00\xfe\x04\x78\x80\xfe\x1e\x00',
    O = b'\x00\xfe\x82\x82\x82\xfe\x1e\x00',
    P = b'\x00\xf0\x90\x90\x90\xfe\x1e\x00',
    Q = b'\x00\xfa\x84\x8a\x82\xfe\x1e\x00',
    R = b'\x00\xf2\x96\x98\x90\xfe\x1e\x00',
    S = b'\x00\x00\xde\x9e\x92\xf2\x06\x00',
    T = b'\x00\x80\x80\xfe\x9e\x80\x00\x00',
    U = b'\x00\xfe\x02\x02\x02\xfe\x1e\x00',
    V = b'\x00\xe0\x10\x08\x04\xfe\x1e\x00',
    W = b'\x00\xfe\x06\x1c\x06\xfe\x1e\x00',
    X = b'\x00\x82\x5e\x30\x30\x5e\x8e\x00',
    Y = b'\x00\x80\xf8\x1c\x26\xc2\x80\x00',
    Z = b'\x00\xc2\xe2\xb2\x9a\x8e\x86\x00')

# ==================== moving  text ============================

# show txt on 8x8 led panel as moving text
def movingTextR(txt='Hello World'):
    leds64 = Leds64()
    leds64.on()
    res = bytearray(0)
    for ch in txt.upper():
        if ch in ctbl:
            res += ctbl[ch]
        else:
            res += bytearray(8)
    while True:
        for i in range(len(res) - 8):
            leds64.write(res[i:i+8])
            time.sleep_ms(250)
              
# ========================= test ================================

def test1():
    movingTextR()
    

def test2():
    movingTextR(' ABC ')


test2()

# =========================== end ===============================

    
