Created
November 27, 2024 21:57
-
-
Save scruss/f7fa55188614087721debe900bb26e0e to your computer and use it in GitHub Desktop.
write a threatening pattern to a pbm
This file contains 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
#!/usr/bin/env micropython | |
# write a threatening pattern to a pbm | |
# scruss, 2024-11 | |
# MicroPython has framebuf, but no math.isqrt() | |
# Python3 has no framebuf, but has math.isqrt() | |
import framebuf | |
from math import sqrt | |
WIDTH = 1280 # original target was a 128 x 64 OLED | |
HEIGHT = 640 | |
buffer = bytearray(round(WIDTH * HEIGHT / 8)) | |
fb = framebuf.FrameBuffer(buffer, WIDTH, HEIGHT, framebuf.MONO_HLSB) | |
def hlsb2pbm(b, w, h): | |
# write out a MONO_HLSB framebuffer's bytearray, b, of size w x h | |
# to a NetPBM RAW PBM file | |
import time | |
ds = "T".join( | |
( | |
"%4d%02d%02d" % (time.localtime()[0:3]), | |
"%02d%02d%02d" % (time.localtime()[3:6]), | |
) | |
) | |
f = open("fb-" + ds + ".pbm", "wb") | |
f.write(bytes("P4\n", "UTF-8")) | |
f.write(bytes(str(w) + " " + str(h) + "\n", "UTF-8")) | |
f.write(b) | |
f.close() | |
# Clear the oled display in case it has junk on it. | |
fb.fill(0) | |
for y in range(HEIGHT): | |
for x in range(WIDTH): | |
p = int(sqrt(x**2 / 160 + y**2 / 160)) % 2 | |
q = ( | |
int(sqrt((WIDTH - x) ** 3 / 160 + (HEIGHT - y) ** 2 / 160)) | |
% 2 | |
) | |
fb.pixel(x, y, p ^ q) | |
# write to file | |
hlsb2pbm(buffer, WIDTH, HEIGHT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment