Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple version of mandelbrot rendering on ODROID GO
"""
Render mandelbrot on Odroid Go
It's for loboris MicroPython port!
Based on code from https://github.com/pypyjs/pypyjs-examples/
"""
import sys
import time
import display
import machine
import micropython
from machine import SPI
from micropython import const
micropython.opt_level(99)
TFT_MISO_PIN = const(19)
TFT_MOSI_PIN = const(23)
TFT_SCLK_PIN = const(18)
TFT_CS_PIN = const(5)
TFT_DC_PIN = const(21)
TFT_WIDTH = const(320)
TFT_HEIGHT = const(240)
TFT_SPEED = const(40000000)
def mandelbrot(tft, width, height, left, right, top, bottom, iterations):
for y in range(height):
for x in range(width):
z = complex(0, 0)
c = complex(left + x * (right - left) / width, top + y * (bottom - top) / height)
norm = abs(z) ** 2
for count in range(iterations):
if norm <= 4:
z = z * z + c
norm = abs(z * z)
else:
break
if count <= 4:
color = tft.DARKGREY
elif count <= 8:
color = tft.GREEN
elif count <= 10:
color = tft.BLUE
elif count <= 12:
color = tft.RED
elif count <= 15:
color = tft.YELLOW
else:
color = tft.BLACK
tft.pixel(x, y, color)
def render():
# https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/display
tft = display.TFT()
try:
tft.init(
type=tft.ILI9341,
width=TFT_WIDTH,
height=TFT_HEIGHT,
speed=TFT_SPEED,
miso=TFT_MISO_PIN,
mosi=TFT_MOSI_PIN,
clk=TFT_SCLK_PIN,
cs=TFT_CS_PIN,
spihost=SPI.VSPI,
dc=TFT_DC_PIN,
bgr=True,
rot=tft.LANDSCAPE_FLIP,
)
except OSError as err:
# e.g.: Error initializing display
sys.print_exception(err)
for i in range(3, 0, -1):
print("Hard reset in %i Sek!" % i)
time.sleep(1)
machine.reset()
tft.clear(tft.BLACK)
# https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/machine#machinewdtenable
machine.WDT(False)
mandelbrot(
tft,
width=TFT_WIDTH,
height=TFT_HEIGHT,
left=const(-2),
right=0.5,
top=const(1.25),
bottom=const(-1.25),
iterations=const(40),
)
tft.deinit()
try:
start_time = time.time()
render()
duration = time.time() - start_time
print("rendered in %.1f sec." % duration)
finally:
# FIXME:
# import gc
# gc.collect()
print("---END---")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.