66
66
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VS1053.git"
67
67
68
68
# 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 )
104
104
# pylint: enable=bad-whitespace
105
105
106
106
107
107
class VS1053 :
108
108
"""Class-level buffer for read and write commands."""
109
+
109
110
# This is NOT thread/re-entrant safe (by design, for less memory hit).
110
111
_SCI_SPI_BUFFER = bytearray (4 )
111
112
112
113
def __init__ (self , spi , cs , xdcs , dreq ):
113
114
# Create SPI device for VS1053
114
115
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
+ )
116
119
# Setup control lines.
117
120
self ._xdcs = digitalio .DigitalInOut (xdcs )
118
121
self ._xdcs .switch_to_output (value = True )
@@ -122,8 +125,11 @@ def __init__(self, spi, cs, xdcs, dreq):
122
125
self .reset ()
123
126
# Check version is 4 (VS1053 ID).
124
127
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
+ )
127
133
128
134
def _sci_write (self , address , value ):
129
135
# Write a 16-bit big-endian value to the provided 8-bit address.
@@ -145,14 +151,16 @@ def _sci_read(self, address):
145
151
# pylint: disable=no-member
146
152
spi .configure (baudrate = _COMMAND_BAUDRATE )
147
153
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)
149
155
spi .readinto (self ._SCI_SPI_BUFFER , end = 2 )
150
156
# pylint: enable=no-member
151
157
return (self ._SCI_SPI_BUFFER [0 ] << 8 ) | self ._SCI_SPI_BUFFER [1 ]
152
158
153
159
def soft_reset (self ):
154
160
"""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
+ )
156
164
time .sleep (0.1 )
157
165
158
166
def reset (self ):
@@ -198,7 +206,7 @@ def byte_rate(self):
198
206
"""Return the bit rate in bytes per second (computed each second).
199
207
Useful to know if a song is being played and how fast it's happening.
200
208
"""
201
- self ._sci_write (_VS1053_REG_WRAMADDR , 0x1e05 )
209
+ self ._sci_write (_VS1053_REG_WRAMADDR , 0x1E05 )
202
210
return self ._sci_read (_VS1053_REG_WRAM )
203
211
204
212
def start_playback (self ):
@@ -207,17 +215,21 @@ def start_playback(self):
207
215
buffers of music data to the play_data function.
208
216
"""
209
217
# 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
+ )
211
221
# Resync.
212
- self ._sci_write (_VS1053_REG_WRAMADDR , 0x1e29 )
222
+ self ._sci_write (_VS1053_REG_WRAMADDR , 0x1E29 )
213
223
self ._sci_write (_VS1053_REG_WRAM , 0 )
214
224
# Set time to zero.
215
225
self .decode_time = 0
216
226
217
227
def stop_playback (self ):
218
228
"""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
+ )
221
233
222
234
def play_data (self , data_buffer , start = 0 , end = None ):
223
235
"""Send a buffer of file data to the VS1053 for playback. Make sure
@@ -250,8 +262,7 @@ def sine_test(self, n, seconds):
250
262
with self ._vs1053_spi as spi :
251
263
# pylint: disable=no-member
252
264
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 ]))
255
266
# pylint: enable=no-member
256
267
finally :
257
268
self ._xdcs .value = True
@@ -261,8 +272,7 @@ def sine_test(self, n, seconds):
261
272
with self ._vs1053_spi as spi :
262
273
# pylint: disable=no-member
263
274
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 ]))
266
276
# pylint: enable=no-member
267
277
finally :
268
278
self ._xdcs .value = True
0 commit comments