Created
March 8, 2022 03:45
-
-
Save idriszmy/392d838254d9f79a9bcd676f712681f7 to your computer and use it in GitHub Desktop.
Talking Photo? Program using CircuitPython on Maker Pi Pico.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Talking Photo? Program using CircuitPython on Maker Pi Pico. | |
Items: | |
- Maker Pi Pico | |
https://my.cytron.io/p-maker-pi-pico | |
- Capacitive Touch Sensor | |
https://my.cytron.io/p-ttp223-capacitive-touch-sensor-module | |
- MakerDisk microSD Card | |
https://my.cytron.io/p-raspberry-pi-approved-makerdisk-microsd-with-rpi-os | |
- 5mm Copper Foil Tape | |
https://my.cytron.io/p-5mm-copper-foil-tape-with-conductive-adhesive-25m | |
- 6W Stereo Speaker | |
https://my.cytron.io/p-6w-stereo-usb-powered-3-5mm-jack-speaker-black | |
- USB Micro B Cable | |
https://my.cytron.io/p-usb-micro-b-cable | |
References: | |
- https://gist.github.com/idriszmy/47ce97a34c07a9b3da79d4121269a40b | |
Last update: 8 Mar 2022 | |
""" | |
import os | |
import time | |
import board | |
import touchio | |
import busio | |
import sdcardio | |
import storage | |
from audiomp3 import MP3Decoder # pylint: disable=wrong-import-position | |
try: | |
from audioio import AudioOut | |
except ImportError: | |
try: | |
from audiopwmio import PWMAudioOut as AudioOut | |
except ImportError: | |
pass # not always supported by every board! | |
touch_pad = board.GP5 | |
touch = touchio.TouchIn(touch_pad) | |
spi = busio.SPI(board.GP10, board.GP11, board.GP12) | |
sd = sdcardio.SDCard(spi, board.GP15) | |
vfs = storage.VfsFat(sd) | |
storage.mount(vfs, '/sd') | |
mp3files = sorted("/sd/" + filename for filename in os.listdir("/sd") | |
if filename.lower().endswith("mp3")) | |
# You have to specify some mp3 file when creating the decoder | |
mp3 = open(mp3files[0], "rb") | |
decoder = MP3Decoder(mp3) | |
audio = AudioOut(board.GP18, right_channel=board.GP19) | |
total_mp3 = 3 | |
count = 0 | |
while True: | |
if touch.value: | |
print("Touched! Playing mp3-{}...".format(count)) | |
filename = mp3files[count] | |
count += 1 | |
if count == total_mp3: | |
count = 0 | |
# Updating the .file property of the existing decoder | |
# helps avoid running out of memory (MemoryError exception) | |
decoder.file = open(filename, "rb") | |
audio.play(decoder) | |
while audio.playing: | |
pass | |
print("Done.") | |
time.sleep(0.05) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment