kmk_firmware/kmk/extensions/peg_oledDisplay.py

134 lines
3.8 KiB
Python
Raw Normal View History

2022-04-14 23:35:54 -07:00
import busio
2022-04-15 00:06:50 -07:00
import gc
import adafruit_displayio_ssd1306
2022-04-14 23:35:54 -07:00
import displayio
import terminalio
from adafruit_display_text import label
2022-04-15 00:06:50 -07:00
2022-04-14 23:35:54 -07:00
from kmk.extensions import Extension
2022-04-15 00:06:50 -07:00
2022-04-14 23:35:54 -07:00
class oled(Extension):
def __init__(
self,
board,
views,
toDisplay: str = 'TXT',
2022-04-14 23:35:54 -07:00
oWidth: int = 128,
oHeight: int = 32,
2022-04-15 00:06:50 -07:00
flip: bool = False,
2022-04-14 23:35:54 -07:00
):
displayio.release_displays()
2022-04-15 00:06:50 -07:00
self._views = views
2022-04-14 23:35:54 -07:00
self._toDisplay = toDisplay
i2c = busio.I2C(board.SCL, board.SDA)
2022-04-15 00:06:50 -07:00
self.width = (oWidth,)
self.height = (oHeight,)
2022-04-14 23:35:54 -07:00
self.rotation = 180 if flip else 0
2022-04-15 00:06:50 -07:00
self._display = adafruit_displayio_ssd1306.SSD1306(
displayio.I2CDisplay(i2c, device_address=0x3C),
width=oWidth,
height=oHeight,
rotation=self.rotation,
)
2022-04-14 23:35:54 -07:00
self._prevLayers = 0
gc.collect()
2022-04-15 00:06:50 -07:00
def returnCurrectRenderText(self, layer, singleView):
2022-04-14 23:35:54 -07:00
# for now we only have static things and react to layers. But when we react to battery % and wpm we can handle the logic here
if singleView[0] == 'STATIC':
2022-04-15 00:06:50 -07:00
return singleView[1][0]
if singleView[0] == 'LAYER':
2022-04-15 00:06:50 -07:00
return singleView[1][layer]
2022-04-14 23:35:54 -07:00
def renderOledTextLayer(self, layer):
splash = displayio.Group()
self._display.show(splash)
splash.append(
2022-04-15 00:06:50 -07:00
label.Label(
terminalio.FONT,
text=self.returnCurrectRenderText(layer, self._views[0]),
color=0xFFFFFF,
x=0,
y=10,
)
)
2022-04-14 23:35:54 -07:00
splash.append(
2022-04-15 00:06:50 -07:00
label.Label(
terminalio.FONT,
text=self.returnCurrectRenderText(layer, self._views[1]),
color=0xFFFFFF,
x=64,
y=10,
)
)
2022-04-14 23:35:54 -07:00
splash.append(
2022-04-15 00:06:50 -07:00
label.Label(
terminalio.FONT,
text=self.returnCurrectRenderText(layer, self._views[2]),
color=0xFFFFFF,
x=0,
y=25,
)
)
2022-04-14 23:35:54 -07:00
splash.append(
2022-04-15 00:06:50 -07:00
label.Label(
terminalio.FONT,
text=self.returnCurrectRenderText(layer, self._views[3]),
color=0xFFFFFF,
x=64,
y=25,
)
)
2022-04-14 23:35:54 -07:00
gc.collect()
2022-04-15 00:06:50 -07:00
2022-04-14 23:35:54 -07:00
def renderOledImgLayer(self, layer):
splash = displayio.Group()
self._display.show(splash)
2022-04-15 00:06:50 -07:00
odb = displayio.OnDiskBitmap(
'/' + self.returnCurrectRenderText(layer, self._views[0])
)
2022-04-14 23:35:54 -07:00
image = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
splash.append(image)
gc.collect()
def updateOLED(self, sandbox):
if self._toDisplay == 'TXT':
2022-04-14 23:35:54 -07:00
self.renderOledTextLayer(sandbox.active_layers[0])
if self._toDisplay == 'IMG':
2022-04-14 23:35:54 -07:00
self.renderOledImgLayer(sandbox.active_layers[0])
gc.collect()
def on_runtime_enable(self, sandbox):
return
def on_runtime_disable(self, sandbox):
return
def during_bootup(self, sandbox):
self.renderOledImgLayer(0)
return
def before_matrix_scan(self, sandbox):
if sandbox.active_layers[0] != self._prevLayers:
self._prevLayers = sandbox.active_layers[0]
self.updateOLED(sandbox)
return
def after_matrix_scan(self, sandbox):
return
def before_hid_send(self, sandbox):
return
def after_hid_send(self, sandbox):
return
def on_powersave_enable(self, sandbox):
return
def on_powersave_disable(self, sandbox):
2022-04-15 00:06:50 -07:00
return