Skip to content

Commit 7aca898

Browse files
authoredMay 15, 2025··
Merge pull request #33 from adafruit/use_ruff
change to ruff
2 parents ef02c3a + 3390908 commit 7aca898

14 files changed

+182
-526
lines changed
 

‎.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
.py text eol=lf
6+
.rst text eol=lf
7+
.txt text eol=lf
8+
.yaml text eol=lf
9+
.toml text eol=lf
10+
.license text eol=lf
11+
.md text eol=lf

‎.pre-commit-config.yaml

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
44

55
repos:
6-
- repo: https://github.com/python/black
7-
rev: 23.3.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v1.1.2
12-
hooks:
13-
- id: reuse
146
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
7+
rev: v4.5.0
168
hooks:
179
- id: check-yaml
1810
- id: end-of-file-fixer
1911
- id: trailing-whitespace
20-
- repo: https://github.com/pycqa/pylint
21-
rev: v2.17.4
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.3.4
2214
hooks:
23-
- id: pylint
24-
name: pylint (library code)
25-
types: [python]
26-
args:
27-
- --disable=consider-using-f-string
28-
exclude: "^(docs/|examples/|tests/|setup.py$)"
29-
- id: pylint
30-
name: pylint (example code)
31-
description: Run pylint rules on "examples/*.py" files
32-
types: [python]
33-
files: "^examples/"
34-
args:
35-
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
36-
- id: pylint
37-
name: pylint (test code)
38-
description: Run pylint rules on "tests/*.py" files
39-
types: [python]
40-
files: "^tests/"
41-
args:
42-
- --disable=missing-docstring,consider-using-f-string,duplicate-code
15+
- id: ruff-format
16+
- id: ruff
17+
args: ["--fix"]
18+
- repo: https://github.com/fsfe/reuse-tool
19+
rev: v3.0.1
20+
hooks:
21+
- id: reuse

‎.pylintrc

Lines changed: 0 additions & 399 deletions
This file was deleted.

‎README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_BitbangIO/actions
1414
:alt: Build Status
1515

16-
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
17-
:target: https://github.com/psf/black
18-
:alt: Code Style: Black
16+
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
17+
:target: https://github.com/astral-sh/ruff
18+
:alt: Code Style: Ruff
1919

2020
A library for adding bitbang I2C and SPI to CircuitPython without the built-in bitbangio module.
2121
The interface is intended to be the same as bitbangio and therefore there is no bit order or chip

