g13gui: g13: Add a bitwidgets backend for displaying to the G13

This commit is contained in:
June Tate-Gans 2021-05-02 15:23:07 -05:00
parent 186f64c33f
commit 493206d552
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,57 @@
import PIL
import struct
from io import BytesIO
from g13gui.bitwidgets.displaydevice import DisplayDevice
class DisplayMetrics(object):
WIDTH_PIXELS = 160
HEIGHT_PIXELS = 48
def ImageToLPBM(image):
"""Simple function to convert a PIL Image into LPBM format."""
i = PIL.PyAccess.new(image, readonly=True)
bio = BytesIO()
maxBytes = (DisplayMetrics.WIDTH_PIXELS *
DisplayMetrics.HEIGHT_PIXELS // 8)
row = 0
col = 0
for byteNum in range(0, maxBytes):
b = int()
if row == 40:
maxSubrow = 3
else:
maxSubrow = 8
for subrow in range(0, maxSubrow):
b |= i[col, row + subrow] << subrow
bio.write(struct.pack('<B', b))
col += 1
if (col % 160) == 0:
col = 0
row += 8
return bio.getvalue()
class G13DisplayDevice(DisplayDevice):
"""A bitwidget display device for the G13 LCD"""
def __init__(self, manager):
self._manager = manager
@property
def dimensions(self):
return (DisplayMetrics.WIDTH_PIXELS, DisplayMetrics.HEIGHT_PIXELS)
def update(self, image):
lpbm = ImageToLPBM(image)
self._manager.setLCDBuffer(lpbm)

View File

@ -0,0 +1,34 @@
import unittest
import time
from g13gui.model.prefs import Preferences
from g13gui.bitwidgets.display import Display
from g13gui.bitwidgets.screen import Screen
from g13gui.bitwidgets.label import Label
from g13gui.g13.displaydevice import G13DisplayDevice
from g13gui.g13.manager import Manager
class DisplayDeviceTests(unittest.TestCase):
def setUp(self):
self.prefs = Preferences()
self.manager = Manager(self.prefs)
self.manager.start()
time.sleep(0.25)
self.dd = G13DisplayDevice(self.manager)
self.d = Display(self.dd)
self.s = Screen(self.d)
def tearDown(self):
time.sleep(1)
self.manager.shutdown()
def testDisplay(self):
label = Label(0, 0, 'Hello, world!')
label.show()
self.s.addChild(label)
self.s.nextFrame()
if __name__ == '__main__':
unittest.main()