Skip to content

Commit da02c76

Browse files
authored
Merge pull request #48 from tekktrik/doc/add-typing
Add type annotations
2 parents baa99d7 + 07a78e6 commit da02c76

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

adafruit_sdcard.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
from micropython import const
4545
from adafruit_bus_device import spi_device
4646

47+
try:
48+
from typing import Union, Optional
49+
from busio import SPI
50+
from digitalio import DigitalInOut
51+
from circuitpython_typing import ReadableBuffer, WriteableBuffer
52+
except ImportError:
53+
pass
54+
4755
__version__ = "0.0.0-auto.0"
4856
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SD.git"
4957

@@ -86,7 +94,7 @@ class SDCard:
8694
8795
"""
8896

89-
def __init__(self, spi, cs, baudrate=1320000):
97+
def __init__(self, spi: SPI, cs: DigitalInOut, baudrate: int = 1320000) -> None:
9098
# Create an SPIDevice running at a lower initialization baudrate first.
9199
self._spi = spi_device.SPIDevice(spi, cs, baudrate=250000, extra_clocks=8)
92100

@@ -102,7 +110,7 @@ def __init__(self, spi, cs, baudrate=1320000):
102110
# Create a new SPIDevice with the (probably) higher operating baudrate.
103111
self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate, extra_clocks=8)
104112

105-
def _init_card(self, chip_select):
113+
def _init_card(self, chip_select: DigitalInOut) -> None:
106114
"""Initialize the card in SPI mode."""
107115
# clock card at least 80 cycles with cs high
108116
with self._spi as card:
@@ -152,7 +160,7 @@ def _init_card(self, chip_select):
152160
if self._cmd(card, 16, 512, 0x15) != 0:
153161
raise OSError("can't set 512 block size")
154162

155-
def _init_card_v1(self, card):
163+
def _init_card_v1(self, card: SPI) -> None:
156164
"""Initialize v1 SDCards which use byte addressing."""
157165
for _ in range(_CMD_TIMEOUT):
158166
self._cmd(card, 55, 0, 0)
@@ -161,7 +169,7 @@ def _init_card_v1(self, card):
161169
return
162170
raise OSError("timeout waiting for v1 card")
163171

164-
def _init_card_v2(self, card):
172+
def _init_card_v2(self, card: SPI) -> None:
165173
"""Initialize v2 SDCards which use 512-byte block addressing."""
166174
ocr = bytearray(4)
167175
for _ in range(_CMD_TIMEOUT):
@@ -180,7 +188,7 @@ def _init_card_v2(self, card):
180188
return
181189
raise OSError("timeout waiting for v2 card")
182190

183-
def _wait_for_ready(self, card, timeout=0.3):
191+
def _wait_for_ready(self, card: SPI, timeout: float = 0.3) -> None:
184192
"""
185193
Wait for the card to clock out 0xff to indicate its ready.
186194
@@ -196,17 +204,25 @@ def _wait_for_ready(self, card, timeout=0.3):
196204
# pylint: disable=no-member
197205
# no-member disable should be reconsidered when it can be tested
198206
def _cmd(
199-
self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True
200-
):
207+
self,
208+
card: SPI,
209+
cmd: int,
210+
arg: Union[int, ReadableBuffer] = 0,
211+
crc: int = 0,
212+
response_buf: Optional[WriteableBuffer] = None,
213+
data_block: bool = True,
214+
wait: bool = True,
215+
) -> int:
201216
"""
202217
Issue a command to the card and read an optional data response.
203218
204219
:param busio.SPI card: The locked SPI bus.
205220
:param int cmd: The command number.
206221
:param int|buf(4) arg: The command argument
207222
:param int crc: The crc to allow the card to verify the command and argument.
208-
:param bytearray response_buf: Buffer to read a data block response into.
223+
:param WriteableBuffer response_buf: Buffer to read a data block response into.
209224
:param bool data_block: True if the response data is in a data block.
225+
:param bool wait: True if the command should wait until the card is ready
210226
"""
211227
# create and send the command
212228
buf = self._cmdbuf
@@ -252,14 +268,22 @@ def _cmd(
252268
# pylint: enable-msg=too-many-arguments
253269

254270
# pylint: disable-msg=too-many-arguments
255-
def _block_cmd(self, card, cmd, block, crc, response_buf=None):
271+
def _block_cmd(
272+
self,
273+
card: SPI,
274+
cmd: int,
275+
block: int,
276+
crc: int,
277+
response_buf: Optional[WriteableBuffer] = None,
278+
) -> int:
256279
"""
257280
Issue a command to the card with a block argument.
258281
259282
:param busio.SPI card: The locked SPI bus.
260283
:param int cmd: The command number.
261284
:param int block: The relevant block.
262285
:param int crc: The crc to allow the card to verify the command and argument.
286+
:param WriteableBuffer response_buf: Buffer to read a data block response into.
263287
"""
264288
if self._cdv == 1:
265289
return self._cmd(card, cmd, block, crc, response_buf=response_buf)
@@ -301,12 +325,13 @@ def _block_cmd(self, card, cmd, block, crc, response_buf=None):
301325

302326
# pylint: enable-msg=too-many-arguments
303327

304-
def _cmd_nodata(self, card, cmd, response=0xFF):
328+
def _cmd_nodata(self, card: SPI, cmd: int, response: int = 0xFF) -> int:
305329
"""
306330
Issue a command to the card with no argument.
307331
308332
:param busio.SPI card: The locked SPI bus.
309333
:param int cmd: The command number.
334+
:param int response: The expected response, default is ``0xFF``
310335
"""
311336
buf = self._cmdbuf
312337
buf[0] = cmd
@@ -319,12 +344,14 @@ def _cmd_nodata(self, card, cmd, response=0xFF):
319344
return 0 # OK
320345
return 1 # timeout
321346

322-
def _readinto(self, card, buf, start=0, end=None):
347+
def _readinto(
348+
self, card: SPI, buf: WriteableBuffer, start: int = 0, end: Optional[int] = None
349+
) -> None:
323350
"""
324351
Read a data block into buf.
325352
326353
:param busio.SPI card: The locked SPI bus.
327-
:param bytearray buf: The buffer to write into
354+
:param WriteableBuffer buf: The buffer to write into
328355
:param int start: The first index to write data at
329356
:param int end: The index after the last byte to write to.
330357
"""
@@ -342,13 +369,20 @@ def _readinto(self, card, buf, start=0, end=None):
342369
card.readinto(self._cmdbuf, end=2, write_value=0xFF)
343370

344371
# pylint: disable-msg=too-many-arguments
345-
def _write(self, card, token, buf, start=0, end=None):
372+
def _write(
373+
self,
374+
card: SPI,
375+
token: int,
376+
buf: ReadableBuffer,
377+
start: int = 0,
378+
end: Optional[int] = None,
379+
) -> int:
346380
"""
347381
Write a data block to the card.
348382
349383
:param busio.SPI card: The locked SPI bus.
350384
:param int token: The start token
351-
:param bytearray buf: The buffer to write from
385+
:param ReadableBuffer buf: The buffer to write from
352386
:param int start: The first index to read data from
353387
:param int end: The index after the last byte to read from.
354388
"""
@@ -386,7 +420,7 @@ def _write(self, card, token, buf, start=0, end=None):
386420

387421
# pylint: enable-msg=too-many-arguments
388422

389-
def count(self):
423+
def count(self) -> int:
390424
"""
391425
Returns the total number of sectors.
392426
@@ -395,12 +429,12 @@ def count(self):
395429
"""
396430
return self._sectors
397431

398-
def readblocks(self, start_block, buf):
432+
def readblocks(self, start_block: int, buf: WriteableBuffer) -> int:
399433
"""
400434
Read one or more blocks from the card
401435
402436
:param int start_block: The block to start reading from
403-
:param bytearray buf: The buffer to write into. Length must be multiple of 512.
437+
:param WriteableBuffer buf: The buffer to write into. Length must be multiple of 512.
404438
"""
405439
nblocks, err = divmod(len(buf), 512)
406440
assert nblocks and not err, "Buffer length is invalid"
@@ -429,12 +463,12 @@ def readblocks(self, start_block, buf):
429463
ret = self._single_byte[0]
430464
return 0
431465

432-
def writeblocks(self, start_block, buf):
466+
def writeblocks(self, start_block: int, buf: ReadableBuffer) -> int:
433467
"""
434468
Write one or more blocks to the card
435469
436470
:param int start_block: The block to start writing to
437-
:param bytearray buf: The buffer to write into. Length must be multiple of 512.
471+
:param ReadableBuffer buf: The buffer to write into. Length must be multiple of 512.
438472
"""
439473
nblocks, err = divmod(len(buf), 512)
440474
assert nblocks and not err, "Buffer length is invalid"
@@ -462,7 +496,7 @@ def writeblocks(self, start_block, buf):
462496
return 0
463497

464498

465-
def _calculate_crc_table():
499+
def _calculate_crc_table() -> bytearray:
466500
"""Precompute the table used in calculate_crc."""
467501
# Code converted from https://github.com/hazelnusse/crc7/blob/master/crc7.cc by devoh747
468502
# With permission from Dale Lukas Peterson <[email protected]>
@@ -487,7 +521,7 @@ def _calculate_crc_table():
487521
CRC_TABLE = _calculate_crc_table()
488522

489523

490-
def calculate_crc(message):
524+
def calculate_crc(message: ReadableBuffer) -> int:
491525
"""
492526
Calculate the CRC of message[0:5], using a precomputed table in CRC_TABLE.
493527

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
Adafruit-Blinka
5+
Adafruit-Blinka>=7.0.0
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-typing

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
# Author details
3535
author="Adafruit Industries",
3636
author_email="[email protected]",
37-
install_requires=["Adafruit-Blinka", "adafruit-circuitpython-busdevice"],
37+
install_requires=[
38+
"Adafruit-Blinka>=7.0.0",
39+
"adafruit-circuitpython-busdevice",
40+
"adafruit-circuitpython-typing",
41+
],
3842
# Choose your license
3943
license="MIT",
4044
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)