From a7c4f8b9e3a58b0da8dcacdc3e5414427f8b487f Mon Sep 17 00:00:00 2001 From: "E. A. Graham Jr" Date: Fri, 2 Aug 2024 11:53:43 -0700 Subject: [PATCH 1/7] A work-around for using byte-string as the data Fixes #137 -- a byte-string is not representable as a `list` for adding, so this just re-wraps the iterable in a "real" `list` --- .gitignore | 2 ++ i2cdisplaybus/__init__.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2c6ddfd..4fc1bd3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ bundles dist **/*.egg-info .vscode +venv +.venv diff --git a/i2cdisplaybus/__init__.py b/i2cdisplaybus/__init__.py index c8d6441..b004e54 100644 --- a/i2cdisplaybus/__init__.py +++ b/i2cdisplaybus/__init__.py @@ -87,7 +87,9 @@ def send(self, command: int, data: ReadableBuffer) -> None: done. """ self._begin_transaction() - self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + data)) + # re-wrap in case of byte-string + buffer = list(data) if isinstance(data, bytes) else data + self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + buffer)) self._end_transaction() def _send( From 5c0068c17cc0adc6ebef5bf2006c37ab6bf97f12 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 15 Nov 2024 10:35:30 -0600 Subject: [PATCH 2/7] add command to _send() --- i2cdisplaybus/__init__.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/i2cdisplaybus/__init__.py b/i2cdisplaybus/__init__.py index b004e54..f1b2d15 100644 --- a/i2cdisplaybus/__init__.py +++ b/i2cdisplaybus/__init__.py @@ -88,8 +88,9 @@ def send(self, command: int, data: ReadableBuffer) -> None: """ self._begin_transaction() # re-wrap in case of byte-string - buffer = list(data) if isinstance(data, bytes) else data - self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + buffer)) + #buffer = list(data) if isinstance(data, bytes) else data + #self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + buffer)) + self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, command) self._end_transaction() def _send( @@ -97,14 +98,18 @@ def _send( data_type: int, _chip_select: int, # Chip select behavior data: ReadableBuffer, + command: int, ): if data_type == DISPLAY_COMMAND: - n = len(data) + n = len(data) + 1 if n > 0: command_bytes = bytearray(n * 2) for i in range(n): command_bytes[2 * i] = 0x80 - command_bytes[2 * i + 1] = data[i] + if i > 0: + command_bytes[2 * i + 1] = data[i] + else: + command_bytes[2 * i + 1] = command try: self._i2c.writeto(self._dev_addr, buffer=command_bytes) @@ -115,9 +120,10 @@ def _send( ) from error raise error else: - data_bytes = bytearray(len(data) + 1) + data_bytes = bytearray(len(data) + 2) data_bytes[0] = 0x40 - data_bytes[1:] = data + data_bytes[1] = command + data_bytes[2:] = data try: self._i2c.writeto(self._dev_addr, buffer=data_bytes) except OSError as error: From a08abe17645ebb8916e3f4e3ec115025f784a840 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 15 Nov 2024 10:42:17 -0600 Subject: [PATCH 3/7] make command optional --- i2cdisplaybus/__init__.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/i2cdisplaybus/__init__.py b/i2cdisplaybus/__init__.py index f1b2d15..04ab42f 100644 --- a/i2cdisplaybus/__init__.py +++ b/i2cdisplaybus/__init__.py @@ -21,6 +21,8 @@ """ import time +from typing import Optional + import busio import digitalio from circuitpython_typing import ReadableBuffer @@ -98,18 +100,23 @@ def _send( data_type: int, _chip_select: int, # Chip select behavior data: ReadableBuffer, - command: int, + command: Optional[int] = None, ): if data_type == DISPLAY_COMMAND: - n = len(data) + 1 + n = len(data) + if command is not None: + n += 1 if n > 0: command_bytes = bytearray(n * 2) for i in range(n): command_bytes[2 * i] = 0x80 - if i > 0: - command_bytes[2 * i + 1] = data[i] + if command is not None: + if i > 0: + command_bytes[2 * i + 1] = data[i] + else: + command_bytes[2 * i + 1] = command else: - command_bytes[2 * i + 1] = command + command_bytes[2 * i + 1] = data[i] try: self._i2c.writeto(self._dev_addr, buffer=command_bytes) @@ -120,10 +127,16 @@ def _send( ) from error raise error else: - data_bytes = bytearray(len(data) + 2) + size = len(data) + 1 + if command is not None: + size += 1 + data_bytes = bytearray(size) data_bytes[0] = 0x40 - data_bytes[1] = command - data_bytes[2:] = data + if command is not None: + data_bytes[1] = command + data_bytes[2:] = data + else: + data_bytes[1:] = data try: self._i2c.writeto(self._dev_addr, buffer=data_bytes) except OSError as error: From b642f95603f46ad600ede2bad2de05ccc6675019 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 15 Nov 2024 10:46:51 -0600 Subject: [PATCH 4/7] remove commented out bits, format code --- i2cdisplaybus/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/i2cdisplaybus/__init__.py b/i2cdisplaybus/__init__.py index 04ab42f..344746c 100644 --- a/i2cdisplaybus/__init__.py +++ b/i2cdisplaybus/__init__.py @@ -89,9 +89,6 @@ def send(self, command: int, data: ReadableBuffer) -> None: done. """ self._begin_transaction() - # re-wrap in case of byte-string - #buffer = list(data) if isinstance(data, bytes) else data - #self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + buffer)) self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, command) self._end_transaction() From 682b50cc10086b42318c8718c737653014e9195b Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 15 Nov 2024 10:48:10 -0600 Subject: [PATCH 5/7] pylint ignore too-many-branches --- i2cdisplaybus/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/i2cdisplaybus/__init__.py b/i2cdisplaybus/__init__.py index 344746c..c963158 100644 --- a/i2cdisplaybus/__init__.py +++ b/i2cdisplaybus/__init__.py @@ -99,6 +99,7 @@ def _send( data: ReadableBuffer, command: Optional[int] = None, ): + # pylint: disable=too-many-branches if data_type == DISPLAY_COMMAND: n = len(data) if command is not None: From b7d8f5fd3a0f8094e9421cd224cc0d4031525684 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 15 Nov 2024 10:53:01 -0600 Subject: [PATCH 6/7] remove deprecated get_html_theme() --- docs/conf.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index a8628ab..df3ec85 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -102,17 +102,7 @@ # on_rtd = os.environ.get("READTHEDOCS", None) == "True" -if not on_rtd: # only import and set the theme if we're building docs locally - try: - import sphinx_rtd_theme - - html_theme = "sphinx_rtd_theme" - html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."] - except: - html_theme = "default" - html_theme_path = ["."] -else: - html_theme_path = ["."] +html_theme = "sphinx_rtd_theme" # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, From 3fbd61659affda5aac037f997b19b9fdeec66c29 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 15 Nov 2024 11:02:33 -0600 Subject: [PATCH 7/7] remove .gitignore changes --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4fc1bd3..2c6ddfd 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,3 @@ bundles dist **/*.egg-info .vscode -venv -.venv