Skip to content

Instantly share code, notes, and snippets.

@MichaelBell
Created October 2, 2023 14:14
Show Gist options
  • Select an option

  • Save MichaelBell/e7a496332ea87852b9e65b74d25a9f03 to your computer and use it in GitHub Desktop.

Select an option

Save MichaelBell/e7a496332ea87852b9e65b74d25a9f03 to your computer and use it in GitHub Desktop.
PicoVision Cellular Automata
import time
import math
import random
import machine
import gc
from picographics import PicoGraphics, PEN_RGB555, WIDESCREEN
machine.freq(125_000_000)
WIDTH = 360
HEIGHT = 240
display = PicoGraphics(PEN_RGB555, WIDTH, HEIGHT, frame_width=WIDTH, frame_height=HEIGHT * 2)
ON = display.create_pen(0, 0, 0)
OFF = display.create_pen(200, 200, 200)
state = bytearray(WIDTH)
next_state = bytearray(WIDTH)
# rule = (0, 1, 1, 1, 1, 0, 0, 0) # 30
state[WIDTH//2] = 1
rule = (0, 1, 0, 1, 1, 0, 1, 0) # 90
for i in range(WIDTH):
state[i] = random.randint(0,1)
rule = (0, 0, 0, 1, 1, 1, 0, 1) # 184
display.set_pen(OFF)
for _ in range(2):
display.set_scroll_group_for_lines(1, 0, HEIGHT*2)
display.clear()
display.update()
start_y = HEIGHT
backup_state = state[:]
while True:
for y in range(start_y, HEIGHT*2):
display.set_scroll_group_offset(1, 0, y // 2)
for i in range(1, WIDTH-1):
current = (state[i-1] << 2) | (state[i] << 1) | state[i+1]
next_state[i] = rule[current]
if next_state[i] == 1: display.set_pen(ON)
else: display.set_pen(OFF)
display.pixel(i, y)
next_state[0] = rule[state[WIDTH-1] << 2 | (state[0] << 1) | state[1]]
next_state[WIDTH-1] = rule[state[WIDTH-2] << 2 | (state[WIDTH-1] << 1) | state[0]]
print(y)
state, next_state = next_state, state
if y == HEIGHT-1:
backup_state = state[:]
state = backup_state
start_y = 0
display.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment