From 4e956ce600b51031d117bc0a396e90c5403ba5da Mon Sep 17 00:00:00 2001 From: Anomalocaridid <29845794+Anomalocaridid@users.noreply.github.com> Date: Thu, 18 Jan 2024 15:15:06 -0500 Subject: [PATCH 1/5] split display.py into files for each backend --- .../{display.py => display/__init__.py} | 93 ------------------- kmk/extensions/display/builtin.py | 24 +++++ kmk/extensions/display/sh1106.py | 50 ++++++++++ kmk/extensions/display/ssd1306.py | 31 +++++++ 4 files changed, 105 insertions(+), 93 deletions(-) rename kmk/extensions/{display.py => display/__init__.py} (77%) create mode 100644 kmk/extensions/display/builtin.py create mode 100644 kmk/extensions/display/sh1106.py create mode 100644 kmk/extensions/display/ssd1306.py diff --git a/kmk/extensions/display.py b/kmk/extensions/display/__init__.py similarity index 77% rename from kmk/extensions/display.py rename to kmk/extensions/display/__init__.py index 0f185ecff..847fd0e9a 100644 --- a/kmk/extensions/display.py +++ b/kmk/extensions/display/__init__.py @@ -1,4 +1,3 @@ -import busio from supervisor import ticks_ms import displayio @@ -12,8 +11,6 @@ from kmk.modules.split import Split, SplitSide from kmk.utils import clamp -displayio.release_displays() - class TextEntry: def __init__( @@ -110,96 +107,6 @@ def root_group(self, group): self.display.root_group = group -# Intended for displays with drivers built into CircuitPython -# that can be used directly without manual initialization -class BuiltInDisplay(DisplayBackend): - def __init__(self, display=None, sleep_command=None, wake_command=None): - self.display = display - self.sleep_command = sleep_command - self.wake_command = wake_command - self.is_awake = True - - def during_bootup(self, width, height, rotation): - self.display.rotation = rotation - return self.display - - def deinit(self): - return - - def sleep(self): - self.display.bus.send(self.sleep_command, b'') - - def wake(self): - self.display.bus.send(self.wake_command, b'') - - -class SSD1306(DisplayBackend): - def __init__(self, i2c=None, sda=None, scl=None, device_address=0x3C): - self.device_address = device_address - # i2c initialization - self.i2c = i2c - if self.i2c is None: - self.i2c = busio.I2C(scl, sda) - - def during_bootup(self, width, height, rotation): - import adafruit_displayio_ssd1306 - - self.display = adafruit_displayio_ssd1306.SSD1306( - displayio.I2CDisplay(self.i2c, device_address=self.device_address), - width=width, - height=height, - rotation=rotation, - ) - - return self.display - - def deinit(self): - self.i2c.deinit() - - -class SH1106(DisplayBackend): - def __init__( - self, - spi=None, - sck=None, - mosi=None, - command=None, - chip_select=None, - reset=None, - baudrate=1000000, - ): - displayio.release_displays() - self.command = command - self.chip_select = chip_select - self.reset = reset - self.baudrate = baudrate - # spi initialization - self.spi = spi - if self.spi is None: - self.spi = busio.SPI(sck, mosi) - - def during_bootup(self, width, height, rotation): - import adafruit_displayio_sh1106 - - self.display = adafruit_displayio_sh1106.SH1106( - displayio.FourWire( - self.spi, - command=self.command, - chip_select=self.chip_select, - reset=self.reset, - baudrate=self.baudrate, - ), - width=width, - height=height, - rotation=rotation, - ) - - return self.display - - def deinit(self): - self.spi.deinit() - - class Display(Extension): def __init__( self, diff --git a/kmk/extensions/display/builtin.py b/kmk/extensions/display/builtin.py new file mode 100644 index 000000000..1bbfc5239 --- /dev/null +++ b/kmk/extensions/display/builtin.py @@ -0,0 +1,24 @@ +from kmk.extensions.display import DisplayBackend + + +# Intended for displays with drivers built into CircuitPython +# that can be used directly without manual initialization +class BuiltInDisplay(DisplayBackend): + def __init__(self, display=None, sleep_command=None, wake_command=None): + self.display = display + self.sleep_command = sleep_command + self.wake_command = wake_command + self.is_awake = True + + def during_bootup(self, width, height, rotation): + self.display.rotation = rotation + return self.display + + def deinit(self): + return + + def sleep(self): + self.display.bus.send(self.sleep_command, b'') + + def wake(self): + self.display.bus.send(self.wake_command, b'') diff --git a/kmk/extensions/display/sh1106.py b/kmk/extensions/display/sh1106.py new file mode 100644 index 000000000..ca18efe10 --- /dev/null +++ b/kmk/extensions/display/sh1106.py @@ -0,0 +1,50 @@ +import busio + +import adafruit_displayio_sh1106 # Display-specific library +import displayio + +from kmk.extensions.display import DisplayBackend + +# Required to initialize this display +displayio.release_displays() + + +class SH1106(DisplayBackend): + def __init__( + self, + spi=None, + sck=None, + mosi=None, + command=None, + chip_select=None, + reset=None, + baudrate=1000000, + ): + displayio.release_displays() + self.command = command + self.chip_select = chip_select + self.reset = reset + self.baudrate = baudrate + # spi initialization + self.spi = spi + if self.spi is None: + self.spi = busio.SPI(sck, mosi) + + def during_bootup(self, width, height, rotation): + self.display = adafruit_displayio_sh1106.SH1106( + displayio.FourWire( + self.spi, + command=self.command, + chip_select=self.chip_select, + reset=self.reset, + baudrate=self.baudrate, + ), + width=width, + height=height, + rotation=rotation, + ) + + return self.display + + def deinit(self): + self.spi.deinit() diff --git a/kmk/extensions/display/ssd1306.py b/kmk/extensions/display/ssd1306.py new file mode 100644 index 000000000..8b12bcf75 --- /dev/null +++ b/kmk/extensions/display/ssd1306.py @@ -0,0 +1,31 @@ +import busio + +import adafruit_displayio_ssd1306 # Display-specific library +import displayio + +from kmk.extensions.display import DisplayBackend + +# Required to initialize this display +displayio.release_displays() + + +class SSD1306(DisplayBackend): + def __init__(self, i2c=None, sda=None, scl=None, device_address=0x3C): + self.device_address = device_address + # i2c initialization + self.i2c = i2c + if self.i2c is None: + self.i2c = busio.I2C(scl, sda) + + def during_bootup(self, width, height, rotation): + self.display = adafruit_displayio_ssd1306.SSD1306( + displayio.I2CDisplay(self.i2c, device_address=self.device_address), + width=width, + height=height, + rotation=rotation, + ) + + return self.display + + def deinit(self): + self.i2c.deinit() From d13b9d178782667f269484271420528bb4313efa Mon Sep 17 00:00:00 2001 From: Anomalocaridid <29845794+Anomalocaridid@users.noreply.github.com> Date: Thu, 18 Jan 2024 15:43:49 -0500 Subject: [PATCH 2/5] update Display.md --- docs/en/Display.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/en/Display.md b/docs/en/Display.md index 8f6fe8aa0..9d7df32bb 100644 --- a/docs/en/Display.md +++ b/docs/en/Display.md @@ -31,8 +31,10 @@ Here's how you may initialize the extension. Note that this includes examples of import board import busio +from kmk.extensions.display import Display, TextEntry, ImageEntry + # For SSD1306 -from kmk.extensions.display import Display, SSD1306, TextEntry, ImageEntry +from kmk.extensions.display.ssd1306 import SSD1306 # Replace SCL and SDA according to your hardware configuration. i2c_bus = busio.I2C(board.GP_SCL, board.GP_SDA) @@ -45,7 +47,7 @@ driver = SSD1306( ) # For SH1106 -from kmk.extensions.display import Display, SH1106, TextEntry, ImageEntry +from kmk.extensions.display.sh1106 import SH1106 # Replace SCK and MOSI according to your hardware configuration. spi_bus = busio.SPI(board.GP_SCK, board.GP_MOSI) @@ -60,7 +62,8 @@ driver = SH1106( ) # For displays initialized by CircuitPython by default -from kmk.extensions.display import Display, BuiltInDisplay, TextEntry, ImageEntry +# IMPORTANT: breaks if a display backend from kmk.extensions.display is also in use +from kmk.extensions.display.builtin import BuiltInDisplay # Replace display, sleep_command, and wake_command according to your hardware configuration. driver = BuiltInDisplay( From 812dc88f0d2e8269039043cb08698459b4c6d4bc Mon Sep 17 00:00:00 2001 From: Anomalocaridid <29845794+Anomalocaridid@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:50:01 -0500 Subject: [PATCH 3/5] remove unecessary call to displayio.release_displays() --- kmk/extensions/display/sh1106.py | 1 - 1 file changed, 1 deletion(-) diff --git a/kmk/extensions/display/sh1106.py b/kmk/extensions/display/sh1106.py index ca18efe10..4e1d70c35 100644 --- a/kmk/extensions/display/sh1106.py +++ b/kmk/extensions/display/sh1106.py @@ -20,7 +20,6 @@ def __init__( reset=None, baudrate=1000000, ): - displayio.release_displays() self.command = command self.chip_select = chip_select self.reset = reset From 1ed3f83201c57fd55998315a7d147676c46abe10 Mon Sep 17 00:00:00 2001 From: Anomalocaridid <29845794+Anomalocaridid@users.noreply.github.com> Date: Sun, 28 Jan 2024 14:13:53 -0500 Subject: [PATCH 4/5] rename DisplayBackend to DisplayBase --- kmk/extensions/display/__init__.py | 2 +- kmk/extensions/display/builtin.py | 4 ++-- kmk/extensions/display/sh1106.py | 4 ++-- kmk/extensions/display/ssd1306.py | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/kmk/extensions/display/__init__.py b/kmk/extensions/display/__init__.py index 847fd0e9a..a1f19f190 100644 --- a/kmk/extensions/display/__init__.py +++ b/kmk/extensions/display/__init__.py @@ -73,7 +73,7 @@ def __init__(self, x=0, y=0, image='', layer=None, side=None): self.side = SplitSide.RIGHT -class DisplayBackend: +class DisplayBase: def __init__(self): raise NotImplementedError diff --git a/kmk/extensions/display/builtin.py b/kmk/extensions/display/builtin.py index 1bbfc5239..4163ab788 100644 --- a/kmk/extensions/display/builtin.py +++ b/kmk/extensions/display/builtin.py @@ -1,9 +1,9 @@ -from kmk.extensions.display import DisplayBackend +from kmk.extensions.display import DisplayBase # Intended for displays with drivers built into CircuitPython # that can be used directly without manual initialization -class BuiltInDisplay(DisplayBackend): +class BuiltInDisplay(DisplayBase): def __init__(self, display=None, sleep_command=None, wake_command=None): self.display = display self.sleep_command = sleep_command diff --git a/kmk/extensions/display/sh1106.py b/kmk/extensions/display/sh1106.py index 4e1d70c35..38efb02e7 100644 --- a/kmk/extensions/display/sh1106.py +++ b/kmk/extensions/display/sh1106.py @@ -3,13 +3,13 @@ import adafruit_displayio_sh1106 # Display-specific library import displayio -from kmk.extensions.display import DisplayBackend +from kmk.extensions.display import DisplayBase # Required to initialize this display displayio.release_displays() -class SH1106(DisplayBackend): +class SH1106(DisplayBase): def __init__( self, spi=None, diff --git a/kmk/extensions/display/ssd1306.py b/kmk/extensions/display/ssd1306.py index 8b12bcf75..41948ec68 100644 --- a/kmk/extensions/display/ssd1306.py +++ b/kmk/extensions/display/ssd1306.py @@ -3,13 +3,13 @@ import adafruit_displayio_ssd1306 # Display-specific library import displayio -from kmk.extensions.display import DisplayBackend +from kmk.extensions.display import DisplayBase # Required to initialize this display displayio.release_displays() -class SSD1306(DisplayBackend): +class SSD1306(DisplayBase): def __init__(self, i2c=None, sda=None, scl=None, device_address=0x3C): self.device_address = device_address # i2c initialization From 67c1e5d4d5d3f3ce5722b5b827299a266d12ea2a Mon Sep 17 00:00:00 2001 From: Anomalocaridid <29845794+Anomalocaridid@users.noreply.github.com> Date: Sun, 28 Jan 2024 14:14:59 -0500 Subject: [PATCH 5/5] use relative imports in display extension --- kmk/extensions/display/builtin.py | 2 +- kmk/extensions/display/sh1106.py | 2 +- kmk/extensions/display/ssd1306.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kmk/extensions/display/builtin.py b/kmk/extensions/display/builtin.py index 4163ab788..9820f561d 100644 --- a/kmk/extensions/display/builtin.py +++ b/kmk/extensions/display/builtin.py @@ -1,4 +1,4 @@ -from kmk.extensions.display import DisplayBase +from . import DisplayBase # Intended for displays with drivers built into CircuitPython diff --git a/kmk/extensions/display/sh1106.py b/kmk/extensions/display/sh1106.py index 38efb02e7..cf93a457c 100644 --- a/kmk/extensions/display/sh1106.py +++ b/kmk/extensions/display/sh1106.py @@ -3,7 +3,7 @@ import adafruit_displayio_sh1106 # Display-specific library import displayio -from kmk.extensions.display import DisplayBase +from . import DisplayBase # Required to initialize this display displayio.release_displays() diff --git a/kmk/extensions/display/ssd1306.py b/kmk/extensions/display/ssd1306.py index 41948ec68..a6b2a0152 100644 --- a/kmk/extensions/display/ssd1306.py +++ b/kmk/extensions/display/ssd1306.py @@ -3,7 +3,7 @@ import adafruit_displayio_ssd1306 # Display-specific library import displayio -from kmk.extensions.display import DisplayBase +from . import DisplayBase # Required to initialize this display displayio.release_displays()