from machine import Pin, SoftI2C
import ssd1306
from time import sleep
import random
i2c = SoftI2C(sda=Pin(21), scl=Pin(22))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
tree_points = []
center_x = 64
top_y = 5
for i in range(18):
width = i * 2 + 1
y = top_y + i * 2
for x in range(width):
tree_points.append((center_x - width + x * 2, y))
trunk = [(center_x - 2, top_y + 40),
(center_x, top_y + 40),
(center_x + 2, top_y + 40),
(center_x - 2, top_y + 42),
(center_x, top_y + 42),
(center_x + 2, top_y + 42)]
def draw_tree(blink=True):
for x, y in tree_points:
if not blink or random.getrandbits(1):
display.pixel(x, y, 1)
def draw_trunk(pulse):
for x, y in trunk:
display.pixel(x, y + pulse, 1)
def sparkles(count=10):
for _ in range(count):
x = random.randint(40, 88)
y = random.randint(5, 45)
display.pixel(x, y, 1)
display.poweron()
pulse = 0
direction = 1
while True:
display.fill(0)
draw_tree(blink=True)
draw_trunk(pulse)
sparkles(12)
display.show()
pulse += direction
if pulse > 1 or pulse < 0:
direction *= -1
sleep(0.15)