Simple test
Ensure your device works with this simple test.
examples/vs1053_simpletest.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Example of sound playback from VS1053 FeatherWing. Can be modified to work
5# with the breakout by changing the SD card and SPI pins mentioned below.
6# NOTE:
7# Unfortunately this doesn't work--the loop isn't fast enough to feed the VS1053
8# data at the rate it needs for playback. You'll see very erratic behavior with
9# the VS1053 making static, stopping, and eventually requiring a hard reset.
10# We'll need to look into interrupt support perhaps to monitor DREQ like in the
11# arduino library. Basic sine wave playback does however work and monitoring
12# of attributes like status register and other VS1053 state is accessible.
13import adafruit_sdcard
14import board
15import busio
16import digitalio
17import storage
18
19import adafruit_vs1053
20
21# Define pins connected to VS1053:
22
23# For FeatherWing with Feather M0:
24SDCS = board.D5 # Pin connected to SD card CS line.
25MP3CS = board.D6 # Pin connected to VS1053 CS line.
26DREQ = board.D9 # Pin connected to VS1053 DREQ line.
27XDCS = board.D10 # Pin connected to VS1053 D/C line.
28
29
30# Other configuration:
31PLAYBACK_FILE = "/sd/test.wav" # Name of file to play.
32# This should be the full path
33# including /sd prefix if on
34# sd card.
35
36BUFFER_SIZE = 128 # Size in bytes of the MP3 data buffer for sending data to
37# the VS1053.
38
39
40# Setup SPI bus (hardware SPI).
41spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
42
43# Setup SD card and mount it in the filesystem.
44sd_cs = digitalio.DigitalInOut(SDCS)
45sdcard = adafruit_sdcard.SDCard(spi, sd_cs)
46vfs = storage.VfsFat(sdcard)
47storage.mount(vfs, "/sd")
48
49# To list all the files on the SD card root uncomment:
50# import os
51# print('SD card root contains:')
52# print(os.listdir('/sd'))
53
54# Setup VS1053.
55vs1053 = adafruit_vs1053.VS1053(spi, MP3CS, XDCS, DREQ)
56
57# Set volume of left and right channels.
58# Value ranges from 0 to 255 for each channel, the lower the higher volume.
59vs1053.set_volume(0, 0)
60
61# Play a test tone (this works).
62print("Playing test tone for two seconds...")
63vs1053.sine_test(0x44, 2.0)
64print("Done playing tone!")
65
66# Play back a MP3 file by starting playback, then reading a buffer of data
67# at a time and sending it to the VS1053.
68# Unfortunately this doesn't work--the loop isn't fast enough to feed the VS1053
69# data at the rate it needs for playback. You'll see very erratic behavior with
70# the VS1053 making static, stopping, and eventually requiring a hard reset.
71# We'll need to look into interrupt support perhaps to monitor DREQ like in the
72# arduino library.
73print(f"Playing {PLAYBACK_FILE}...")
74vs1053.start_playback()
75with open(PLAYBACK_FILE, "rb") as infile:
76 music_data = infile.read(BUFFER_SIZE)
77 while music_data:
78 while not vs1053.ready_for_data:
79 pass
80 vs1053.play_data(music_data, end=len(music_data))
81 music_data = infile.read(BUFFER_SIZE)
82
83print("Done!")