Skip to content

Commit d110742

Browse files
authored
Merge pull request #2970 from FoamyGuy/metro2350
CircuitPythonMetro2350 Examples
2 parents 18a037d + 3e33418 commit d110742

File tree

11 files changed

+200
-0
lines changed

11 files changed

+200
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S Tone playback example.
5+
Plays a tone for one second on, one
6+
second off, in a loop.
7+
8+
"""
9+
import time
10+
import array
11+
import math
12+
import audiocore
13+
import board
14+
import audiobusio
15+
16+
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
17+
18+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
19+
frequency = 440 # Set this to the Hz of the tone you want to generate.
20+
length = 8000 // frequency
21+
sine_wave = array.array("h", [0] * length)
22+
for i in range(length):
23+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
24+
sine_wave_sample = audiocore.RawSample(sine_wave)
25+
26+
while True:
27+
audio.play(sine_wave_sample, loop=True)
28+
time.sleep(1)
29+
audio.stop()
30+
time.sleep(1)
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S WAV file playback.
5+
Plays a WAV file once.
6+
7+
"""
8+
import audiocore
9+
import board
10+
import audiobusio
11+
12+
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
13+
14+
with open("StreetChicken.wav", "rb") as wave_file:
15+
wav = audiocore.WaveFile(wave_file)
16+
17+
print("Playing wav file!")
18+
audio.play(wav)
19+
while audio.playing:
20+
pass
21+
22+
print("Done!")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
import gc
4+
5+
print(gc.mem_free())
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython multiple MP3 playback example.
5+
Plays two MP3 files consecutively, once time each.
6+
"""
7+
8+
import board
9+
import audiomp3
10+
import audiopwmio
11+
12+
audio = audiopwmio.PWMAudioOut(board.A0)
13+
14+
mp3files = ["slow.mp3", "happy.mp3"]
15+
16+
with open(mp3files[0], "rb") as mp3:
17+
decoder = audiomp3.MP3Decoder(mp3)
18+
19+
for filename in mp3files:
20+
with open(filename, "rb") as decoder.file:
21+
audio.play(decoder)
22+
print("Playing:", filename)
23+
while audio.playing:
24+
pass
25+
26+
print("Done playing!")
Binary file not shown.
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython single MP3 playback example.
5+
Plays a single MP3 once.
6+
"""
7+
import board
8+
import audiomp3
9+
import audiopwmio
10+
11+
audio = audiopwmio.PWMAudioOut(board.A0)
12+
13+
with open("slow.mp3", "rb") as mp3_file:
14+
decoder = audiomp3.MP3Decoder(mp3_file)
15+
16+
audio.play(decoder)
17+
while audio.playing:
18+
pass
19+
20+
print("Done playing!")
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
CircuitPython Essentials SD Card Read Demo
6+
7+
"""
8+
9+
import os
10+
import digitalio
11+
import busio
12+
import board
13+
import storage
14+
import adafruit_sdcard
15+
16+
# The SD_CS pin is the chip select line.
17+
SD_CS = board.SD_CS
18+
19+
# Connect to the card and mount the filesystem.
20+
cs = digitalio.DigitalInOut(SD_CS)
21+
sdcard = adafruit_sdcard.SDCard(busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs)
22+
vfs = storage.VfsFat(sdcard)
23+
storage.mount(vfs, "/sd")
24+
25+
# Use the filesystem as normal! Our files are under /sd
26+
27+
# This helper function will print the contents of the SD
28+
def print_directory(path, tabs=0):
29+
for file in os.listdir(path):
30+
stats = os.stat(path + "/" + file)
31+
filesize = stats[6]
32+
isdir = stats[0] & 0x4000
33+
34+
if filesize < 1000:
35+
sizestr = str(filesize) + " bytes"
36+
elif filesize < 1000000:
37+
sizestr = "%0.1f KB" % (filesize / 1000)
38+
else:
39+
sizestr = "%0.1f MB" % (filesize / 1000000)
40+
41+
prettyprintname = ""
42+
for _ in range(tabs):
43+
prettyprintname += " "
44+
prettyprintname += file
45+
if isdir:
46+
prettyprintname += "/"
47+
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
48+
49+
# recursively print directory contents
50+
if isdir:
51+
print_directory(path + "/" + file, tabs + 1)
52+
53+
54+
print("Files on filesystem:")
55+
print("====================")
56+
print_directory("/sd")

0 commit comments

Comments
 (0)