Skip to content

Commit 9507787

Browse files
committed
fixes keypad driver, a better way
1 parent 1e70559 commit 9507787

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

display_configs/LilyGo-TDeck/keyboard_s3.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,77 @@
22

33
import lvgl as lv # NOQA
44
import keypad_framework
5+
from micropython import const # NOQA
6+
import lcd_utils
7+
8+
_LILYGO_KB_BRIGHTNESS_CMD = const(0x01)
9+
_LILYGO_KB_ALT_B_BRIGHTNESS_CMD = const(0x02)
510

611

712
class Keyboard(keypad_framework.KeypadDriver):
813
def __init__(self, device, debug=False): # NOQA
914
self._device = device
1015
self._debug = debug
16+
self._brightness = 0
17+
self._brightness_default = 127
18+
1119
super().__init__()
1220

21+
def set_default_brioghtness(self, value):
22+
value = lcd_utils.remap(float(value), 0.0, 100.0, 30.0, 255.0)
23+
value = int(round(value))
24+
25+
# clamp value at 29 - 255
26+
if value <= 30:
27+
value = 0
28+
29+
if value > 255:
30+
value = 255
31+
32+
self._brightness_default = value
33+
34+
value = bytearray([value])
35+
self._device.write_mem(_LILYGO_KB_ALT_B_BRIGHTNESS_CMD, value)
36+
37+
def get_brightness_default(self):
38+
value = self._brightness_default
39+
if value == 0:
40+
value = 30
41+
42+
value = lcd_utils.remap(float(value), 30.0, 255.0, 0.0, 100.0)
43+
return round(value, 1)
44+
45+
def set_brightness(self, value):
46+
value = lcd_utils.remap(float(value), 0.0, 100.0, 30.0, 255.0)
47+
value = int(round(value))
48+
49+
# clamp value at 29 - 255
50+
if value <= 30:
51+
value = 0
52+
53+
if value > 255:
54+
value = 255
55+
56+
self._brightness = value
57+
value = bytearray([value])
58+
self._device.write_mem(_LILYGO_KB_BRIGHTNESS_CMD, value)
59+
60+
def get_brightness(self):
61+
value = self._brightness
62+
if value == 0:
63+
value = 30
64+
65+
value = lcd_utils.remap(float(value), 30.0, 255.0, 0.0, 100.0)
66+
return round(value, 1)
67+
1368
def _get_key(self):
1469
# lv.KEY.ESC = 0x1B
1570
# lv.KEY.DEL = 0x7F
1671
# lv.KEY.NEXT = 0x09
1772
# lv.KEY.PREV = 0x0B
1873
# lv.KEY.HOME = 0x02
1974
# lv.KEY.END = 0x03
20-
key = bytearray(self._device.read(1))[0]
75+
key = bytearray(self._device.read_mem(0x00, num_bytes=1))[0]
2176
if key == 0x00: # no key
2277
return None
2378
elif key == 0x08: # backspace

0 commit comments

Comments
 (0)