Skip to content

Commit a40dd83

Browse files
authored
Merge pull request #18 from tcfranks/main
Correct Missing Type Annotations
2 parents 7b79080 + 8ee4e02 commit a40dd83

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

adafruit_vs1053.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
wave test currently works. The problem is that pure Python code is currently
1414
too slow to keep up with feeding data to the VS1053 fast enough. There's no
1515
interrupt support so Python code has to monitor the DREQ line and provide a
16-
small buffer of data when ready, but the overhead of the interpretor means we
16+
small buffer of data when ready, but the overhead of the interpreter means we
1717
can't keep up. Optimizing SPI to use DMA transfers could help but ultimately
1818
an interrupt-based approach is likely what can make this work better (or C
1919
functions built in to custom builds that monitor the DREQ line and feed a
@@ -45,6 +45,14 @@
4545
from micropython import const
4646
from adafruit_bus_device.spi_device import SPIDevice
4747

48+
try:
49+
from typing import Optional
50+
from circuitpython_typing import ReadableBuffer
51+
from microcontroller import Pin
52+
from busio import SPI
53+
except ImportError:
54+
pass
55+
4856
__version__ = "0.0.0+auto.0"
4957
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VS1053.git"
5058

@@ -91,7 +99,7 @@ class VS1053:
9199
# This is NOT thread/re-entrant safe (by design, for less memory hit).
92100
_SCI_SPI_BUFFER = bytearray(4)
93101

94-
def __init__(self, spi, cs, xdcs, dreq):
102+
def __init__(self, spi: SPI, cs: Pin, xdcs: Pin, dreq: Pin) -> None:
95103
# Create SPI device for VS1053
96104
self._cs = digitalio.DigitalInOut(cs)
97105
self._vs1053_spi = SPIDevice(
@@ -107,12 +115,10 @@ def __init__(self, spi, cs, xdcs, dreq):
107115
# Check version is 4 (VS1053 ID).
108116
if self.version != 4:
109117
raise RuntimeError(
110-
"Expected version 4 (VS1053) but got: {} Check wiring!".format(
111-
self.version
112-
)
118+
f"Expected version 4 (VS1053) but got: {self.version} Check wiring!"
113119
)
114120

115-
def _sci_write(self, address, value):
121+
def _sci_write(self, address: int, value: int) -> None:
116122
# Write a 16-bit big-endian value to the provided 8-bit address.
117123
self._SCI_SPI_BUFFER[0] = _VS1053_SCI_WRITE
118124
self._SCI_SPI_BUFFER[1] = address & 0xFF
@@ -123,7 +129,7 @@ def _sci_write(self, address, value):
123129
spi.configure(baudrate=_COMMAND_BAUDRATE)
124130
spi.write(self._SCI_SPI_BUFFER)
125131

126-
def _sci_read(self, address):
132+
def _sci_read(self, address: int) -> int:
127133
# Read a 16-bit big-endian value from the provided 8-bit address.
128134
# Write a 16-bit big-endian value to the provided 8-bit address.
129135
self._SCI_SPI_BUFFER[0] = _VS1053_SCI_READ
@@ -137,59 +143,59 @@ def _sci_read(self, address):
137143
# pylint: enable=no-member
138144
return (self._SCI_SPI_BUFFER[0] << 8) | self._SCI_SPI_BUFFER[1]
139145

140-
def soft_reset(self):
146+
def soft_reset(self) -> None:
141147
"""Perform a quick soft reset of the VS1053."""
142148
self._sci_write(
143149
_VS1053_REG_MODE, _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_RESET
144150
)
145151
time.sleep(0.1)
146152

147-
def reset(self):
153+
def reset(self) -> None:
148154
"""Perform a longer full reset with clock and volume reset too."""
149155
self._xdcs.value = True
150156
self.soft_reset()
151157
time.sleep(0.1)
152158
self._sci_write(_VS1053_REG_CLOCKF, 0x6000)
153159
self.set_volume(40, 40)
154160

155-
def set_volume(self, left, right):
161+
def set_volume(self, left: int, right: int) -> None:
156162
"""Set the volume of the left and right channels to the provided byte
157163
value (0-255), the lower the louder.
158164
"""
159165
volume = ((left & 0xFF) << 8) | (right & 0xFF)
160166
self._sci_write(_VS1053_REG_VOLUME, volume)
161167

162168
@property
163-
def ready_for_data(self):
169+
def ready_for_data(self) -> bool:
164170
"""Return True if the VS1053 is ready to accept data, false otherwise."""
165171
return self._dreq.value
166172

167173
@property
168-
def version(self):
174+
def version(self) -> int:
169175
"""Return the status register version value."""
170176
return (self._sci_read(_VS1053_REG_STATUS) >> 4) & 0x0F
171177

172178
@property
173-
def decode_time(self):
179+
def decode_time(self) -> int:
174180
"""Return the decode time register value. This is the amount of time
175181
the current file has been played back in seconds."""
176182
return self._sci_read(_VS1053_REG_DECODETIME)
177183

178184
@decode_time.setter
179-
def decode_time(self, value):
185+
def decode_time(self, value: int) -> None:
180186
"""Set the decode time register value."""
181187
# From datasheet, set twice to ensure it is correctly set (pg. 43)
182188
self._sci_write(_VS1053_REG_DECODETIME, value)
183189

184190
@property
185-
def byte_rate(self):
191+
def byte_rate(self) -> int:
186192
"""Return the bit rate in bytes per second (computed each second).
187193
Useful to know if a song is being played and how fast it's happening.
188194
"""
189195
self._sci_write(_VS1053_REG_WRAMADDR, 0x1E05)
190196
return self._sci_read(_VS1053_REG_WRAM)
191197

192-
def start_playback(self):
198+
def start_playback(self) -> None:
193199
"""Prepare for playback of a file. After calling this check the
194200
ready_for_data property continually until true and then send in
195201
buffers of music data to the play_data function.
@@ -204,14 +210,16 @@ def start_playback(self):
204210
# Set time to zero.
205211
self.decode_time = 0
206212

207-
def stop_playback(self):
213+
def stop_playback(self) -> None:
208214
"""Stop any playback of audio."""
209215
self._sci_write(
210216
_VS1053_REG_MODE,
211217
_VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_CANCEL,
212218
)
213219

214-
def play_data(self, data_buffer, start=0, end=None):
220+
def play_data(
221+
self, data_buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None
222+
):
215223
"""Send a buffer of file data to the VS1053 for playback. Make sure
216224
the ready_for_data property is True before calling!
217225
"""
@@ -227,7 +235,7 @@ def play_data(self, data_buffer, start=0, end=None):
227235
finally:
228236
self._xdcs.value = True
229237

230-
def sine_test(self, n, seconds):
238+
def sine_test(self, n: int, seconds: float) -> None:
231239
"""Play a sine wave for the specified number of seconds. Useful to
232240
test the VS1053 is working.
233241
"""

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-typing

0 commit comments

Comments
 (0)