-
Notifications
You must be signed in to change notification settings - Fork 25
register accessor layer and SPI support #59
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 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dc6d647
register accessor layer and SPI support
FoamyGuy 4532b05
repo and version meta vars
FoamyGuy 77983d4
I2C accessor implementation, repo and version meta vars
FoamyGuy a5e9569
Merge remote-tracking branch 'foamyguy/accessor_layer' into accessor_…
FoamyGuy 9bdc1f8
docstrings. implement bool vs int logic in i2c write
FoamyGuy 5196696
move read current value out of accessor into Bit(s). Support multibyt…
FoamyGuy c850754
pass address to read/write Accessor functions. Fix start/end indexes …
FoamyGuy 5039f29
SPI start/end fix for multibyte address. big endian multibyte address…
FoamyGuy b42f50e
split address and data buffers. Address buffer inside Accessor, data …
FoamyGuy 262e989
only use full_buffer for i2c.write(). Split SPI write into two instea…
FoamyGuy f4861a7
add protocol details to both Accessor docstrings. Remove buffer lengt…
FoamyGuy 4b553b4
add i2c comms details
FoamyGuy 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
docstrings. implement bool vs int logic in i2c write
- Loading branch information
commit 9bdc1f857c220bee0e78f112a88ae259e315a311
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,24 @@ | |
| __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git" | ||
|
|
||
|
|
||
| try: | ||
| from typing import Union | ||
|
|
||
| from adafruit_bus_device.i2c_device import I2CDevice | ||
| from adafruit_bus_device.spi_device import SPIDevice | ||
| except ImportError: | ||
| pass | ||
|
|
||
|
|
||
| class SPIRegisterAccessor: | ||
| def __init__(self, spi_device): | ||
| """ | ||
| RegisterAccessor class for SPI bus transport. Provides interface to read/write | ||
| registers over SPI. | ||
|
|
||
| :param SPIDevice spi_device: The SPI bus device to communicate over. | ||
| """ | ||
|
|
||
| def __init__(self, spi_device: SPIDevice): | ||
| self.spi_device = spi_device | ||
|
|
||
| def _shift_rw_cmd_bit_into_address_byte(self, buffer, bit_value): | ||
|
|
@@ -29,13 +45,42 @@ def _shift_rw_cmd_bit_into_address_byte(self, buffer, bit_value): | |
| # Set the MSB to the desired bit value | ||
| buffer[0] = cleared_byte | (bit_value << 7) | ||
|
|
||
| def read_register(self, buffer): | ||
| def read_register(self, buffer: bytearray): | ||
tannewt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Read register value over SPIDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
| Must be long enough to be read all data send by the device. Data will be | ||
| read into indexes 1-N. | ||
| :return: None | ||
| """ | ||
| with self.spi_device as spi: | ||
| self._shift_rw_cmd_bit_into_address_byte(buffer, 1) | ||
| spi.write(buffer, end=1) | ||
| spi.readinto(buffer, start=1) | ||
|
|
||
| def write_register(self, buffer, value, lsb_first, bit_mask, lowest_bit, byte=None): | ||
| def write_register( | ||
| self, | ||
| buffer: bytearray, | ||
| value: Union[int, bool], | ||
| lsb_first: bool, | ||
| bit_mask: int, | ||
| lowest_bit: int, | ||
| byte=None, | ||
| ): | ||
tannewt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Write register value over SPIDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
| Must be long enough to be read all data send by the device for specified register. | ||
| :param Union[int, bool] value: Value to write to the register. | ||
| :param bool lsb_first: Whether the least significant byte is first in multibyte registers. | ||
| :param int bit_mask: Bitmask of the bits where value will be written within the full data | ||
| of the register. | ||
| :param int lowest_bit: Index of the lowest bit in the full data of the register. | ||
| :param byte: Byte index within the full data of a multibyte register. | ||
| :return: None | ||
| """ | ||
| # read current register data | ||
| with self.spi_device as spi: | ||
| self._shift_rw_cmd_bit_into_address_byte(buffer, 1) | ||
|
|
@@ -73,31 +118,67 @@ def write_register(self, buffer, value, lsb_first, bit_mask, lowest_bit, byte=No | |
|
|
||
|
|
||
| class I2CRegisterAccessor: | ||
| def __init__(self, i2c_device): | ||
| """ | ||
| RegisterAccessor class for I2C bus transport. Provides interface to read/write | ||
| registers over I2C | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, explain the protocol over I2C that this supports.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also talk about the relationship between address and data. |
||
| :param I2CDevice i2c_device: I2C device to communicate over | ||
| """ | ||
|
|
||
| def __init__(self, i2c_device: I2CDevice): | ||
| self.i2c_device = i2c_device | ||
| pass | ||
|
|
||
| def read_register(self, buffer): | ||
| """ | ||
| Read register value over I2CDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
| Must be long enough to be read all data send by the device. Data will be | ||
| read into indexes 1-N. | ||
| :return: None | ||
| """ | ||
| with self.i2c_device as i2c: | ||
| i2c.write_then_readinto(buffer, buffer, out_end=1, in_start=1) | ||
FoamyGuy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def write_register(self, buffer, value, lsb_first, bit_mask, lowest_bit, byte=None): | ||
| """ | ||
| Write register value over I2CDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
FoamyGuy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Must be long enough to be read all data send by the device for specified register. | ||
| :param Union[int, bool] value: Value to write to the register. | ||
| :param bool lsb_first: Whether the least significant byte is first in multibyte registers. | ||
| :param int bit_mask: Bitmask of the bits where value will be written within the full data | ||
| of the register. | ||
| :param int lowest_bit: Index of the lowest bit in the full data of the register. | ||
| :param byte: Byte index within the buffer where boolean value will be written into. | ||
| :return: None | ||
| """ | ||
|
|
||
| value <<= lowest_bit # shift the value over to the right spot | ||
| with self.i2c_device as i2c: | ||
| i2c.write_then_readinto(buffer, buffer, out_end=1, in_start=1) | ||
|
|
||
| reg = 0 | ||
| order = range(len(buffer) - 1, 0, -1) | ||
| if not lsb_first: | ||
| order = range(1, len(buffer)) | ||
| for i in order: | ||
| reg = (reg << 8) | buffer[i] | ||
| # print("old reg: ", hex(reg)) | ||
| reg &= ~bit_mask # mask off the bits we're about to change | ||
| reg |= value # then or in our new value | ||
| # print("new reg: ", hex(reg)) | ||
| for i in reversed(order): | ||
| buffer[i] = reg & 0xFF | ||
| reg >>= 8 | ||
| if isinstance(value, int): | ||
| reg = 0 | ||
| order = range(len(buffer) - 1, 0, -1) | ||
| if not lsb_first: | ||
| order = range(1, len(buffer)) | ||
| for i in order: | ||
| reg = (reg << 8) | buffer[i] | ||
| # print("old reg: ", hex(reg)) | ||
| reg &= ~bit_mask # mask off the bits we're about to change | ||
| reg |= value # then or in our new value | ||
| # print("new reg: ", hex(reg)) | ||
| for i in reversed(order): | ||
| buffer[i] = reg & 0xFF | ||
| reg >>= 8 | ||
|
|
||
| elif isinstance(value, bool): | ||
| if value: | ||
| buffer[byte] |= bit_mask | ||
| else: | ||
| buffer[byte] &= ~bit_mask | ||
|
|
||
| i2c.write(buffer) | ||
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.