Skip to content

Commit 8d7f385

Browse files
committed
Metro ESP32-S3 guide code.
1 parent 70d280a commit 8d7f385

File tree

15 files changed

+453
-3
lines changed

15 files changed

+453
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
5+
"""
6+
import time
7+
import board
8+
import touchio
9+
10+
touch = touchio.TouchIn(board.A0)
11+
12+
while True:
13+
if touch.value:
14+
print("Pin touched!")
15+
time.sleep(0.1)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
5+
"""
6+
import time
7+
import board
8+
import touchio
9+
10+
touch_one = touchio.TouchIn(board.A0)
11+
touch_two = touchio.TouchIn(board.A5)
12+
13+
while True:
14+
if touch_one.value:
15+
print("Pin one touched!")
16+
if touch_two.value:
17+
print("Pin two touched!")
18+
time.sleep(0.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
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.A0, board.A1, board.A2)
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)
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.A0, board.A1, board.A2)
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""CircuitPython Essentials SD Card Read Demo"""
5+
6+
import os
7+
import digitalio
8+
import board
9+
import storage
10+
import adafruit_sdcard
11+
12+
# The SD_CS pin is the chip select line.
13+
SD_CS = board.SD_CS
14+
15+
# Connect to the card and mount the filesystem.
16+
cs = digitalio.DigitalInOut(SD_CS)
17+
sdcard = adafruit_sdcard.SDCard(board.SPI(), cs)
18+
vfs = storage.VfsFat(sdcard)
19+
storage.mount(vfs, "/sd")
20+
21+
# Use the filesystem as normal! Our files are under /sd
22+
23+
# This helper function will print the contents of the SD
24+
def print_directory(path, tabs=0):
25+
for file in os.listdir(path):
26+
stats = os.stat(path + "/" + file)
27+
filesize = stats[6]
28+
isdir = stats[0] & 0x4000
29+
30+
if filesize < 1000:
31+
sizestr = str(filesize) + " bytes"
32+
elif filesize < 1000000:
33+
sizestr = "%0.1f KB" % (filesize / 1000)
34+
else:
35+
sizestr = "%0.1f MB" % (filesize / 1000000)
36+
37+
prettyprintname = ""
38+
for _ in range(tabs):
39+
prettyprintname += " "
40+
prettyprintname += file
41+
if isdir:
42+
prettyprintname += "/"
43+
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
44+
45+
# recursively print directory contents
46+
if isdir:
47+
print_directory(path + "/" + file, tabs + 1)
48+
49+
50+
print("Files on filesystem:")
51+
print("====================")
52+
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Essentials Storage CP Filesystem boot.py file
5+
"""
6+
import board
7+
import digitalio
8+
import storage
9+
10+
pin = digitalio.DigitalInOut(board.A0)
11+
pin.switch_to_input(pull=digitalio.Pull.UP)
12+
13+
# If the pin is connected to ground, the filesystem is writable by CircuitPython
14+
storage.remount("/", readonly=pin.value)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Essentials Storage CP Filesystem code.py file
5+
6+
For use with boards with a built-in red LED.
7+
"""
8+
import time
9+
import board
10+
import digitalio
11+
import microcontroller
12+
13+
led = digitalio.DigitalInOut(board.LED)
14+
led.switch_to_output()
15+
16+
try:
17+
with open("/temperature.txt", "a") as temp_log:
18+
while True:
19+
# The microcontroller temperature in Celsius. Include the
20+
# math to do the C to F conversion here, if desired.
21+
temperature = microcontroller.cpu.temperature
22+
23+
# Write the temperature to the temperature.txt file every 10 seconds.
24+
temp_log.write('{0:.2f}\n'.format(temperature))
25+
temp_log.flush()
26+
27+
# Blink the LED on every write...
28+
led.value = True
29+
time.sleep(1) # ...for one second.
30+
led.value = False # Then turn it off...
31+
time.sleep(9) # ...for the other 9 seconds.
32+
33+
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
34+
delay = 0.5 # ...blink the LED every half second.
35+
if e.args[0] == 28: # If the file system is full...
36+
delay = 0.15 # ...blink the LED every 0.15 seconds!
37+
while True:
38+
led.value = not led.value
39+
time.sleep(delay)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""CircuitPython analog pin value example"""
4+
import time
5+
import board
6+
import analogio
7+
8+
analog_pin = analogio.AnalogIn(board.D3)
9+
10+
while True:
11+
print(analog_pin.value)
12+
time.sleep(0.1)

0 commit comments

Comments
 (0)