Skip to content

Commit

Permalink
Reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-hull committed Nov 5, 2020
1 parent dfcbef0 commit 452969d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
14 changes: 3 additions & 11 deletions luma/oled/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from luma.core.framebuffer import diff_to_previous, full_frame
from luma.core.bitmap_font import embedded_fonts
import luma.oled.const

from luma.oled.device.framebuffer_mixin import __framebuffer_mixin

__all__ = ["ssd1306", "ssd1309", "ssd1322", "ssd1362", "ssd1322_nhd", "ssd1325", "ssd1327", "ssd1331", "ssd1351", "sh1106", "ws0010", "winstar_weh"]

Expand Down Expand Up @@ -756,7 +756,7 @@ def _set_position(self, top, right, bottom, left):
0x75, top, bottom - 1) # set row addr


class ws0010(parallel_device, character):
class ws0010(parallel_device, character, __framebuffer_mixin):
"""
Serial interface to a monochrome Winstar WS0010 OLED display. This
interface will work with most ws0010 powered devices including the weg010016.
Expand Down Expand Up @@ -838,15 +838,7 @@ def __init__(self, serial_interface=None, width=100, height=16, undefined='_', f
const=luma.oled.const.ws0010, **kwargs):
super(ws0010, self).__init__(const, serial_interface, exec_time=exec_time, **kwargs)
self.capabilities(width, height, rotate)
if isinstance(framebuffer, str):
import warnings
warnings.warn(
"Specifying framebuffer as a string is now deprecated; Supply an instance of class full_frame() or diff_to_previous() instead",
RuntimeWarning
)
self.framebuffer = getattr(luma.core.framebuffer, framebuffer)()
else:
self.framebuffer = framebuffer
self.init_framebuffer(framebuffer)
self.font = font if font is not None else embedded_fonts(self._const.FONTDATA, selected_font=selected_font)
self._undefined = undefined
self.device = self
Expand Down
13 changes: 3 additions & 10 deletions luma/oled/device/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,16 @@
import luma.core.error
import luma.core.framebuffer
import luma.oled.const
from luma.oled.device.framebuffer_mixin import __framebuffer_mixin


class color_device(device):
class color_device(device, __framebuffer_mixin):
__metaclass__ = ABCMeta

def __init__(self, serial_interface, width, height, rotate, framebuffer, **kwargs):
super(color_device, self).__init__(luma.oled.const.common, serial_interface)
self.capabilities(width, height, rotate, mode="RGB")
if isinstance(framebuffer, str):
import warnings
warnings.warn(
"Specifying framebuffer as a string is now deprecated; Supply an instance of class full_frame() or diff_to_previous() instead",
RuntimeWarning
)
self.framebuffer = getattr(luma.core.framebuffer, framebuffer)()
else:
self.framebuffer = framebuffer
self.init_framebuffer(framebuffer)

if (width, height) not in self._supported_dimensions():
raise luma.core.error.DeviceDisplayModeError(
Expand Down
24 changes: 24 additions & 0 deletions luma/oled/device/framebuffer_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import luma.core.framebuffer


class __framebuffer_mixin(object):
"""
Helper class for initializing the framebuffer. Its only purpose is to
log a deprecation warning if a string framebuffer is specified.
.. note::
Specifying the framebuffer as a string will be removed at the next
major release, and hence this mixin will become redundant and will
also be removed at that point.
"""

def init_framebuffer(self, framebuffer):
if isinstance(framebuffer, str):
import warnings
warnings.warn(
"Specifying framebuffer as a string is now deprecated; Supply an instance of class full_frame() or diff_to_previous() instead",
DeprecationWarning
)
self.framebuffer = getattr(luma.core.framebuffer, framebuffer)()
else:
self.framebuffer = framebuffer
14 changes: 4 additions & 10 deletions luma/oled/device/greyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@
from luma.core.device import device
import luma.core.error
import luma.oled.const
from luma.oled.device.framebuffer_mixin import __framebuffer_mixin


class greyscale_device(device):
class greyscale_device(device, __framebuffer_mixin):
__metaclass__ = ABCMeta

def __init__(self, const, serial_interface, width, height, rotate, mode,
framebuffer, nibble_order, **kwargs):
super(greyscale_device, self).__init__(const, serial_interface)
self.capabilities(width, height, rotate, mode)
if isinstance(framebuffer, str):
import warnings
warnings.warn(
"Specifying framebuffer as a string is now deprecated; Supply an instance of class full_frame() or diff_to_previous() instead",
RuntimeWarning
)
self.framebuffer = getattr(luma.core.framebuffer, framebuffer)()
else:
self.framebuffer = framebuffer
self.init_framebuffer(framebuffer)

self._populate = self._render_mono if mode == "1" else self._render_greyscale
self._nibble_order = nibble_order

Expand Down

0 comments on commit 452969d

Please sign in to comment.