Skip to content

Commit 9cc6085

Browse files
authored
Merge pull request #3 from makermelissa/master
Added Extended SPI class
2 parents 88d085c + e83da21 commit 9cc6085

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

adafruit_extended_bus.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
import threading
3535

3636
from os import path
37-
from busio import I2C
37+
from busio import I2C, SPI
3838
from adafruit_blinka.microcontroller.generic_linux.i2c import I2C as _I2C
39+
from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
3940

4041
__version__ = "0.0.0-auto.0"
4142
__repo__ = "https://github.com/adafruit/Adafruit_Python_Extended_Bus.git"
@@ -66,3 +67,39 @@ def init(self, bus_id, frequency):
6667
self._lock = threading.RLock()
6768

6869
# pylint: enable=arguments-differ
70+
71+
72+
# pylint: disable=too-few-public-methods
73+
class ExtendedSPI(SPI):
74+
"""Extended SPI is a busio extension that allows creating a compatible
75+
SPI object using the Bus ID number. The bus ID is the numbers at the end
76+
of /dev/spidev#.# and you can find which SPI devices you have by typing
77+
`ls /dev/spi*`"""
78+
79+
# pylint: disable=invalid-name, redefined-builtin
80+
class Pin:
81+
"""Fake Pin class"""
82+
83+
def __init__(self, id):
84+
self.id = id
85+
86+
# pylint: enable=invalid-name, redefined-builtin
87+
88+
# pylint: disable=super-init-not-called
89+
def __init__(self, bus_id, chip_select):
90+
self.deinit()
91+
92+
# Check if the file /dev/i2c-{bus_id} exists and error if not
93+
if not path.exists("/dev/spidev{}.{}".format(bus_id, chip_select)):
94+
raise ValueError(
95+
"No device found for /dev/spidev{}.{}".format(bus_id, chip_select)
96+
)
97+
98+
self._spi = _SPI((bus_id, chip_select))
99+
# Pins aren't used in Linux, so we just use fake pins
100+
self._pins = (self.Pin(0), self.Pin(0), self.Pin(0))
101+
102+
# pylint: enable=super-init-not-called
103+
104+
105+
# pylint: enable=too-few-public-methods

0 commit comments

Comments
 (0)