# uPy - RPi Pico testing Fram MB85RS64 through SPI and FS
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-01-28  initial release

from	machine		import	Pin, SPI
from	mve_fram	import	MB85RS64	as	Fram
from	ve_fram_fs	import	FramFs

# ========== Hardware definitions (Esp32s2) =============

# For Esp32 rw2050
portC   = ( 2,  1,  3,  5,  7,  9, 11, 12)				# pins port C (RW2050 servo's)
portD   = (14, 13)

pin_led         = 15				# on-board Esp32s2_mini
pin_spi_mosi	= portD[1]
pin_spi_miso	= portC[7]
pin_spi_sck		= portD[0]
pin_spi_cs		= portC[6]
spi_mod			= 1

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

led		= Pin(pin_led, Pin.OUT, value=1)
spi	    = SPI(1, 30_000_000, sck=pin_spi_sck, mosi=pin_spi_mosi,
              miso=pin_spi_miso)
spi_cs  = Pin(pin_spi_cs, Pin.OUT, value=1)

# ======================= main =========================
    
def main():
    fram = Fram(spi=spi, cs=spi_cs)
    fs   = FramFs(fram)
    fs.erase()
    fs.write('A1', 'hello')
    fs.write('A2', '12345')
    fs.write('A3', 'Hello world')
    fs.write('A4', '0123456789')
    print(fs.read('A1'))
    print(fs.read('A2'))
    print(fs.read('A3'))
    print(fs.read('A4'))
    fs.dump(0x0000, 0x0040);	print()
    fs.dump(0x0400, 0x0440);	print()
    fs.printLs();    			print()
    print(fs.ls())
    fs.delete('A2')
    print(fs.ls())
    fs.defragment()
    print(fs.ls())
    fs.dump(0x0000, 0x0040);	print()
    fs.dump(0x0400, 0x0440);	print()
    fs.printLs();				print()
    print(fs.ls());				print()
    
    print(fs.read('A1'))
    print(fs.read('A3'))
    print(fs.read('A4'))

    file1 = 'line1\nline2\nline3'
    fs.write('file1.txt', file1)
    print(fs.read('file1.txt'))

    file2 = '''
line 10
line 20
line 33'''
    fs.write('file2.txt', file2)
    print(fs.read('file2.txt'))

    d = dict(key1=123, key2='hello', key3=3.14, key4=[1,2,3,4,5])
    fs.write('A5', d)
    print(fs.read('A5'))


main()

# ======================== End ==========================
