-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwall.py
More file actions
49 lines (42 loc) · 1.44 KB
/
wall.py
File metadata and controls
49 lines (42 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from config import USE_SIMULATOR, GPIO_PIN
try:
import neopixel
import board
except ImportError:
neopixel = None
try:
from simulator.simulator_display import Simulator
except ImportError:
Simulator = None
class Wall:
def __init__(self, width, height):
self.width = width
self.height = height
self.num_pixels = width * height
if USE_SIMULATOR:
self.simulator = True
self.driver = Simulator(width, height)
else:
self.simulator = False
self.pixels = neopixel.NeoPixel(
board.D18, self.num_pixels, pixel_order=neopixel.GRBW
)
def show(self, frame):
if self.simulator:
self.driver.show(frame) # Simulator uses regular 'show'
else:
self._show(frame) # NeoPixel uses '_show'
def clear(self):
blank = [(0,0,0,0)] * self.width * self.height
self.show(blank)
def _show(self, frame): # NeoPixel-only
for y in range(self.height):
for x in range(self.width):
# Convert from row-major to serpentine
logical_idx = y * self.width + x
if y % 2 == 0:
physical_idx = logical_idx
else:
physical_idx = y * self.width + (self.width - 1 - x)
self.pixels[physical_idx] = frame[logical_idx]
self.pixels.show()