代码: 全选
from machine import Pin
import neopixel
import random
from time import sleep_ms
def rand(n):
return random.getrandbits(16)%n
class neo16x16_img:
def __init__(self, pin):
self.np = neopixel.NeoPixel(pin, 256)
def clear(self):
self.np.fill((0,0,0))
self.np.write()
def set(self, n, color):
self.np[n] = color
self.np.write()
def show(self,dat,pos=0,update=1):
for x in range(16):
for y in range(8):
if ((x+pos)*8)>=len(dat):
self.np[x*16+y*2]=(0,0,0)
self.np[x*16+y*2+1]=(0,0,0)
else:
t=dat[(x+pos)*8+y]
r=t%16
g=(t>>4)%16
b=(t>>8)%16
if pos%2:
self.np[x*16+y*2]=(r,g,b)
else:
self.np[x*16+15-y*2]=(r,g,b)
r=(t>>12)%16
g=(t>>16)%16
b=(t>>20)%16
if pos%2:
self.np[x*16+y*2+1]=(r,g,b)
else:
self.np[x*16+14-y*2]=(r,g,b)
if update:
self.np.write()
np = neo16x16_img(Pin(2))
npd=[
0x000FFF, 0x080000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x080000, 0x080000, 0x000080, 0xFFF000,
0x000FFF, 0x080000, 0x080080, 0x080080,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x080000,
0x080080, 0x080080, 0x069080, 0xFFF069,
0x000FFF, 0x080000, 0x080080, 0x080080,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x080000, 0x080000, 0x000080, 0xFFF000,
0x000FFF, 0x080000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0xFFF000,
0x000FFF, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0xFFF000,
0x000FFF, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0xFFF000,
0x000FFF, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0xFFF000,
0x000FFF, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0xFFF000,
]
# 0: y, 1: x, 2:bright
snow = []
np.show(npd)
while True:
if len(snow) > 0:
np.show(npd, update=0)
for i in range(len(snow)):
snow[i][0]+=1
snow[i][1]+=1-rand(3)
snow[i][1]=max(0, min(snow[i][1], 15))
y=snow[i][0]
x=snow[i][1]
br=snow[i][2]
if x%2:
np.np[x*16+15-y]=[br,br,br]
else:
np.np[x*16+y]=[br,br,br]
if snow[0][0]>14:
t=snow.pop(0)
np.np.write()
if rand(3) == 0:
snow.append([0, rand(16), rand(15)+1])
sleep_ms(50+rand(100))