Skip to content

Commit 49d71b7

Browse files
authored
Merge pull request #2621 from kattni/m7sd
Adding M7 SD code.
2 parents 2ee27f9 + 31013d4 commit 49d71b7

File tree

9 files changed

+213
-0
lines changed

9 files changed

+213
-0
lines changed

Adafruit_Metro_M7_SD/I2S/Tone/code.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
import time
9+
import array
10+
import math
11+
import audiocore
12+
import board
13+
import audiobusio
14+
15+
audio = audiobusio.I2SOut(board.D10, board.D9, board.D12)
16+
17+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
18+
frequency = 440 # Set this to the Hz of the tone you want to generate.
19+
length = 8000 // frequency
20+
sine_wave = array.array("h", [0] * length)
21+
for i in range(length):
22+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
23+
sine_wave_sample = audiocore.RawSample(sine_wave)
24+
25+
while True:
26+
audio.play(sine_wave_sample, loop=True)
27+
time.sleep(1)
28+
audio.stop()
29+
time.sleep(1)
413 KB
Binary file not shown.

Adafruit_Metro_M7_SD/I2S/WAV/code.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
import audiocore
8+
import board
9+
import audiobusio
10+
11+
audio = audiobusio.I2SOut(board.D10, board.D9, board.D12)
12+
13+
with open("StreetChicken.wav", "rb") as wave_file:
14+
wav = audiocore.WaveFile(wave_file)
15+
16+
print("Playing wav file!")
17+
audio.play(wav)
18+
while audio.playing:
19+
pass
20+
21+
print("Done!")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython PWM Audio Out tone example
7+
Plays a tone for one second on, one
8+
second off, in a loop.
9+
"""
10+
import time
11+
import array
12+
import math
13+
import board
14+
from audiocore import RawSample
15+
from audiopwmio import PWMAudioOut as AudioOut
16+
17+
audio = AudioOut(board.A1)
18+
19+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
20+
frequency = 440 # Set this to the Hz of the tone you want to generate.
21+
length = 8000 // frequency
22+
sine_wave = array.array("H", [0] * length)
23+
for i in range(length):
24+
sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
25+
26+
sine_wave_sample = RawSample(sine_wave)
27+
28+
while True:
29+
audio.play(sine_wave_sample, loop=True)
30+
time.sleep(1)
31+
audio.stop()
32+
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: 2018 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython PWM Audio Out WAV example
7+
Play a WAV file once.
8+
"""
9+
import board
10+
from audiocore import WaveFile
11+
from audiopwmio import PWMAudioOut as AudioOut
12+
13+
audio = AudioOut(board.A1)
14+
15+
with open("StreetChicken.wav", "rb") as wave_file:
16+
wave = WaveFile(wave_file)
17+
print("Playing wav file!")
18+
audio.play(wave)
19+
while audio.playing:
20+
pass
21+
22+
print("Done!")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
import os
9+
import digitalio
10+
import board
11+
import storage
12+
import adafruit_sdcard
13+
14+
# The SD_CS pin is the chip select line.
15+
SD_CS = board.SD_CS
16+
17+
# Connect to the card and mount the filesystem.
18+
cs = digitalio.DigitalInOut(SD_CS)
19+
sdcard = adafruit_sdcard.SDCard(board.SPI(), cs)
20+
vfs = storage.VfsFat(sdcard)
21+
storage.mount(vfs, "/sd")
22+
23+
# Use the filesystem as normal! Our files are under /sd
24+
25+
# This helper function will print the contents of the SD
26+
def print_directory(path, tabs=0):
27+
for file in os.listdir(path):
28+
stats = os.stat(path + "/" + file)
29+
filesize = stats[6]
30+
isdir = stats[0] & 0x4000
31+
32+
if filesize < 1000:
33+
sizestr = str(filesize) + " bytes"
34+
elif filesize < 1000000:
35+
sizestr = "%0.1f KB" % (filesize / 1000)
36+
else:
37+
sizestr = "%0.1f MB" % (filesize / 1000000)
38+
39+
prettyprintname = ""
40+
for _ in range(tabs):
41+
prettyprintname += " "
42+
prettyprintname += file
43+
if isdir:
44+
prettyprintname += "/"
45+
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
46+
47+
# recursively print directory contents
48+
if isdir:
49+
print_directory(path + "/" + file, tabs + 1)
50+
51+
52+
print("Files on filesystem:")
53+
print("====================")
54+
print_directory("/sd")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython Essentials SD Card Write Demo
7+
"""
8+
9+
import time
10+
import adafruit_sdcard
11+
import board
12+
import digitalio
13+
import microcontroller
14+
import storage
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(board.SPI(), 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+
print("Logging temperature to filesystem")
28+
# append to the file!
29+
while True:
30+
# open file for append
31+
with open("/sd/temperature.txt", "a") as f:
32+
t = microcontroller.cpu.temperature
33+
print("Temperature = %0.1f" % t)
34+
f.write("%0.1f\n" % t)
35+
# file is saved
36+
time.sleep(1)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Digital Input Example - Blinking an LED using a button switch.
5+
"""
6+
import board
7+
import digitalio
8+
9+
led = digitalio.DigitalInOut(board.LED)
10+
led.direction = digitalio.Direction.OUTPUT
11+
12+
button = digitalio.DigitalInOut(board.A0)
13+
button.switch_to_input(pull=digitalio.Pull.UP)
14+
15+
while True:
16+
if not button.value:
17+
led.value = True
18+
else:
19+
led.value = False

0 commit comments

Comments
 (0)