‎adafruit_bitbangio.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
"""
2525

2626
try:
27-
from typing import List, Optional, Type
28-
from typing_extensions import Literal
2927
from types import TracebackType
30-
from circuitpython_typing import WriteableBuffer, ReadableBuffer
28+
from typing import List, Optional, Type
29+
30+
from circuitpython_typing import ReadableBuffer, WriteableBuffer
3131
from microcontroller import Pin
32+
from typing_extensions import Literal
3233
except ImportError:
3334
pass
3435

3536
# imports
3637
from time import monotonic
38+
3739
from digitalio import DigitalInOut
3840

3941
__version__ = "0.0.0+auto.0"
@@ -79,20 +81,15 @@ def __exit__(
7981
) -> None:
8082
self.deinit()
8183

82-
# pylint: disable=no-self-use
8384
def deinit(self) -> None:
8485
"""Free any hardware used by the object."""
8586
return
8687

87-
# pylint: enable=no-self-use
88-
8988

9089
class I2C(_BitBangIO):
9190
"""Software-based implementation of the I2C protocol over GPIO pins."""
9291

93-
def __init__(
94-
self, scl: Pin, sda: Pin, *, frequency: int = 400000, timeout: float = 1
95-
) -> None:
92+
def __init__(self, scl: Pin, sda: Pin, *, frequency: int = 400000, timeout: float = 1) -> None:
9693
"""Initialize bitbang (or software) based I2C. Must provide the I2C
9794
clock, and data pin numbers.
9895
"""
@@ -312,9 +309,7 @@ def _read(self, address: int, length: int) -> bytearray:
312309
class SPI(_BitBangIO):
313310
"""Software-based implementation of the SPI protocol over GPIO pins."""
314311

315-
def __init__(
316-
self, clock: Pin, MOSI: Optional[Pin] = None, MISO: Optional[Pin] = None
317-
) -> None:
312+
def __init__(self, clock: Pin, MOSI: Optional[Pin] = None, MISO: Optional[Pin] = None) -> None:
318313
"""Initialize bit bang (or software) based SPI. Must provide the SPI
319314
clock, and optionally MOSI and MISO pin numbers. If MOSI is set to None
320315
then writes will be disabled and fail with an error, likewise for MISO
@@ -367,9 +362,9 @@ def configure(
367362
raise ValueError("bits must be an integer")
368363
if bits < 1 or bits > 8:
369364
raise ValueError("bits must be in the range of 1-8")
370-
if polarity not in (0, 1):
365+
if polarity not in {0, 1}:
371366
raise ValueError("polarity must be either 0 or 1")
372-
if phase not in (0, 1):
367+
if phase not in {0, 1}:
373368
raise ValueError("phase must be either 0 or 1")
374369
self._baudrate = baudrate
375370
self._polarity = polarity
@@ -401,9 +396,7 @@ def _should_read(self, to_active: Literal[0, 1]) -> bool:
401396
# Data is read on the idle->active transition only when the phase is 1
402397
return self._phase == 1 - to_active
403398

404-
def write(
405-
self, buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None
406-
) -> None:
399+
def write(self, buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None) -> None:
407400
"""Write the data contained in buf. Requires the SPI being locked.
408401
If the buffer is empty, nothing happens.
409402
"""
@@ -436,7 +429,6 @@ def write(
436429
# to settle.
437430
start_time = self._wait(start_time)
438431

439-
# pylint: disable=too-many-branches
440432
def readinto(
441433
self,
442434
buffer: WriteableBuffer,
@@ -522,9 +514,7 @@ def write_readinto(
522514
for byte_position, _ in enumerate(buffer_out[out_start:out_end]):
523515
for bit_position in range(self._bits):
524516
bit_mask = 0x80 >> bit_position
525-
bit_value = (
526-
buffer_out[byte_position + out_start] & 0x80 >> bit_position
527-
)
517+
bit_value = buffer_out[byte_position + out_start] & 0x80 >> bit_position
528518
in_byte_position = byte_position + in_start
529519
# clock: idle, or has made an active->idle transition.
530520
if self._should_write(to_active=0):
@@ -555,8 +545,6 @@ def write_readinto(
555545
# clock: wait another half period for the last transition.
556546
start_time = self._wait(start_time)
557547

558-
# pylint: enable=too-many-branches
559-
560548
@property
561549
def frequency(self) -> int:
562550
"""Return the currently configured baud rate"""

‎docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7+
API Reference
8+
#############
9+
710
.. automodule:: adafruit_bitbangio
811
:members:

‎docs/conf.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# -*- coding: utf-8 -*-
2-
31
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
42
#
53
# SPDX-License-Identifier: MIT
64

5+
import datetime
76
import os
87
import sys
9-
import datetime
108

119
sys.path.insert(0, os.path.abspath(".."))
1210

@@ -46,9 +44,7 @@
4644
creation_year = "2020"
4745
current_year = str(datetime.datetime.now().year)
4846
year_duration = (
49-
current_year
50-
if current_year == creation_year
51-
else creation_year + " - " + current_year
47+
current_year if current_year == creation_year else creation_year + " - " + current_year
5248
)
5349
copyright = year_duration + " Melissa LeBlanc-Williams"
5450
author = "Melissa LeBlanc-Williams"

‎examples/bitbangio_simpletest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import board
1111
import digitalio
12+
1213
import adafruit_bitbangio as bitbangio
1314

1415
# Change these to the actual connections
@@ -29,4 +30,4 @@
2930
spi.readinto(data)
3031
spi.unlock()
3132
cs.value = 1
32-
print("Result is {}".format(data))
33+
print(f"Result is {data}")

‎ruff.toml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
target-version = "py38"
6+
line-length = 100
7+
8+
[lint]
9+
preview = true
10+
select = ["I", "PL", "UP"]
11+
12+
extend-select = [
13+
"D419", # empty-docstring
14+
"E501", # line-too-long
15+
"W291", # trailing-whitespace
16+
"PLC0414", # useless-import-alias
17+
"PLC2401", # non-ascii-name
18+
"PLC2801", # unnecessary-dunder-call
19+
"PLC3002", # unnecessary-direct-lambda-call
20+
"E999", # syntax-error
21+
"PLE0101", # return-in-init
22+
"F706", # return-outside-function
23+
"F704", # yield-outside-function
24+
"PLE0116", # continue-in-finally
25+
"PLE0117", # nonlocal-without-binding
26+
"PLE0241", # duplicate-bases
27+
"PLE0302", # unexpected-special-method-signature
28+
"PLE0604", # invalid-all-object
29+
"PLE0605", # invalid-all-format
30+
"PLE0643", # potential-index-error
31+
"PLE0704", # misplaced-bare-raise
32+
"PLE1141", # dict-iter-missing-items
33+
"PLE1142", # await-outside-async
34+
"PLE1205", # logging-too-many-args
35+
"PLE1206", # logging-too-few-args
36+
"PLE1307", # bad-string-format-type
37+
"PLE1310", # bad-str-strip-call
38+
"PLE1507", # invalid-envvar-value
39+
"PLE2502", # bidirectional-unicode
40+
"PLE2510", # invalid-character-backspace
41+
"PLE2512", # invalid-character-sub
42+
"PLE2513", # invalid-character-esc
43+
"PLE2514", # invalid-character-nul
44+
"PLE2515", # invalid-character-zero-width-space
45+
"PLR0124", # comparison-with-itself
46+
"PLR0202", # no-classmethod-decorator
47+
"PLR0203", # no-staticmethod-decorator
48+
"UP004", # useless-object-inheritance
49+
"PLR0206", # property-with-parameters
50+
"PLR0904", # too-many-public-methods
51+
"PLR0911", # too-many-return-statements
52+
"PLR0912", # too-many-branches
53+
"PLR0913", # too-many-arguments
54+
"PLR0914", # too-many-locals
55+
"PLR0915", # too-many-statements
56+
"PLR0916", # too-many-boolean-expressions
57+
"PLR1702", # too-many-nested-blocks
58+
"PLR1704", # redefined-argument-from-local
59+
"PLR1711", # useless-return
60+
"C416", # unnecessary-comprehension
61+
"PLR1733", # unnecessary-dict-index-lookup
62+
"PLR1736", # unnecessary-list-index-lookup
63+
64+
# ruff reports this rule is unstable
65+
#"PLR6301", # no-self-use
66+
67+
"PLW0108", # unnecessary-lambda
68+
"PLW0120", # useless-else-on-loop
69+
"PLW0127", # self-assigning-variable
70+
"PLW0129", # assert-on-string-literal
71+
"B033", # duplicate-value
72+
"PLW0131", # named-expr-without-context
73+
"PLW0245", # super-without-brackets
74+
"PLW0406", # import-self
75+
"PLW0602", # global-variable-not-assigned
76+
"PLW0603", # global-statement
77+
"PLW0604", # global-at-module-level
78+
79+
# fails on the try: import typing used by libraries
80+
#"F401", # unused-import
81+
82+
"F841", # unused-variable
83+
"E722", # bare-except
84+
"PLW0711", # binary-op-exception
85+
"PLW1501", # bad-open-mode
86+
"PLW1508", # invalid-envvar-default
87+
"PLW1509", # subprocess-popen-preexec-fn
88+
"PLW2101", # useless-with-lock
89+
"PLW3301", # nested-min-max
90+
]
91+
92+
ignore = [
93+
"PLR2004", # magic-value-comparison
94+
"UP030", # format literals
95+
"PLW1514", # unspecified-encoding
96+
"PLR0913", # too-many-arguments
97+
"PLR0915", # too-many-statements
98+
"PLR0917", # too-many-positional-arguments
99+
"PLR0904", # too-many-public-methods
100+
"PLR0912", # too-many-branches
101+
"PLR0916", # too-many-boolean-expressions
102+
"PLR6301", # could-be-static no-self-use
103+
"PLC0415", # import outside toplevel
104+
]
105+
106+
[format]
107+
line-ending = "lf"

‎tests/simulated_i2c.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
# SPDX-License-Identifier: MIT
33
"""Implementation of testable I2C devices."""
44

5-
from typing import Any, Callable, Optional, Union
65
import dataclasses
76
import enum
87
import signal
98
import types
10-
from typing_extensions import TypeAlias
9+
from typing import Any, Callable, Optional, Union
10+
1111
import simulator as sim
12+
from typing_extensions import TypeAlias
1213

13-
_SignalHandler: TypeAlias = Union[
14-
Callable[[int, Optional[types.FrameType]], Any], int, None
15-
]
14+
_SignalHandler: TypeAlias = Union[Callable[[int, Optional[types.FrameType]], Any], int, None]
1615

1716

1817
@enum.unique
@@ -123,7 +122,7 @@ def _on_clock_fall(self) -> None:
123122
self._maybe_clock_stretch()
124123

125124
# Return early unless we need to send data.
126-
if self._state not in (State.ACK, State.ACK_DONE, State.WRITE):
125+
if self._state not in {State.ACK, State.ACK_DONE, State.WRITE}:
127126
return
128127

129128
if self._state == State.ACK:
@@ -154,7 +153,7 @@ def _on_clock_fall(self) -> None:
154153
self._sent_bit_count += 1
155154

156155
def _on_clock_rise(self) -> None:
157-
if self._state not in (State.ADDRESS, State.READ, State.WAIT_ACK):
156+
if self._state not in {State.ADDRESS, State.READ, State.WAIT_ACK}:
158157
return
159158
bit_value = 1 if self._sda.net.level == sim.Level.HIGH else 0
160159
if self._state == State.WAIT_ACK:

‎tests/simulated_spi.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Implementation of testable SPI devices."""
44

55
import dataclasses
6+
67
import simulator as sim
78

89

@@ -37,11 +38,9 @@ def write_bit(self) -> None:
3738
"""Writes the next bit to the cipo net."""
3839
if self._bit_position >= len(self._data):
3940
# Just write a zero
40-
self._cipo.value(0) # pylint: disable=not-callable
41+
self._cipo.value(0)
4142
return
42-
self._cipo.value(
43-
int(self._data[self._bit_position]) # pylint: disable=not-callable
44-
)
43+
self._cipo.value(int(self._data[self._bit_position]))
4544
self._bit_position += 1
4645

4746
def _on_level_change(self, net: sim.Net) -> None:

‎tests/simulator.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# SPDX-License-Identifier: MIT
33
"""Simple logic level simulator to test I2C/SPI interactions."""
44

5-
from typing import Any, Callable, List, Literal, Optional, Sequence
65
import dataclasses
76
import enum
87
import functools
98
import time
9+
from typing import Any, Callable, List, Literal, Optional, Sequence
1010

1111
import digitalio
1212

@@ -89,7 +89,7 @@ def change_history(self) -> Sequence[Change]:
8989

9090
def write_vcd(self, path: str) -> None:
9191
"""Writes monitored nets to the provided path as a VCD file."""
92-
with open(path, "wt") as vcdfile:
92+
with open(path, "w") as vcdfile:
9393
vcdfile.write("$version pytest output $end\n")
9494
vcdfile.write("$timescale 1 us $end\n")
9595
vcdfile.write("$scope module top $end\n")
@@ -116,14 +116,14 @@ def write_vcd(self, path: str) -> None:
116116
class FakePin:
117117
"""Test double for a microcontroller pin used in tests."""
118118

119-
IN = Mode.IN # pylint: disable=invalid-name
119+
IN = Mode.IN
120120
OUT = Mode.OUT
121121
PULL_NONE = Pull.NONE
122122
PULL_UP = Pull.UP
123123
PULL_DOWN = Pull.DOWN
124124

125125
def __init__(self, pin_id: str, net: Optional["Net"] = None):
126-
self.id = pin_id # pylint: disable=invalid-name
126+
self.id = pin_id
127127
self.mode: Optional[Mode] = None
128128
self.pull: Optional[Pull] = None
129129
self.level: Level = Level.Z
@@ -157,11 +157,9 @@ def value(self, val: Optional[Literal[0, 1]] = None) -> Optional[Literal[0, 1]]:
157157
# Nothing is actively driving the line - we assume that during
158158
# testing, this is an error either in the test setup, or
159159
# something is asking for a value in an uninitialized state.
160-
raise ValueError(
161-
f"{self.id}: value read but nothing is driving the net."
162-
)
160+
raise ValueError(f"{self.id}: value read but nothing is driving the net.")
163161
return 1 if level == Level.HIGH else 0
164-
if val in (0, 1):
162+
if val in {0, 1}:
165163
if self.mode != Mode.OUT:
166164
raise ValueError(f"{self.id}: is not an output")
167165
nlevel = Level.HIGH if val else Level.LOW

‎tests/test_adafruit_bitbangio_i2c.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# SPDX-License-Identifier: MIT
33

44
from typing import Sequence
5+
56
import pytest
67
import simulated_i2c as si2c
78
import simulator as sim
8-
import adafruit_bitbangio
99

10+
import adafruit_bitbangio
1011

1112
_SCL_NET = "scl"
1213
_SDA_NET = "sda"
@@ -16,17 +17,11 @@ class TestBitbangI2C:
1617
def setup_method(self) -> None:
1718
sim.engine.reset()
1819
# Create nets, with a pullup by default.
19-
scl = sim.engine.create_net(
20-
_SCL_NET, monitor=True, default_level=sim.Level.HIGH
21-
)
22-
sda = sim.engine.create_net(
23-
_SDA_NET, monitor=True, default_level=sim.Level.HIGH
24-
)
25-
# pylint: disable=attribute-defined-outside-init
20+
scl = sim.engine.create_net(_SCL_NET, monitor=True, default_level=sim.Level.HIGH)
21+
sda = sim.engine.create_net(_SDA_NET, monitor=True, default_level=sim.Level.HIGH)
2622
self.scl_pin = sim.FakePin("scl_pin", scl)
2723
self.sda_pin = sim.FakePin("sda_pin", sda)
2824
self.i2cbus = si2c.I2CBus(scl=scl, sda=sda)
29-
# pylint: enable=attribute-defined-outside-init
3025

3126
@sim.stub
3227
@pytest.mark.parametrize("addresses", [[0x42, 0x43]])
@@ -59,9 +54,7 @@ def test_write(
5954
device = si2c.Constant("target", address=0x42, bus=self.i2cbus)
6055

6156
# Write data over the bus and verify the device received it.
62-
with adafruit_bitbangio.I2C(
63-
scl=self.scl_pin, sda=self.sda_pin, frequency=1000
64-
) as i2c:
57+
with adafruit_bitbangio.I2C(scl=self.scl_pin, sda=self.sda_pin, frequency=1000) as i2c:
6558
i2c.try_lock()
6659
i2c.writeto(address=0x42, buffer=data_array)
6760
i2c.unlock()
@@ -75,9 +68,7 @@ def test_write_no_ack(self) -> None:
7568
# attach a device that will ack the address, but not the data.
7669
si2c.Constant("target", address=0x42, bus=self.i2cbus, ack_data=False)
7770

78-
with adafruit_bitbangio.I2C(
79-
scl=self.scl_pin, sda=self.sda_pin, frequency=1000
80-
) as i2c:
71+
with adafruit_bitbangio.I2C(scl=self.scl_pin, sda=self.sda_pin, frequency=1000) as i2c:
8172
i2c.try_lock()
8273
with pytest.raises(RuntimeError) as info:
8374
i2c.writeto(address=0x42, buffer=b"\x42")
@@ -92,9 +83,7 @@ def test_write_clock_stretching(self, data: str) -> None:
9283
data_array = bytearray(int(data, 2).to_bytes(datalen, byteorder="big"))
9384

9485
# attach a device that does clock stretching, but not exceed our timeout.
95-
device = si2c.Constant(
96-
"target", address=0x42, bus=self.i2cbus, clock_stretch_sec=1
97-
)
86+
device = si2c.Constant("target", address=0x42, bus=self.i2cbus, clock_stretch_sec=1)
9887

9988
with adafruit_bitbangio.I2C(
10089
scl=self.scl_pin, sda=self.sda_pin, frequency=1000, timeout=2.0
@@ -132,9 +121,7 @@ def test_readfrom(self, count: int, data: str) -> None:
132121
si2c.Constant("target", address=0x42, bus=self.i2cbus, data_to_send=value)
133122

134123
# Confirm we were able to read back the data
135-
with adafruit_bitbangio.I2C(
136-
scl=self.scl_pin, sda=self.sda_pin, frequency=1000
137-
) as i2c:
124+
with adafruit_bitbangio.I2C(scl=self.scl_pin, sda=self.sda_pin, frequency=1000) as i2c:
138125
i2c.try_lock()
139126
i2c.readfrom_into(address=0x42, buffer=data_array)
140127
i2c.unlock()
@@ -168,18 +155,12 @@ def test_writeto_readfrom(self, send_data: str, expect_data: str) -> None:
168155
data_array = bytearray(1)
169156

170157
# attach a device that sends a constant byte of data.
171-
device = si2c.Constant(
172-
"target", address=0x42, bus=self.i2cbus, data_to_send=expect_value
173-
)
158+
device = si2c.Constant("target", address=0x42, bus=self.i2cbus, data_to_send=expect_value)
174159

175160
# Send the send_data, and check we got back expect_data
176-
with adafruit_bitbangio.I2C(
177-
scl=self.scl_pin, sda=self.sda_pin, frequency=1000
178-
) as i2c:
161+
with adafruit_bitbangio.I2C(scl=self.scl_pin, sda=self.sda_pin, frequency=1000) as i2c:
179162
i2c.try_lock()
180-
i2c.writeto_then_readfrom(
181-
address=0x42, buffer_out=send_array, buffer_in=data_array
182-
)
163+
i2c.writeto_then_readfrom(address=0x42, buffer_out=send_array, buffer_in=data_array)
183164
i2c.unlock()
184165

185166
# Useful to debug signals in pulseview.

‎tests/test_adafruit_bitbangio_spi.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# SPDX-License-Identifier: MIT
33

44
from typing import Literal, Sequence
5+
56
import pytest
67
import simulated_spi as sspi
78
import simulator as sim
8-
import adafruit_bitbangio
99

10+
import adafruit_bitbangio
1011

1112
_CLOCK_NET = "clock"
1213
_COPI_NET = "copi"
@@ -39,9 +40,7 @@ def _check_write(
3940
last_clock_state = sim.Level.Z
4041
last_copi_state = sim.Level.Z
4142
last_copi_us = 0
42-
idle, active = (
43-
(sim.Level.HIGH, sim.Level.LOW) if polarity else (sim.Level.LOW, sim.Level.HIGH)
44-
)
43+
idle, active = (sim.Level.HIGH, sim.Level.LOW) if polarity else (sim.Level.LOW, sim.Level.HIGH)
4544
bits_read = 0
4645
# We want data to be written at least this long before a read
4746
# transition.
@@ -88,18 +87,16 @@ def setup_method(self) -> None:
8887
copi = sim.engine.create_net(_COPI_NET, monitor=True)
8988
cipo = sim.engine.create_net(_CIPO_NET, monitor=True)
9089
enable = sim.engine.create_net(_ENABLE_NET, monitor=True)
91-
# pylint: disable=attribute-defined-outside-init
9290
self.clock_pin = sim.FakePin("clock_pin", clock)
9391
self.copi_pin = sim.FakePin("copi_pin", copi)
9492
self.cipo_pin = sim.FakePin("cipo_pin", cipo)
9593
self.enable_pin = sim.FakePin("enable_pin", enable)
9694
self.enable_pin.init(mode=sim.Mode.OUT)
9795
self.spibus = sspi.SpiBus(clock=clock, copi=copi, cipo=cipo, enable=enable)
98-
# pylint: enable=attribute-defined-outside-init
9996
self._enable_net(0)
10097

10198
def _enable_net(self, val: Literal[0, 1]) -> None:
102-
self.enable_pin.value(val) # pylint: disable=not-callable
99+
self.enable_pin.value(val)
103100

104101
@sim.stub
105102
@pytest.mark.parametrize("baud", [100])
@@ -139,9 +136,7 @@ def test_readinto(
139136
data_int = int(data, 2)
140137
data_array = bytearray(data_int.to_bytes(1, byteorder="big"))
141138
# attach a device that sends a constant.
142-
_ = sspi.Constant(
143-
data=data_array, bus=self.spibus, polarity=polarity, phase=phase
144-
)
139+
_ = sspi.Constant(data=data_array, bus=self.spibus, polarity=polarity, phase=phase)
145140

146141
# Read/write a byte of data
147142
with adafruit_bitbangio.SPI(
@@ -181,9 +176,7 @@ def test_write_readinto(
181176
nbytes = len(data) // 8
182177
data_array = bytearray(int(data, 2).to_bytes(nbytes, byteorder="big"))
183178
# attach a device that sends a constant.
184-
_ = sspi.Constant(
185-
data=data_array, bus=self.spibus, polarity=polarity, phase=phase
186-
)
179+
_ = sspi.Constant(data=data_array, bus=self.spibus, polarity=polarity, phase=phase)
187180

188181
# Read/write data array
189182
with adafruit_bitbangio.SPI(

0 commit comments

Comments
 (0)
Please sign in to comment.