Skip to content

Commit 239e7e4

Browse files
authored
Merge pull request adafruit#1150 from jepler/RGB_Matrix_Slot_Machine
Add code for the guide
2 parents 7d71ee6 + 1d587e5 commit 239e7e4

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

RGB_Matrix_Slot_Machine/code.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import random
2+
import time
3+
4+
import adafruit_imageload.bmp
5+
import audioio
6+
import audiomp3
7+
import board
8+
import displayio
9+
import digitalio
10+
import framebufferio
11+
import rgbmatrix
12+
13+
displayio.release_displays()
14+
15+
matrix = rgbmatrix.RGBMatrix(
16+
width=64, height=32, bit_depth=4,
17+
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
18+
addr_pins=[board.A5, board.A4, board.A3, board.A2],
19+
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
20+
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
21+
22+
# Each wheel can be in one of three states:
23+
STOPPED, RUNNING, BRAKING = range(3)
24+
25+
# Return a duplicate of the input list in a random (shuffled) order.
26+
def shuffled(seq):
27+
return sorted(seq, key=lambda _: random.random())
28+
29+
# The Wheel class manages the state of one wheel. "pos" is a position in
30+
# floating point coordinates, with one 1 pixel being 1 position.
31+
# The wheel also has a velocity (in positions
32+
# per tick) and a state (one of the above constants)
33+
class Wheel(displayio.TileGrid):
34+
def __init__(self, bitmap, palette):
35+
# Portions of up to 3 tiles are visible.
36+
super().__init__(bitmap=bitmap, pixel_shader=palette,
37+
width=1, height=3, tile_width=20, tile_height=24)
38+
self.order = shuffled(range(20))
39+
self.state = STOPPED
40+
self.pos = 0
41+
self.vel = 0
42+
self.termvel = 2
43+
self.y = 0
44+
self.x = 0
45+
self.stop_time = time.monotonic_ns()
46+
self.step()
47+
48+
def step(self):
49+
# Update each wheel for one time step
50+
if self.state == RUNNING:
51+
# Slowly lose speed when running, but go at least terminal velocity
52+
self.vel = max(self.vel * .99, self.termvel)
53+
if time.monotonic_ns() > self.stop_time:
54+
self.state = BRAKING
55+
elif self.state == BRAKING:
56+
# More quickly lose speed when baking, down to speed 0.4
57+
self.vel = max(self.vel * .85, 0.4)
58+
59+
# Advance the wheel according to the velocity, and wrap it around
60+
# after 24*20 positions
61+
self.pos = (self.pos + self.vel) % (20*24)
62+
63+
# Compute the rounded Y coordinate
64+
yy = round(self.pos)
65+
# Compute the offset of the tile (tiles are 24 pixels tall)
66+
yyy = yy % 24
67+
# Find out which tile is the top tile
68+
off = yy // 24
69+
70+
# If we're braking and a tile is close to midscreen,
71+
# then stop and make sure that tile is exactly centered
72+
if self.state == BRAKING and self.vel <= 2 and yyy < 8:
73+
self.pos = off * 24
74+
self.vel = 0
75+
yyy = 0
76+
self.state = STOPPED
77+
78+
# Move the displayed tiles to the correct height and make sure the
79+
# correct tiles are displayed.
80+
self.y = yyy - 20
81+
for i in range(3):
82+
self[i] = self.order[(19 - i + off) % 20]
83+
84+
# Set the wheel running again, using a slight bit of randomness.
85+
# The 'i' value makes sure the first wheel brakes first, the second
86+
# brakes second, and the third brakes third.
87+
def kick(self, i):
88+
self.state = RUNNING
89+
self.vel = random.uniform(8, 10)
90+
self.termvel = random.uniform(1.8, 4.2)
91+
self.stop_time = time.monotonic_ns() + 3000000000 + i * 350000000
92+
93+
94+
# This bitmap contains the emoji we're going to use. It is assumed
95+
# to contain 20 icons, each 20x24 pixels. This fits nicely on the 64x32
96+
# RGB matrix display.
97+
the_bitmap, the_palette = adafruit_imageload.load(
98+
"/emoji.bmp",
99+
bitmap=displayio.Bitmap,
100+
palette=displayio.Palette)
101+
102+
# Our fruit machine has 3 wheels, let's create them with a correct horizontal
103+
# (x) offset and arbitrary vertical (y) offset.
104+
g = displayio.Group(max_size=3)
105+
wheels = []
106+
for idx in range(3):
107+
wheel = Wheel(the_bitmap, the_palette)
108+
wheel.x = idx * 22
109+
wheel.y = -20
110+
g.append(wheel)
111+
wheels.append(wheel)
112+
display.show(g)
113+
114+
# We want a digital input to trigger the fruit machine
115+
button = digitalio.DigitalInOut(board.A1)
116+
button.switch_to_input(pull=digitalio.Pull.UP)
117+
118+
# Enable the speaker
119+
enable = digitalio.DigitalInOut(board.D4)
120+
enable.switch_to_output(True)
121+
122+
mp3file = open("/triangles-loop.mp3", "rb")
123+
sample = audiomp3.MP3Decoder(mp3file)
124+
125+
# Play the sample (just loop it for now)
126+
speaker = audioio.AudioOut(board.A0)
127+
speaker.play(sample, loop=True)
128+
129+
# Here's the main loop
130+
while True:
131+
# Refresh the dislpay (doing this manually ensures the wheels move
132+
# together, not at different times)
133+
display.refresh(minimum_frames_per_second=0, target_frames_per_second=60)
134+
135+
all_stopped = all(si.state == STOPPED for si in wheels)
136+
if all_stopped:
137+
# Once everything comes to a stop, wait until the lever is pulled and
138+
# start everything over again. Maybe you want to check if the
139+
# combination is a "winner" and add a light show or something.
140+
141+
while button.value:
142+
pass
143+
for idx, si in enumerate(wheels):
144+
si.kick(idx)
145+
146+
# Otherwise, let the wheels keep spinning...
147+
for idx, si in enumerate(wheels):
148+
si.step()

0 commit comments

Comments
 (0)