2
2
# as used in https://learn.adafruit.com/animated-led-sand
3
3
# Explainatory comments are used verbatim from that code.
4
4
5
- import board
6
- import busio
7
5
import math
8
6
import random
9
7
8
+ import board
9
+ import busio
10
+
10
11
import adafruit_lsm303
11
12
import adafruit_dotstar
12
13
18
19
GRAIN_COLOR = (64 , 64 , 64 )
19
20
MAX_X = WIDTH * 256 - 1
20
21
MAX_Y = HEIGHT * 256 - 1
21
-
22
+
22
23
class Grain :
23
24
def __init__ (self ):
24
- x = 0
25
+ x = 0
25
26
y = 0
26
27
vx = 0
27
28
vy = 0
@@ -42,9 +43,9 @@ def __init__(self):
42
43
43
44
def index_of_xy (x , y ):
44
45
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 ):
48
49
if x == grains [j ].x or y == grains [j ].y :
49
50
return True
50
51
return False
@@ -58,13 +59,13 @@ def already_present(i, x, y):
58
59
occupied_bits [index_of_xy (g .x , g .y )] = True
59
60
g .vx = 0
60
61
g .vy = 0
61
-
62
+
62
63
while True :
63
64
# Display frame rendered on prior pass. It's done immediately after the
64
65
# FPS sync (rather than after rendering) for consistent animation timing.
65
66
66
67
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 )
68
69
wing .show ()
69
70
70
71
# Read accelerometer...
@@ -125,7 +126,8 @@ def already_present(i, x, y):
125
126
126
127
oldidx = index_of_xy (g .x , g .y ) # prior pixel
127
128
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...
129
131
delta = abs (newidx - oldidx ) # What direction when blocked?
130
132
if delta == 1 : # 1 pixel left or right
131
133
newx = g .x # cancel x motion
0 commit comments