Implement some of the suggested changes
This commit is contained in:
parent
74c2afdff2
commit
4dc7dd2238
@ -1,5 +1,4 @@
|
|||||||
import busio
|
import busio
|
||||||
import gc
|
|
||||||
from supervisor import ticks_ms
|
from supervisor import ticks_ms
|
||||||
|
|
||||||
import adafruit_displayio_ssd1306
|
import adafruit_displayio_ssd1306
|
||||||
@ -11,6 +10,7 @@ from kmk.extensions import Extension
|
|||||||
from kmk.handlers.stock import passthrough as handler_passthrough
|
from kmk.handlers.stock import passthrough as handler_passthrough
|
||||||
from kmk.keys import make_key
|
from kmk.keys import make_key
|
||||||
from kmk.kmktime import check_deadline
|
from kmk.kmktime import check_deadline
|
||||||
|
from kmk.utils import clamp
|
||||||
|
|
||||||
|
|
||||||
class OledEntryType:
|
class OledEntryType:
|
||||||
@ -42,28 +42,23 @@ class OledData:
|
|||||||
_x = 0
|
_x = 0
|
||||||
_x_anchor = 0.0
|
_x_anchor = 0.0
|
||||||
_y_anchor = 0.0
|
_y_anchor = 0.0
|
||||||
if x_anchor == 'left' or x_anchor == 'L':
|
if x_anchor == 'L':
|
||||||
_x_anchor = 0.0
|
_x_anchor = 0.0
|
||||||
elif x_anchor == 'middle' or x_anchor == 'M':
|
elif x_anchor == 'M':
|
||||||
_x_anchor = 0.5
|
_x_anchor = 0.5
|
||||||
elif x_anchor == 'right' or x_anchor == 'R':
|
elif x_anchor == 'R':
|
||||||
_x_anchor = 1.0
|
_x_anchor = 1.0
|
||||||
else:
|
else:
|
||||||
_x_anchor = 0.0
|
_x_anchor = 0.0
|
||||||
if y_anchor == 'top' or y_anchor == 'T':
|
if y_anchor == 'T':
|
||||||
_y_anchor = 0.0
|
_y_anchor = 0.0
|
||||||
elif y_anchor == 'middle' or y_anchor == 'M':
|
elif y_anchor == 'M':
|
||||||
_y_anchor = 0.5
|
_y_anchor = 0.5
|
||||||
elif y_anchor == 'bottom' or y_anchor == 'B':
|
elif y_anchor == 'B':
|
||||||
_y_anchor = 1.0
|
_y_anchor = 1.0
|
||||||
else:
|
else:
|
||||||
_y_anchor = 0.0
|
_y_anchor = 0.0
|
||||||
if (
|
if x_anchor != 'm' or x_anchor != 'r':
|
||||||
x_anchor != 'middle'
|
|
||||||
or x_anchor != 'right'
|
|
||||||
or x_anchor != 'm'
|
|
||||||
or x_anchor != 'r'
|
|
||||||
):
|
|
||||||
_x = x + 1
|
_x = x + 1
|
||||||
else:
|
else:
|
||||||
_x = x
|
_x = x
|
||||||
@ -112,7 +107,6 @@ class Oled(Extension):
|
|||||||
powersave_dim_time=10,
|
powersave_dim_time=10,
|
||||||
powersave_off_time=60,
|
powersave_off_time=60,
|
||||||
):
|
):
|
||||||
displayio.release_displays()
|
|
||||||
self._flip = flip
|
self._flip = flip
|
||||||
self._flip_left = flip_left
|
self._flip_left = flip_left
|
||||||
self._flip_right = flip_right
|
self._flip_right = flip_right
|
||||||
@ -124,8 +118,6 @@ class Oled(Extension):
|
|||||||
self._brightness = brightness
|
self._brightness = brightness
|
||||||
self._brightness_step = brightness_step
|
self._brightness_step = brightness_step
|
||||||
self._timer_start = ticks_ms()
|
self._timer_start = ticks_ms()
|
||||||
self._dark = False
|
|
||||||
self._go_dark = False
|
|
||||||
self._powersave = False
|
self._powersave = False
|
||||||
self._dim_time_ms = dim_time * 1000 if dim_time else None
|
self._dim_time_ms = dim_time * 1000 if dim_time else None
|
||||||
self._off_time_ms = off_time * 1000 if off_time else None
|
self._off_time_ms = off_time * 1000 if off_time else None
|
||||||
@ -138,47 +130,48 @@ class Oled(Extension):
|
|||||||
self._split = split
|
self._split = split
|
||||||
|
|
||||||
make_key(
|
make_key(
|
||||||
names=('OLED_BRI',), on_press=self._oled_bri, on_release=handler_passthrough
|
names=('OLED_BRI',),
|
||||||
|
on_press=self._oled_brightness_increase,
|
||||||
|
on_release=handler_passthrough,
|
||||||
)
|
)
|
||||||
make_key(
|
make_key(
|
||||||
names=('OLED_BRD',), on_press=self._oled_brd, on_release=handler_passthrough
|
names=('OLED_BRD',),
|
||||||
|
on_press=self._oled_brightness_decrease,
|
||||||
|
on_release=handler_passthrough,
|
||||||
)
|
)
|
||||||
|
|
||||||
def render_oled(self, layer):
|
def render_oled(self, layer):
|
||||||
splash = displayio.Group()
|
splash = displayio.Group()
|
||||||
|
|
||||||
for view in self._views:
|
for view in self._views:
|
||||||
if self._dark is False:
|
if view[4] == layer or view[4] is None:
|
||||||
if view[4] == layer or view[4] is None:
|
if view[5] == OledEntryType.TXT:
|
||||||
if view[5] == OledEntryType.TXT:
|
splash.append(
|
||||||
splash.append(
|
label.Label(
|
||||||
label.Label(
|
terminalio.FONT,
|
||||||
terminalio.FONT,
|
text=view[0],
|
||||||
text=view[0],
|
color=0xFFFFFF if not view[10] else 0x000000,
|
||||||
color=0xFFFFFF if not view[10] else 0x000000,
|
background_color=0x000000 if not view[10] else 0xFFFFFF,
|
||||||
background_color=0x000000 if not view[10] else 0xFFFFFF,
|
anchor_point=(view[6], view[7]),
|
||||||
anchor_point=(view[6], view[7]),
|
anchored_position=(view[1], view[2]),
|
||||||
anchored_position=(view[1], view[2]),
|
label_direction=view[8],
|
||||||
label_direction=view[8],
|
line_spacing=view[9],
|
||||||
line_spacing=view[9],
|
padding_left=1,
|
||||||
padding_left=1,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
elif view[5] == OledEntryType.IMG:
|
)
|
||||||
splash.append(
|
elif view[5] == OledEntryType.IMG:
|
||||||
displayio.TileGrid(
|
splash.append(
|
||||||
view[0],
|
displayio.TileGrid(
|
||||||
pixel_shader=view[0].pixel_shader,
|
view[0],
|
||||||
x=view[1],
|
pixel_shader=view[0].pixel_shader,
|
||||||
y=view[2],
|
x=view[1],
|
||||||
)
|
y=view[2],
|
||||||
)
|
)
|
||||||
gc.collect()
|
)
|
||||||
self._display.show(splash)
|
self._display.show(splash)
|
||||||
|
|
||||||
def updateOLED(self, sandbox):
|
def updateOLED(self, sandbox):
|
||||||
self.render_oled(sandbox.active_layers[0])
|
self.render_oled(sandbox.active_layers[0])
|
||||||
gc.collect()
|
|
||||||
|
|
||||||
def on_runtime_enable(self, sandbox):
|
def on_runtime_enable(self, sandbox):
|
||||||
return
|
return
|
||||||
@ -211,20 +204,16 @@ class Oled(Extension):
|
|||||||
brightness=self._brightness,
|
brightness=self._brightness,
|
||||||
)
|
)
|
||||||
self.render_oled(0)
|
self.render_oled(0)
|
||||||
return
|
|
||||||
|
|
||||||
def before_matrix_scan(self, sandbox):
|
def before_matrix_scan(self, sandbox):
|
||||||
self.dim()
|
self.dim()
|
||||||
if self._dark != self._go_dark or sandbox.active_layers[0] != self._prevLayers:
|
if sandbox.active_layers[0] != self._prevLayers:
|
||||||
self._dark = self._go_dark
|
|
||||||
self._prevLayers = sandbox.active_layers[0]
|
self._prevLayers = sandbox.active_layers[0]
|
||||||
self.updateOLED(sandbox)
|
self.updateOLED(sandbox)
|
||||||
return
|
|
||||||
|
|
||||||
def after_matrix_scan(self, keyboard):
|
def after_matrix_scan(self, keyboard):
|
||||||
if keyboard.matrix_update or keyboard.secondary_matrix_update:
|
if keyboard.matrix_update or keyboard.secondary_matrix_update:
|
||||||
self.timer_time_reset()
|
self.timer_time_reset()
|
||||||
return
|
|
||||||
|
|
||||||
def before_hid_send(self, sandbox):
|
def before_hid_send(self, sandbox):
|
||||||
return
|
return
|
||||||
@ -234,25 +223,19 @@ class Oled(Extension):
|
|||||||
|
|
||||||
def on_powersave_enable(self, sandbox):
|
def on_powersave_enable(self, sandbox):
|
||||||
self._powersave = True
|
self._powersave = True
|
||||||
return
|
|
||||||
|
|
||||||
def on_powersave_disable(self, sandbox):
|
def on_powersave_disable(self, sandbox):
|
||||||
self._powersave = False
|
self._powersave = False
|
||||||
return
|
|
||||||
|
|
||||||
def _oled_brightness_increase(self):
|
def _oled_brightness_increase(self):
|
||||||
self._display.brightness = (
|
self._display.brightness = clamp(
|
||||||
self._display.brightness + self._brightness_step
|
self._display.brightness + self._brightness_step, 0, 1
|
||||||
if self._display.brightness + self._brightness_step <= 1.0
|
|
||||||
else 1.0
|
|
||||||
)
|
)
|
||||||
self._brightness = self._display.brightness # Save current brightness
|
self._brightness = self._display.brightness # Save current brightness
|
||||||
|
|
||||||
def _oled_brd(self, *args, **kwargs):
|
def _oled_brightness_decrease(self):
|
||||||
self._display.brightness = (
|
self._display.brightness = clamp(
|
||||||
self._display.brightness - self._brightness_step
|
self._display.brightness - self._brightness_step, 0, 1
|
||||||
if self._display.brightness - self._brightness_step >= 0.1
|
|
||||||
else 0.1
|
|
||||||
)
|
)
|
||||||
self._brightness = self._display.brightness # Save current brightness
|
self._brightness = self._display.brightness # Save current brightness
|
||||||
|
|
||||||
@ -261,10 +244,12 @@ class Oled(Extension):
|
|||||||
|
|
||||||
def dim(self):
|
def dim(self):
|
||||||
if self._powersave:
|
if self._powersave:
|
||||||
|
|
||||||
if self._powersave_off_time_ms is not None and not check_deadline(
|
if self._powersave_off_time_ms is not None and not check_deadline(
|
||||||
ticks_ms(), self._timer_start, self._powersave_off_time_ms
|
ticks_ms(), self._timer_start, self._powersave_off_time_ms
|
||||||
):
|
):
|
||||||
self._go_dark = True
|
self._display.sleep()
|
||||||
|
|
||||||
elif self._powersave_dim_time_ms is not None and not check_deadline(
|
elif self._powersave_dim_time_ms is not None and not check_deadline(
|
||||||
ticks_ms(), self._timer_start, self._powersave_dim_time_ms
|
ticks_ms(), self._timer_start, self._powersave_dim_time_ms
|
||||||
):
|
):
|
||||||
@ -273,19 +258,23 @@ class Oled(Extension):
|
|||||||
if self._display.brightness > 0.5
|
if self._display.brightness > 0.5
|
||||||
else 0.1
|
else 0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._display.brightness = self._brightness
|
self._display.brightness = self._brightness
|
||||||
self._go_dark = False
|
self._display.wake()
|
||||||
|
|
||||||
elif self._off_time_ms is not None and not check_deadline(
|
elif self._off_time_ms is not None and not check_deadline(
|
||||||
ticks_ms(), self._timer_start, self._off_time_ms
|
ticks_ms(), self._timer_start, self._off_time_ms
|
||||||
):
|
):
|
||||||
self._go_dark = True
|
self._display.sleep()
|
||||||
|
|
||||||
elif self._dim_time_ms is not None and not check_deadline(
|
elif self._dim_time_ms is not None and not check_deadline(
|
||||||
ticks_ms(), self._timer_start, self._dim_time_ms
|
ticks_ms(), self._timer_start, self._dim_time_ms
|
||||||
):
|
):
|
||||||
self._display.brightness = (
|
self._display.brightness = (
|
||||||
self._display.brightness / 2 if self._display.brightness > 0.5 else 0.1
|
self._display.brightness / 2 if self._display.brightness > 0.5 else 0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._display.brightness = self._brightness
|
self._display.brightness = self._brightness
|
||||||
self._go_dark = False
|
self._display.wake()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user