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