Graeme Winter

Mandelbrot set / MicroSD

For my own notes really - now moved Grand Central onto CircuitPython as I thought that may give me built-in i2s support (spoiler, it does for output but I wanted input). While there though, port back Mandelbrot set calculation in assembly from MicroPython loadable module, and write to MicroSD card as an exercise (rather than writing to a UART) which was already working in MicroPython.

Code simple - standard boiler plate to mount / open MicroSD card, 1280 x write, close:

import time
import mandelbrot
from uctypes import addressof

import digitalio
import board
import busio
import sdcardio
import storage
import os


def main():
    spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
    cs = board.SD_CS

    sdcard = sdcardio.SDCard(spi, cs)
    vfs = storage.VfsFat(sdcard)
    storage.mount(vfs, "/sd")

    led = digitalio.DigitalInOut(board.LED)
    led.direction = digitalio.Direction.OUTPUT

    buffer = bytearray(1280 * 4)
    address = addressof(buffer)

    fout = open("/sd/mandelbrot.dat", "wb")

    t0 = time.time()
    for i in range(1280):
        Ci = (-5 << 22) + 0x4000 + i * 0x8000
        mandelbrot.mandelbrot(address, Ci)
        fout.write(buffer)
        led.value = not led.value
    t1 = time.time()

    print("Mandelbrot set calculation: %ds" % (t1 - t0))

    fout.close()
    storage.umount("/sd")


main()

Achieves the same objective as the write-over-USB-UART version in a very similar amount of time.