22# as used in https://learn.adafruit.com/animated-led-sand
33# Explainatory comments are used verbatim from that code.
44
5- import board
6- import busio
75import math
86import random
97
8+ import board
9+ import busio
10+
1011import adafruit_lsm303
1112import adafruit_dotstar
1213
1819GRAIN_COLOR = (64 , 64 , 64 )
1920MAX_X = WIDTH * 256 - 1
2021MAX_Y = HEIGHT * 256 - 1
21-
22+
2223class Grain :
2324 def __init__ (self ):
24- x = 0
25+ x = 0
2526 y = 0
2627 vx = 0
2728 vy = 0
@@ -42,9 +43,9 @@ def __init__(self):
4243
4344def index_of_xy (x , y ):
4445 return (y >> 8 ) * WIDTH + (x >> 8 )
45-
46- def already_present (i , x , y ):
47- for j in range (i ):
46+
47+ def already_present (limit , x , y ):
48+ for j in range (limit ):
4849 if x == grains [j ].x or y == grains [j ].y :
4950 return True
5051 return False
@@ -58,13 +59,13 @@ def already_present(i, x, y):
5859 occupied_bits [index_of_xy (g .x , g .y )] = True
5960 g .vx = 0
6061 g .vy = 0
61-
62+
6263while True :
6364 # Display frame rendered on prior pass. It's done immediately after the
6465 # FPS sync (rather than after rendering) for consistent animation timing.
6566
6667 for i in range (NUMBER_PIXELS ):
67- wing [i ] = GRAIN_COLOR if occupied_bits [i ] else (0 ,0 , 0 )
68+ wing [i ] = GRAIN_COLOR if occupied_bits [i ] else (0 , 0 , 0 )
6869 wing .show ()
6970
7071 # Read accelerometer...
@@ -125,7 +126,8 @@ def already_present(i, x, y):
125126
126127 oldidx = index_of_xy (g .x , g .y ) # prior pixel
127128 newidx = index_of_xy (newx , newy ) # new pixel
128- if oldidx != newidx and occupied_bits [newidx ]: # If grain is moving to a new pixel... but if that pixel is already occupied...
129+ if oldidx != newidx and occupied_bits [newidx ]: # If grain is moving to a new pixel...
130+ # but if that pixel is already occupied...
129131 delta = abs (newidx - oldidx ) # What direction when blocked?
130132 if delta == 1 : # 1 pixel left or right
131133 newx = g .x # cancel x motion
0 commit comments