-
Notifications
You must be signed in to change notification settings - Fork 41
Provide proxies for I2C and SPI #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# SPDX-FileCopyrightText: 2022 Randall Bohn (dexter) | ||
# | ||
# SPDX-License-Identifier: MIT | ||
"Provides the protocol objects for I2C and SPI" | ||
|
||
from busio import I2C, SPI | ||
from digitalio import DigitalInOut | ||
|
||
|
||
class I2C_Impl: | ||
"Protocol implementation for the I2C bus." | ||
|
||
def __init__(self, i2c: I2C, address: int) -> None: | ||
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel | ||
i2c_device, | ||
) | ||
|
||
self._i2c = i2c_device.I2CDevice(i2c, address) | ||
|
||
def read_register(self, register: int, length: int) -> bytearray: | ||
"Read from the device register." | ||
with self._i2c as i2c: | ||
i2c.write(bytes([register & 0xFF])) | ||
result = bytearray(length) | ||
i2c.readinto(result) | ||
return result | ||
|
||
def write_register_byte(self, register: int, value: int) -> None: | ||
"Write to the device register." | ||
with self._i2c as i2c: | ||
i2c.write(bytes([register & 0xFF, value & 0xFF])) | ||
|
||
|
||
class SPI_Impl: | ||
"Protocol implemenation for the SPI bus." | ||
|
||
def __init__(self, spi: SPI, cs: DigitalInOut, baudrate: int = 100000) -> None: | ||
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel | ||
spi_device, | ||
) | ||
|
||
self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate) | ||
|
||
def read_register(self, register: int, length: int) -> bytearray: | ||
"Read from the device register." | ||
register = (register | 0x80) & 0xFF # Read single, bit 7 high. | ||
with self._spi as spi: | ||
spi.write(bytearray([register])) # pylint: disable=no-member | ||
result = bytearray(length) | ||
spi.readinto(result) # pylint: disable=no-member | ||
# print("$%02X => %s" % (register, [hex(i) for i in result])) | ||
return result | ||
|
||
def write_register_byte(self, register: int, value: int) -> None: | ||
"Write value to the device register." | ||
register &= 0x7F # Write, bit 7 low. | ||
with self._spi as spi: | ||
spi.write(bytes([register, value & 0xFF])) # pylint: disable=no-member |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.