Skip to content

Commit d35648c

Browse files
authored
Merge branch 'master' into master
2 parents 992abf0 + bb232c7 commit d35648c

7 files changed

Lines changed: 289 additions & 2 deletions

File tree

CircuitPython_Slideshow/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Adafruit Learning System tutorial - Creating Slideshows in CircuitPython
2+
3+
Make a picture slideshow with an Adafruit CircuitPython board with attached display.
4+
5+
Follow the Adafruit learn guide here: https://learn.adafruit.com/creating-slideshows-in-circuitpython/
6+
7+
**code.py** is the code that will run the slideshow program when the board boots up.
8+
9+
Adafruit invests time and resources providing this open source code,
10+
please support Adafruit and open-source hardware by purchasing
11+
products from [Adafruit](https://www.adafruit.com)!
12+
13+
MIT license, guide written by Mike Barela, code by Limor Fried
14+
15+
All text above, and the splash screen below must be included in any redistribution
16+
17+
-----------------------
18+
If you are looking to make changes/additions, please use the GitHub Issues and Pull Request mechanisms.

CircuitPython_Slideshow/code.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# CircuitPython Slideshow - uses the adafruit_slideshow.mpy library
2+
import board
3+
from adafruit_slideshow import PlayBackOrder, SlideShow
4+
5+
# Create the slideshow object that plays through once alphabetically.
6+
slideshow = SlideShow(board.DISPLAY,
7+
folder="/images",
8+
loop=True,
9+
order=PlayBackOrder.ALPHABETICAL,
10+
dwell=60)
11+
12+
while slideshow.update():
13+
pass

Minesweep/SpriteSheet.bmp

2.16 KB
Binary file not shown.

Minesweep/code.py

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
"""
2+
PyPortal MineSweeper
3+
4+
Adafruit invests time and resources providing this open source code.
5+
Please support Adafruit and open source hardware by purchasing
6+
products from Adafruit!
7+
8+
Written by Dave Astels for Adafruit Industries
9+
Copyright (c) 2019 Adafruit Industries
10+
Licensed under the MIT license.
11+
12+
All text above must be included in any redistribution.
13+
"""
14+
15+
import time
16+
from random import seed, randint
17+
import board
18+
import digitalio
19+
import displayio
20+
import audioio
21+
try:
22+
from audioio import WaveFile
23+
except ImportError:
24+
from audiocore import WaveFile
25+
26+
import adafruit_imageload
27+
import adafruit_touchscreen
28+
29+
seed(int(time.monotonic()))
30+
31+
# Set up audio
32+
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
33+
speaker_enable.switch_to_output(False)
34+
if hasattr(board, 'AUDIO_OUT'):
35+
audio = audioio.AudioOut(board.AUDIO_OUT)
36+
elif hasattr(board, 'SPEAKER'):
37+
audio = audioio.AudioOut(board.SPEAKER)
38+
else:
39+
raise AttributeError('Board does not have a builtin speaker!')
40+
41+
42+
NUMBER_OF_BOMBS = 15
43+
44+
# Board pieces
45+
46+
OPEN0 = 0
47+
OPEN1 = 1
48+
OPEN2 = 2
49+
OPEN3 = 3
50+
OPEN4 = 4
51+
OPEN5 = 5
52+
OPEN6 = 6
53+
OPEN7 = 7
54+
OPEN8 = 8
55+
BLANK = 9
56+
BOMBDEATH = 10
57+
BOMBFLAGGED = 11
58+
BOMBMISFLAGGED = 12
59+
BOMBQUESTION = 13
60+
BOMB = 14
61+
62+
sprite_sheet, palette = adafruit_imageload.load("/SpriteSheet.bmp",
63+
bitmap=displayio.Bitmap,
64+
palette=displayio.Palette)
65+
66+
display = board.DISPLAY
67+
group = displayio.Group(scale=1, max_size=5)
68+
touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
69+
board.TOUCH_YD, board.TOUCH_YU,
70+
calibration=((9000, 59000),
71+
(8000, 57000)),
72+
size=(display.width, display.height))
73+
tilegrid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
74+
width=20, height=15,
75+
tile_height=16, tile_width=16,
76+
default_tile=BLANK)
77+
group.append(tilegrid)
78+
79+
80+
81+
display.show(group)
82+
83+
board_data = bytearray(b'\x00' * 300)
84+
85+
#pylint:disable=redefined-outer-name
86+
def get_data(x, y):
87+
return board_data[y * 20 + x]
88+
89+
def set_data(x, y, value):
90+
board_data[y * 20 + x] = value
91+
#pylint:disable=redefined-outer-name
92+
93+
def seed_bombs(how_many):
94+
for _ in range(how_many):
95+
while True:
96+
bomb_x = randint(0, 19)
97+
bomb_y = randint(0, 14)
98+
if get_data(bomb_x, bomb_y) == 0:
99+
set_data(bomb_x, bomb_y, 14)
100+
break
101+
102+
def compute_counts():
103+
"""For each bomb, increment the count in each non-bomb square around it"""
104+
for y in range(15):
105+
for x in range(20):
106+
if get_data(x, y) != 14:
107+
continue # keep looking for bombs
108+
for dx in (-1, 0, 1):
109+
if x + dx < 0 or x + dx >= 20:
110+
continue # off screen
111+
for dy in (-1, 0, 1):
112+
if y + dy < 0 or y + dy >= 15:
113+
continue # off screen
114+
count = get_data(x + dx, y + dy)
115+
if count == 14:
116+
continue # don't process bombs
117+
set_data(x + dx, y + dy, count + 1)
118+
119+
def reveal():
120+
for x in range(20):
121+
for y in range(15):
122+
if tilegrid[x, y] == BOMBFLAGGED and get_data(x, y) != BOMB:
123+
tilegrid[x, y] = BOMBMISFLAGGED
124+
else:
125+
tilegrid[x, y] = get_data(x, y)
126+
127+
#pylint:disable=too-many-nested-blocks
128+
def expand_uncovered(start_x, start_y):
129+
number_uncovered = 1
130+
stack = [(start_x, start_y)]
131+
while len(stack) > 0:
132+
x, y = stack.pop()
133+
if tilegrid[x, y] == BLANK:
134+
under_the_tile = get_data(x, y)
135+
if under_the_tile <= OPEN8:
136+
tilegrid[x, y] = under_the_tile
137+
number_uncovered += 1
138+
if under_the_tile == OPEN0:
139+
for dx in (-1, 0, 1):
140+
if x + dx < 0 or x + dx >= 20:
141+
continue # off screen
142+
for dy in (-1, 0, 1):
143+
if y + dy < 0 or y + dy >= 15:
144+
continue # off screen
145+
if dx == 0 and dy == 0:
146+
continue # don't process where the bomb
147+
stack.append((x + dx, y + dy))
148+
return number_uncovered
149+
#pylint:enable=too-many-nested-blocks
150+
151+
def check_for_win():
152+
"""Check for a complete, winning game. That's one with all squares uncovered
153+
and all bombs correctly flagged, with no non-bomb squares flaged.
154+
"""
155+
# first make sure everything has been explored and decided
156+
for x in range(20):
157+
for y in range(15):
158+
if tilegrid[x, y] == BLANK or tilegrid[x, y] == BOMBQUESTION:
159+
return None #still ignored or question squares
160+
# then check for mistagged bombs
161+
for x in range(20):
162+
for y in range(15):
163+
if tilegrid[x, y] == BOMBFLAGGED and get_data(x, y) != BOMB:
164+
return False #misflagged bombs, not done
165+
return True #nothing unexplored, and no misflagged bombs
166+
167+
#pylint:disable=too-many-branches
168+
# This could be broken apart but I think it's more understandable
169+
# with it all in one place
170+
def play_a_game():
171+
number_uncovered = 0
172+
touch_x = -1
173+
touch_y = -1
174+
touch_time = 0
175+
wait_for_release = False
176+
while True:
177+
now = time.monotonic()
178+
if now >= touch_time:
179+
touch_time = now + 0.2
180+
# process touch
181+
touch_at = touchscreen.touch_point
182+
if touch_at is None:
183+
wait_for_release = False
184+
else:
185+
if wait_for_release:
186+
continue
187+
wait_for_release = True
188+
touch_x = max(min([touch_at[0] // 16, 19]), 0)
189+
touch_y = max(min([touch_at[1] // 16, 14]), 0)
190+
if tilegrid[touch_x, touch_y] == BLANK:
191+
tilegrid[touch_x, touch_y] = BOMBQUESTION
192+
elif tilegrid[touch_x, touch_y] == BOMBQUESTION:
193+
tilegrid[touch_x, touch_y] = BOMBFLAGGED
194+
elif tilegrid[touch_x, touch_y] == BOMBFLAGGED:
195+
under_the_tile = get_data(touch_x, touch_y)
196+
if under_the_tile == 14:
197+
set_data(touch_x, touch_y, BOMBDEATH) #reveal a red bomb
198+
tilegrid[touch_x, touch_y] = BOMBDEATH
199+
return False #lost
200+
elif under_the_tile > OPEN0 and under_the_tile <= OPEN8:
201+
tilegrid[touch_x, touch_y] = under_the_tile
202+
elif under_the_tile == OPEN0:
203+
tilegrid[touch_x, touch_y] = BLANK
204+
number_uncovered += expand_uncovered(touch_x, touch_y)
205+
else: #something bad happened
206+
raise ValueError('Unexpected value on board')
207+
status = check_for_win()
208+
if status is None:
209+
continue
210+
return status
211+
#pylint:enable=too-many-branches
212+
213+
def reset_board():
214+
for x in range(20):
215+
for y in range(15):
216+
tilegrid[x, y] = BLANK
217+
set_data(x, y, 0)
218+
seed_bombs(NUMBER_OF_BOMBS)
219+
compute_counts()
220+
221+
def play_sound(file_name):
222+
board.DISPLAY.wait_for_frame()
223+
wavfile = open(file_name, "rb")
224+
wavedata = WaveFile(wavfile)
225+
speaker_enable.value = True
226+
audio.play(wavedata)
227+
return wavfile
228+
229+
def wait_for_sound_and_cleanup(wavfile):
230+
while audio.playing:
231+
pass
232+
wavfile.close()
233+
speaker_enable.value = False
234+
235+
def win():
236+
print('You won')
237+
wait_for_sound_and_cleanup(play_sound('win.wav'))
238+
239+
def lose():
240+
print('You lost')
241+
wavfile = play_sound('lose.wav')
242+
for _ in range(10):
243+
tilegrid.x = randint(-2, 2)
244+
tilegrid.y = randint(-2, 2)
245+
display.refresh_soon()
246+
display.wait_for_frame()
247+
tilegrid.x = 0
248+
tilegrid.y = 0
249+
wait_for_sound_and_cleanup(wavfile)
250+
251+
while True:
252+
reset_board()
253+
if play_a_game():
254+
win()
255+
else:
256+
reveal()
257+
lose()
258+
time.sleep(5.0)

Minesweep/lose.wav

156 KB
Binary file not shown.

Minesweep/win.wav

99.6 KB
Binary file not shown.

Twitter_API/code.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def get_bearer_token():
6262
json_path=(0, 'text'),
6363
status_neopixel=board.NEOPIXEL,
6464
default_bg=cwd + '/twitter_background.bmp',
65-
# external_spi=spi,
66-
# esp=esp,
6765
text_font=cwd+'/fonts/Helvetica-Bold-16.bdf',
6866
text_position=(20, 60),
6967
text_color=0xFFFFFF,

0 commit comments

Comments
 (0)