Created
September 1, 2020 01:59
-
-
Save ewalk153/912c323e33f43e12e54886efd3e40bbb to your computer and use it in GitHub Desktop.
Mandelbrot set, on Micropython Makerfab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this will be the mandelbrot set, written in python | |
| import sys | |
| import machine | |
| import st7789py as st7789 | |
| import random | |
| from machine import Pin | |
| def scale_x(px): | |
| #scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1)) | |
| return -2.5 + px * 3.5 / 239 | |
| def scale_y(py): | |
| #scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1)) | |
| return -1 + py * 2 / 239 | |
| def mandelbrot_pixel(px, py): | |
| x0 = scale_x(px) | |
| y0 = scale_y(py) | |
| max_iteration = 20 | |
| x2 = 0 | |
| y2 = 0 | |
| w = 0 | |
| iteration = 0 | |
| while x2 + y2 <= 4 and iteration < max_iteration: | |
| x = x2 - y2 + x0 | |
| y = w - x2 - y2 + y0 | |
| x2 = x * x | |
| y2 = y * y | |
| w = (x + y) * (x + y) | |
| iteration += 1 | |
| return iteration | |
| class Render: | |
| """Abstracts rendering out from mandelbrot logic""" | |
| def __init__(self, display, buffered=True): | |
| self.buffered = buffered | |
| self.display = display | |
| if buffered: | |
| self.pallete = build_byte_pallete() | |
| else: | |
| self.pallete = build_colors_pallete() | |
| self.buffer = bytearray() | |
| def plot(self, i, j, iter): | |
| if not self.buffered: | |
| self.display.pixel(i, j, self.pallete[iter]) | |
| return | |
| self.buffer.append(self.pallete[iter*2]) | |
| self.buffer.append(self.pallete[iter*2+1]) | |
| if j % 10 == 9 and i == 239: | |
| self.display.blit_buffer(self.buffer, 0, j-9, 240, 10) | |
| self.buffer = bytearray() | |
| def build_colors_pallete(): | |
| colors = bytearray() | |
| for i in range(21): | |
| colors.append(st7789.color565( | |
| random.getrandbits(8), | |
| random.getrandbits(8), | |
| random.getrandbits(8))) | |
| return colors | |
| def build_byte_pallete(): | |
| colors = bytearray() | |
| for i in range(21): | |
| colors.append(random.getrandbits(8)) | |
| colors.append(random.getrandbits(8)) | |
| return colors | |
| def mandelbrot(render): | |
| for j in range(240): | |
| for i in range(240): | |
| iter = mandelbrot_pixel(i, j) | |
| render.plot(i, j, iter) | |
| bl = machine.Pin(5, machine.Pin.OUT) | |
| bl.value(1) | |
| spi = machine.SPI(1, baudrate=80000000, polarity=1, phase=0, sck=machine.Pin(14), mosi=machine.Pin(13), miso=machine.Pin(12))#HSPI | |
| display = st7789.ST7789( | |
| spi, 240, 240, | |
| reset=machine.Pin(21, machine.Pin.OUT), | |
| dc=machine.Pin(22, machine.Pin.OUT), | |
| cs=machine.Pin(15, machine.Pin.OUT), | |
| backlight=machine.Pin(5, machine.Pin.OUT), | |
| ) | |
| display.fill(st7789.BLACK) | |
| render = Render(display, buffered=True) | |
| mandelbrot(render) | |
| print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment