6666__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VS1053.git"
6767
6868# pylint: disable=bad-whitespace
69- _COMMAND_BAUDRATE = const (250000 ) # Speed for command transfers (MUST be slow)
70- _DATA_BAUDRATE = const (8000000 ) # Speed for data transfers (fast!)
71-
72- _VS1053_SCI_READ = const (0x03 )
73- _VS1053_SCI_WRITE = const (0x02 )
74-
75- _VS1053_REG_MODE = const (0x00 )
76- _VS1053_REG_STATUS = const (0x01 )
77- _VS1053_REG_BASS = const (0x02 )
78- _VS1053_REG_CLOCKF = const (0x03 )
79- _VS1053_REG_DECODETIME = const (0x04 )
80- _VS1053_REG_AUDATA = const (0x05 )
81- _VS1053_REG_WRAM = const (0x06 )
82- _VS1053_REG_WRAMADDR = const (0x07 )
83- _VS1053_REG_HDAT0 = const (0x08 )
84- _VS1053_REG_HDAT1 = const (0x09 )
85- _VS1053_REG_VOLUME = const (0x0B )
86-
87- _VS1053_GPIO_DDR = const (0xC017 )
88- _VS1053_GPIO_IDATA = const (0xC018 )
89- _VS1053_GPIO_ODATA = const (0xC019 )
90-
91- _VS1053_INT_ENABLE = const (0xC01A )
92-
93- _VS1053_MODE_SM_DIFF = const (0x0001 )
94- _VS1053_MODE_SM_LAYER12 = const (0x0002 )
95- _VS1053_MODE_SM_RESET = const (0x0004 )
96- _VS1053_MODE_SM_CANCEL = const (0x0008 )
97- _VS1053_MODE_SM_EARSPKLO = const (0x0010 )
98- _VS1053_MODE_SM_TESTS = const (0x0020 )
99- _VS1053_MODE_SM_STREAM = const (0x0040 )
100- _VS1053_MODE_SM_SDINEW = const (0x0800 )
101- _VS1053_MODE_SM_ADPCM = const (0x1000 )
102- _VS1053_MODE_SM_LINE1 = const (0x4000 )
103- _VS1053_MODE_SM_CLKRANGE = const (0x8000 )
69+ _COMMAND_BAUDRATE = const (250000 ) # Speed for command transfers (MUST be slow)
70+ _DATA_BAUDRATE = const (8000000 ) # Speed for data transfers (fast!)
71+
72+ _VS1053_SCI_READ = const (0x03 )
73+ _VS1053_SCI_WRITE = const (0x02 )
74+
75+ _VS1053_REG_MODE = const (0x00 )
76+ _VS1053_REG_STATUS = const (0x01 )
77+ _VS1053_REG_BASS = const (0x02 )
78+ _VS1053_REG_CLOCKF = const (0x03 )
79+ _VS1053_REG_DECODETIME = const (0x04 )
80+ _VS1053_REG_AUDATA = const (0x05 )
81+ _VS1053_REG_WRAM = const (0x06 )
82+ _VS1053_REG_WRAMADDR = const (0x07 )
83+ _VS1053_REG_HDAT0 = const (0x08 )
84+ _VS1053_REG_HDAT1 = const (0x09 )
85+ _VS1053_REG_VOLUME = const (0x0B )
86+
87+ _VS1053_GPIO_DDR = const (0xC017 )
88+ _VS1053_GPIO_IDATA = const (0xC018 )
89+ _VS1053_GPIO_ODATA = const (0xC019 )
90+
91+ _VS1053_INT_ENABLE = const (0xC01A )
92+
93+ _VS1053_MODE_SM_DIFF = const (0x0001 )
94+ _VS1053_MODE_SM_LAYER12 = const (0x0002 )
95+ _VS1053_MODE_SM_RESET = const (0x0004 )
96+ _VS1053_MODE_SM_CANCEL = const (0x0008 )
97+ _VS1053_MODE_SM_EARSPKLO = const (0x0010 )
98+ _VS1053_MODE_SM_TESTS = const (0x0020 )
99+ _VS1053_MODE_SM_STREAM = const (0x0040 )
100+ _VS1053_MODE_SM_SDINEW = const (0x0800 )
101+ _VS1053_MODE_SM_ADPCM = const (0x1000 )
102+ _VS1053_MODE_SM_LINE1 = const (0x4000 )
103+ _VS1053_MODE_SM_CLKRANGE = const (0x8000 )
104104# pylint: enable=bad-whitespace
105105
106106
107107class VS1053 :
108108 """Class-level buffer for read and write commands."""
109+
109110 # This is NOT thread/re-entrant safe (by design, for less memory hit).
110111 _SCI_SPI_BUFFER = bytearray (4 )
111112
112113 def __init__ (self , spi , cs , xdcs , dreq ):
113114 # Create SPI device for VS1053
114115 self ._cs = digitalio .DigitalInOut (cs )
115- self ._vs1053_spi = SPIDevice (spi , self ._cs , baudrate = _COMMAND_BAUDRATE , polarity = 0 , phase = 0 )
116+ self ._vs1053_spi = SPIDevice (
117+ spi , self ._cs , baudrate = _COMMAND_BAUDRATE , polarity = 0 , phase = 0
118+ )
116119 # Setup control lines.
117120 self ._xdcs = digitalio .DigitalInOut (xdcs )
118121 self ._xdcs .switch_to_output (value = True )
@@ -122,8 +125,11 @@ def __init__(self, spi, cs, xdcs, dreq):
122125 self .reset ()
123126 # Check version is 4 (VS1053 ID).
124127 if self .version != 4 :
125- raise RuntimeError ('Expected version 4 (VS1053) but got: {} Check wiring!'
126- .format (self .version ))
128+ raise RuntimeError (
129+ "Expected version 4 (VS1053) but got: {} Check wiring!" .format (
130+ self .version
131+ )
132+ )
127133
128134 def _sci_write (self , address , value ):
129135 # Write a 16-bit big-endian value to the provided 8-bit address.
@@ -145,14 +151,16 @@ def _sci_read(self, address):
145151 # pylint: disable=no-member
146152 spi .configure (baudrate = _COMMAND_BAUDRATE )
147153 spi .write (self ._SCI_SPI_BUFFER , end = 2 )
148- time .sleep (0.00001 ) # Delay 10 microseconds (at least)
154+ time .sleep (0.00001 ) # Delay 10 microseconds (at least)
149155 spi .readinto (self ._SCI_SPI_BUFFER , end = 2 )
150156 # pylint: enable=no-member
151157 return (self ._SCI_SPI_BUFFER [0 ] << 8 ) | self ._SCI_SPI_BUFFER [1 ]
152158
153159 def soft_reset (self ):
154160 """Perform a quick soft reset of the VS1053."""
155- self ._sci_write (_VS1053_REG_MODE , _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_RESET )
161+ self ._sci_write (
162+ _VS1053_REG_MODE , _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_RESET
163+ )
156164 time .sleep (0.1 )
157165
158166 def reset (self ):
@@ -198,7 +206,7 @@ def byte_rate(self):
198206 """Return the bit rate in bytes per second (computed each second).
199207 Useful to know if a song is being played and how fast it's happening.
200208 """
201- self ._sci_write (_VS1053_REG_WRAMADDR , 0x1e05 )
209+ self ._sci_write (_VS1053_REG_WRAMADDR , 0x1E05 )
202210 return self ._sci_read (_VS1053_REG_WRAM )
203211
204212 def start_playback (self ):
@@ -207,17 +215,21 @@ def start_playback(self):
207215 buffers of music data to the play_data function.
208216 """
209217 # Reset playback.
210- self ._sci_write (_VS1053_REG_MODE , _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW )
218+ self ._sci_write (
219+ _VS1053_REG_MODE , _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW
220+ )
211221 # Resync.
212- self ._sci_write (_VS1053_REG_WRAMADDR , 0x1e29 )
222+ self ._sci_write (_VS1053_REG_WRAMADDR , 0x1E29 )
213223 self ._sci_write (_VS1053_REG_WRAM , 0 )
214224 # Set time to zero.
215225 self .decode_time = 0
216226
217227 def stop_playback (self ):
218228 """Stop any playback of audio."""
219- self ._sci_write (_VS1053_REG_MODE , _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW |
220- _VS1053_MODE_SM_CANCEL )
229+ self ._sci_write (
230+ _VS1053_REG_MODE ,
231+ _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_CANCEL ,
232+ )
221233
222234 def play_data (self , data_buffer , start = 0 , end = None ):
223235 """Send a buffer of file data to the VS1053 for playback. Make sure
@@ -250,8 +262,7 @@ def sine_test(self, n, seconds):
250262 with self ._vs1053_spi as spi :
251263 # pylint: disable=no-member
252264 spi .configure (baudrate = _DATA_BAUDRATE )
253- spi .write (bytes ([0x53 , 0xEF , 0x6E , n & 0xFF , 0x00 , 0x00 ,
254- 0x00 , 0x00 ]))
265+ spi .write (bytes ([0x53 , 0xEF , 0x6E , n & 0xFF , 0x00 , 0x00 , 0x00 , 0x00 ]))
255266 # pylint: enable=no-member
256267 finally :
257268 self ._xdcs .value = True
@@ -261,8 +272,7 @@ def sine_test(self, n, seconds):
261272 with self ._vs1053_spi as spi :
262273 # pylint: disable=no-member
263274 spi .configure (baudrate = _DATA_BAUDRATE )
264- spi .write (bytes ([0x45 , 0x78 , 0x69 , 0x74 , 0x00 , 0x00 , 0x00 ,
265- 0x00 ]))
275+ spi .write (bytes ([0x45 , 0x78 , 0x69 , 0x74 , 0x00 , 0x00 , 0x00 , 0x00 ]))
266276 # pylint: enable=no-member
267277 finally :
268278 self ._xdcs .value = True
0 commit comments