urwid 2.6.0.post0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of urwid might be problematic. Click here for more details.
- urwid/__init__.py +333 -0
- urwid/canvas.py +1413 -0
- urwid/command_map.py +137 -0
- urwid/container.py +59 -0
- urwid/decoration.py +65 -0
- urwid/display/__init__.py +97 -0
- urwid/display/_posix_raw_display.py +413 -0
- urwid/display/_raw_display_base.py +914 -0
- urwid/display/_web.css +12 -0
- urwid/display/_web.js +462 -0
- urwid/display/_win32.py +171 -0
- urwid/display/_win32_raw_display.py +269 -0
- urwid/display/common.py +1219 -0
- urwid/display/curses.py +690 -0
- urwid/display/escape.py +624 -0
- urwid/display/html_fragment.py +251 -0
- urwid/display/lcd.py +518 -0
- urwid/display/raw.py +37 -0
- urwid/display/web.py +636 -0
- urwid/event_loop/__init__.py +55 -0
- urwid/event_loop/abstract_loop.py +175 -0
- urwid/event_loop/asyncio_loop.py +231 -0
- urwid/event_loop/glib_loop.py +294 -0
- urwid/event_loop/main_loop.py +721 -0
- urwid/event_loop/select_loop.py +230 -0
- urwid/event_loop/tornado_loop.py +206 -0
- urwid/event_loop/trio_loop.py +302 -0
- urwid/event_loop/twisted_loop.py +269 -0
- urwid/event_loop/zmq_loop.py +275 -0
- urwid/font.py +695 -0
- urwid/graphics.py +96 -0
- urwid/highlight.css +19 -0
- urwid/listbox.py +1899 -0
- urwid/monitored_list.py +522 -0
- urwid/numedit.py +376 -0
- urwid/signals.py +330 -0
- urwid/split_repr.py +130 -0
- urwid/str_util.py +358 -0
- urwid/text_layout.py +632 -0
- urwid/treetools.py +515 -0
- urwid/util.py +557 -0
- urwid/version.py +16 -0
- urwid/vterm.py +1806 -0
- urwid/widget/__init__.py +181 -0
- urwid/widget/attr_map.py +161 -0
- urwid/widget/attr_wrap.py +140 -0
- urwid/widget/bar_graph.py +649 -0
- urwid/widget/big_text.py +77 -0
- urwid/widget/box_adapter.py +126 -0
- urwid/widget/columns.py +1145 -0
- urwid/widget/constants.py +574 -0
- urwid/widget/container.py +227 -0
- urwid/widget/divider.py +110 -0
- urwid/widget/edit.py +718 -0
- urwid/widget/filler.py +403 -0
- urwid/widget/frame.py +539 -0
- urwid/widget/grid_flow.py +539 -0
- urwid/widget/line_box.py +194 -0
- urwid/widget/overlay.py +829 -0
- urwid/widget/padding.py +597 -0
- urwid/widget/pile.py +971 -0
- urwid/widget/popup.py +170 -0
- urwid/widget/progress_bar.py +141 -0
- urwid/widget/scrollable.py +597 -0
- urwid/widget/solid_fill.py +44 -0
- urwid/widget/text.py +354 -0
- urwid/widget/widget.py +852 -0
- urwid/widget/widget_decoration.py +166 -0
- urwid/widget/wimp.py +792 -0
- urwid/wimp.py +23 -0
- urwid-2.6.0.post0.dist-info/COPYING +504 -0
- urwid-2.6.0.post0.dist-info/METADATA +332 -0
- urwid-2.6.0.post0.dist-info/RECORD +75 -0
- urwid-2.6.0.post0.dist-info/WHEEL +5 -0
- urwid-2.6.0.post0.dist-info/top_level.txt +1 -0
urwid/display/lcd.py
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
# Urwid LCD display module
|
|
2
|
+
# Copyright (C) 2010 Ian Ward
|
|
3
|
+
#
|
|
4
|
+
# This library is free software; you can redistribute it and/or
|
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
|
6
|
+
# License as published by the Free Software Foundation; either
|
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+
# Lesser General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
|
15
|
+
# License along with this library; if not, write to the Free Software
|
|
16
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17
|
+
#
|
|
18
|
+
# Urwid web site: https://urwid.org/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import abc
|
|
24
|
+
import time
|
|
25
|
+
import typing
|
|
26
|
+
|
|
27
|
+
from .common import BaseScreen
|
|
28
|
+
|
|
29
|
+
if typing.TYPE_CHECKING:
|
|
30
|
+
from collections.abc import Iterable, Sequence
|
|
31
|
+
|
|
32
|
+
from typing_extensions import Literal
|
|
33
|
+
|
|
34
|
+
from urwid import Canvas
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class LCDScreen(BaseScreen, abc.ABC):
|
|
38
|
+
"""Base class for LCD-based screens."""
|
|
39
|
+
|
|
40
|
+
DISPLAY_SIZE: tuple[int, int]
|
|
41
|
+
|
|
42
|
+
def set_terminal_properties(self, colors=None, bright_is_bold=None, has_underline=None):
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
def set_input_timeouts(self, *args):
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
def reset_default_terminal_palette(self, *args):
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
def get_cols_rows(self):
|
|
52
|
+
return self.DISPLAY_SIZE
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class CFLCDScreen(LCDScreen):
|
|
56
|
+
"""
|
|
57
|
+
Common methods for Crystal Fonts LCD displays
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
KEYS: typing.ClassVar[list[str | None]] = [
|
|
61
|
+
None, # no key with code 0
|
|
62
|
+
"up_press",
|
|
63
|
+
"down_press",
|
|
64
|
+
"left_press",
|
|
65
|
+
"right_press",
|
|
66
|
+
"enter_press",
|
|
67
|
+
"exit_press",
|
|
68
|
+
"up_release",
|
|
69
|
+
"down_release",
|
|
70
|
+
"left_release",
|
|
71
|
+
"right_release",
|
|
72
|
+
"enter_release",
|
|
73
|
+
"exit_release",
|
|
74
|
+
"ul_press",
|
|
75
|
+
"ur_press",
|
|
76
|
+
"ll_press",
|
|
77
|
+
"lr_press",
|
|
78
|
+
"ul_release",
|
|
79
|
+
"ur_release",
|
|
80
|
+
"ll_release",
|
|
81
|
+
"lr_release",
|
|
82
|
+
]
|
|
83
|
+
CMD_PING = 0
|
|
84
|
+
CMD_VERSION = 1
|
|
85
|
+
CMD_CLEAR = 6
|
|
86
|
+
CMD_CGRAM = 9
|
|
87
|
+
CMD_CURSOR_POSITION = 11 # data = [col, row]
|
|
88
|
+
CMD_CURSOR_STYLE = 12 # data = [style (0-4)]
|
|
89
|
+
CMD_LCD_CONTRAST = 13 # data = [contrast (0-255)]
|
|
90
|
+
CMD_BACKLIGHT = 14 # data = [power (0-100)]
|
|
91
|
+
CMD_LCD_DATA = 31 # data = [col, row] + text
|
|
92
|
+
CMD_GPO = 34 # data = [pin(0-12), value(0-100)]
|
|
93
|
+
|
|
94
|
+
# sent from device
|
|
95
|
+
CMD_KEY_ACTIVITY = 0x80
|
|
96
|
+
CMD_ACK = 0x40 # in high two bits ie. & 0xc0
|
|
97
|
+
|
|
98
|
+
CURSOR_NONE = 0
|
|
99
|
+
CURSOR_BLINKING_BLOCK = 1
|
|
100
|
+
CURSOR_UNDERSCORE = 2
|
|
101
|
+
CURSOR_BLINKING_BLOCK_UNDERSCORE = 3
|
|
102
|
+
CURSOR_INVERTING_BLINKING_BLOCK = 4
|
|
103
|
+
|
|
104
|
+
MAX_PACKET_DATA_LENGTH = 22
|
|
105
|
+
|
|
106
|
+
colors = 1
|
|
107
|
+
has_underline = False
|
|
108
|
+
|
|
109
|
+
def __init__(self, device_path: str, baud: int):
|
|
110
|
+
"""
|
|
111
|
+
device_path -- eg. '/dev/ttyUSB0'
|
|
112
|
+
baud -- baud rate
|
|
113
|
+
"""
|
|
114
|
+
super().__init__()
|
|
115
|
+
self.device_path = device_path
|
|
116
|
+
from serial import Serial
|
|
117
|
+
|
|
118
|
+
self._device = Serial(device_path, baud, timeout=0)
|
|
119
|
+
self._unprocessed = bytearray()
|
|
120
|
+
|
|
121
|
+
@classmethod
|
|
122
|
+
def get_crc(cls, buf: bytearray) -> int:
|
|
123
|
+
# This seed makes the output of this shift based algorithm match
|
|
124
|
+
# the table based algorithm. The center 16 bits of the 32-bit
|
|
125
|
+
# "newCRC" are used for the CRC. The MSB of the lower byte is used
|
|
126
|
+
# to see what bit was shifted out of the center 16 bit CRC
|
|
127
|
+
# accumulator ("carry flag analog");
|
|
128
|
+
new_crc = 0x00F32100
|
|
129
|
+
for byte in buf:
|
|
130
|
+
# Push this byte's bits through a software
|
|
131
|
+
# implementation of a hardware shift & xor.
|
|
132
|
+
for bit_count in range(8):
|
|
133
|
+
# Shift the CRC accumulator
|
|
134
|
+
new_crc >>= 1
|
|
135
|
+
# The new MSB of the CRC accumulator comes
|
|
136
|
+
# from the LSB of the current data byte.
|
|
137
|
+
if byte & (0x01 << bit_count):
|
|
138
|
+
new_crc |= 0x00800000
|
|
139
|
+
# If the low bit of the current CRC accumulator was set
|
|
140
|
+
# before the shift, then we need to XOR the accumulator
|
|
141
|
+
# with the polynomial (center 16 bits of 0x00840800)
|
|
142
|
+
if new_crc & 0x00000080:
|
|
143
|
+
new_crc ^= 0x00840800
|
|
144
|
+
# All the data has been done. Do 16 more bits of 0 data.
|
|
145
|
+
for _bit_count in range(16):
|
|
146
|
+
# Shift the CRC accumulator
|
|
147
|
+
new_crc >>= 1
|
|
148
|
+
# If the low bit of the current CRC accumulator was set
|
|
149
|
+
# before the shift we need to XOR the accumulator with
|
|
150
|
+
# 0x00840800.
|
|
151
|
+
if new_crc & 0x00000080:
|
|
152
|
+
new_crc ^= 0x00840800
|
|
153
|
+
# Return the center 16 bits, making this CRC match the one's
|
|
154
|
+
# complement that is sent in the packet.
|
|
155
|
+
return ((~new_crc) >> 8) & 0xFFFF
|
|
156
|
+
|
|
157
|
+
def _send_packet(self, command, data):
|
|
158
|
+
"""
|
|
159
|
+
low-level packet sending.
|
|
160
|
+
Following the protocol requires waiting for ack packet between
|
|
161
|
+
sending each packet to the device.
|
|
162
|
+
"""
|
|
163
|
+
buf = chr(command) + chr(len(data)) + data
|
|
164
|
+
crc = self.get_crc(buf)
|
|
165
|
+
buf = buf + chr(crc & 0xFF) + chr(crc >> 8)
|
|
166
|
+
self._device.write(buf)
|
|
167
|
+
|
|
168
|
+
def _read_packet(self) -> tuple[int, bytearray] | None:
|
|
169
|
+
"""
|
|
170
|
+
low-level packet reading.
|
|
171
|
+
returns (command/report code, data) or None
|
|
172
|
+
|
|
173
|
+
This method stored data read and tries to resync when bad data
|
|
174
|
+
is received.
|
|
175
|
+
"""
|
|
176
|
+
# pull in any new data available
|
|
177
|
+
self._unprocessed += self._device.read()
|
|
178
|
+
while True:
|
|
179
|
+
try:
|
|
180
|
+
command, data, unprocessed = self._parse_data(self._unprocessed)
|
|
181
|
+
self._unprocessed = unprocessed
|
|
182
|
+
except self.MoreDataRequired: # noqa: PERF203
|
|
183
|
+
return None
|
|
184
|
+
except self.InvalidPacket:
|
|
185
|
+
# throw out a byte and try to parse again
|
|
186
|
+
self._unprocessed = self._unprocessed[1:]
|
|
187
|
+
else:
|
|
188
|
+
return command, data
|
|
189
|
+
|
|
190
|
+
class InvalidPacket(Exception):
|
|
191
|
+
pass
|
|
192
|
+
|
|
193
|
+
class MoreDataRequired(Exception):
|
|
194
|
+
pass
|
|
195
|
+
|
|
196
|
+
@classmethod
|
|
197
|
+
def _parse_data(cls, data: bytearray) -> tuple[int, bytearray, bytearray]:
|
|
198
|
+
"""
|
|
199
|
+
Try to read a packet from the start of data, returning
|
|
200
|
+
(command/report code, packet_data, remaining_data)
|
|
201
|
+
or raising InvalidPacket or MoreDataRequired
|
|
202
|
+
"""
|
|
203
|
+
if len(data) < 2:
|
|
204
|
+
raise cls.MoreDataRequired
|
|
205
|
+
|
|
206
|
+
command: int = data[0]
|
|
207
|
+
packet_len: int = data[1]
|
|
208
|
+
|
|
209
|
+
if packet_len > cls.MAX_PACKET_DATA_LENGTH:
|
|
210
|
+
raise cls.InvalidPacket("length value too large")
|
|
211
|
+
|
|
212
|
+
if len(data) < packet_len + 4:
|
|
213
|
+
raise cls.MoreDataRequired
|
|
214
|
+
|
|
215
|
+
data_end = 2 + packet_len
|
|
216
|
+
crc = cls.get_crc(data[:data_end])
|
|
217
|
+
pcrc = ord(data[data_end : data_end + 1]) + (ord(data[data_end + 1 : data_end + 2]) << 8)
|
|
218
|
+
if crc != pcrc:
|
|
219
|
+
raise cls.InvalidPacket("CRC doesn't match")
|
|
220
|
+
return command, data[2:data_end], data[data_end + 2 :]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class KeyRepeatSimulator:
|
|
224
|
+
"""
|
|
225
|
+
Provide simulated repeat key events when given press and
|
|
226
|
+
release events.
|
|
227
|
+
|
|
228
|
+
If two or more keys are pressed disable repeating until all
|
|
229
|
+
keys are released.
|
|
230
|
+
"""
|
|
231
|
+
|
|
232
|
+
def __init__(self, repeat_delay: float, repeat_next: float) -> None:
|
|
233
|
+
"""
|
|
234
|
+
repeat_delay -- seconds to wait before starting to repeat keys
|
|
235
|
+
repeat_next -- time between each repeated key
|
|
236
|
+
"""
|
|
237
|
+
self.repeat_delay = repeat_delay
|
|
238
|
+
self.repeat_next = repeat_next
|
|
239
|
+
self.pressed: dict[str, float] = {}
|
|
240
|
+
self.multiple_pressed = False
|
|
241
|
+
|
|
242
|
+
def press(self, key: str) -> None:
|
|
243
|
+
if self.pressed:
|
|
244
|
+
self.multiple_pressed = True
|
|
245
|
+
self.pressed[key] = time.time()
|
|
246
|
+
|
|
247
|
+
def release(self, key: str) -> None:
|
|
248
|
+
if key not in self.pressed:
|
|
249
|
+
return # ignore extra release events
|
|
250
|
+
del self.pressed[key]
|
|
251
|
+
if not self.pressed:
|
|
252
|
+
self.multiple_pressed = False
|
|
253
|
+
|
|
254
|
+
def next_event(self) -> tuple[float, str] | None:
|
|
255
|
+
"""
|
|
256
|
+
Return (remaining, key) where remaining is the number of seconds
|
|
257
|
+
(float) until the key repeat event should be sent, or None if no
|
|
258
|
+
events are pending.
|
|
259
|
+
"""
|
|
260
|
+
if len(self.pressed) != 1 or self.multiple_pressed:
|
|
261
|
+
return None
|
|
262
|
+
for key, val in self.pressed.items():
|
|
263
|
+
return max(0.0, val + self.repeat_delay - time.time()), key
|
|
264
|
+
return None
|
|
265
|
+
|
|
266
|
+
def sent_event(self) -> None:
|
|
267
|
+
"""
|
|
268
|
+
Cakk this method when you have sent a key repeat event so the
|
|
269
|
+
timer will be reset for the next event
|
|
270
|
+
"""
|
|
271
|
+
if len(self.pressed) != 1:
|
|
272
|
+
return # ignore event that shouldn't have been sent
|
|
273
|
+
for key in self.pressed:
|
|
274
|
+
self.pressed[key] = time.time() - self.repeat_delay + self.repeat_next
|
|
275
|
+
return
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class CF635Screen(CFLCDScreen):
|
|
279
|
+
"""
|
|
280
|
+
Crystal Fontz 635 display
|
|
281
|
+
|
|
282
|
+
20x4 character display + cursor
|
|
283
|
+
no foreground/background colors or settings supported
|
|
284
|
+
|
|
285
|
+
see CGROM for list of close unicode matches to characters available
|
|
286
|
+
|
|
287
|
+
6 button input
|
|
288
|
+
up, down, left, right, enter (check mark), exit (cross)
|
|
289
|
+
"""
|
|
290
|
+
|
|
291
|
+
DISPLAY_SIZE = (20, 4)
|
|
292
|
+
|
|
293
|
+
# ① through ⑧ are programmable CGRAM (chars 0-7, repeated at 8-15)
|
|
294
|
+
# double arrows (⇑⇓) appear as double arrowheads (chars 18, 19)
|
|
295
|
+
# ⑴ resembles a bell
|
|
296
|
+
# ⑵ resembles a filled-in "Y"
|
|
297
|
+
# ⑶ is the letters "Pt" together
|
|
298
|
+
# partial blocks (▇▆▄▃▁) are actually shorter versions of (▉▋▌▍▏)
|
|
299
|
+
# both groups are intended to draw horizontal bars with pixel
|
|
300
|
+
# precision, use ▇*[▆▄▃▁]? for a thin bar or ▉*[▋▌▍▏]? for a thick bar
|
|
301
|
+
CGROM = (
|
|
302
|
+
"①②③④⑤⑥⑦⑧①②③④⑤⑥⑦⑧"
|
|
303
|
+
"►◄⇑⇓«»↖↗↙↘▲▼↲^ˇ█"
|
|
304
|
+
" !\"#¤%&'()*+,-./"
|
|
305
|
+
"0123456789:;<=>?"
|
|
306
|
+
"¡ABCDEFGHIJKLMNO"
|
|
307
|
+
"PQRSTUVWXYZÄÖÑܧ"
|
|
308
|
+
"¿abcdefghijklmno"
|
|
309
|
+
"pqrstuvwxyzäöñüà"
|
|
310
|
+
"⁰¹²³⁴⁵⁶⁷⁸⁹½¼±≥≤μ"
|
|
311
|
+
"♪♫⑴♥♦⑵⌜⌟“”()αɛδ∞"
|
|
312
|
+
"@£$¥èéùìòÇᴾØøʳÅå"
|
|
313
|
+
"⌂¢ΦτλΩπΨΣθΞ♈ÆæßÉ"
|
|
314
|
+
"ΓΛΠϒ_ÈÊêçğŞşİι~◊"
|
|
315
|
+
"▇▆▄▃▁ƒ▉▋▌▍▏⑶◽▪↑→"
|
|
316
|
+
"↓←ÁÍÓÚÝáíóúýÔôŮů"
|
|
317
|
+
r"ČĔŘŠŽčĕřšž[\]{|}"
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
cursor_style = CFLCDScreen.CURSOR_INVERTING_BLINKING_BLOCK
|
|
321
|
+
|
|
322
|
+
def __init__(
|
|
323
|
+
self,
|
|
324
|
+
device_path: str,
|
|
325
|
+
baud: int = 115200,
|
|
326
|
+
repeat_delay: float = 0.5,
|
|
327
|
+
repeat_next: float = 0.125,
|
|
328
|
+
key_map: Iterable[str] = ("up", "down", "left", "right", "enter", "esc"),
|
|
329
|
+
):
|
|
330
|
+
"""
|
|
331
|
+
device_path -- eg. '/dev/ttyUSB0'
|
|
332
|
+
baud -- baud rate
|
|
333
|
+
repeat_delay -- seconds to wait before starting to repeat keys
|
|
334
|
+
repeat_next -- time between each repeated key
|
|
335
|
+
key_map -- the keys to send for this device's buttons
|
|
336
|
+
"""
|
|
337
|
+
super().__init__(device_path, baud)
|
|
338
|
+
|
|
339
|
+
self.repeat_delay = repeat_delay
|
|
340
|
+
self.repeat_next = repeat_next
|
|
341
|
+
self.key_repeat = KeyRepeatSimulator(repeat_delay, repeat_next)
|
|
342
|
+
self.key_map = tuple(key_map)
|
|
343
|
+
|
|
344
|
+
self._last_command = None
|
|
345
|
+
self._last_command_time = 0
|
|
346
|
+
self._command_queue = []
|
|
347
|
+
self._screen_buf = None
|
|
348
|
+
self._previous_canvas = None
|
|
349
|
+
self._update_cursor = False
|
|
350
|
+
|
|
351
|
+
def get_input_descriptors(self):
|
|
352
|
+
"""
|
|
353
|
+
return the fd from our serial device so we get called
|
|
354
|
+
on input and responses
|
|
355
|
+
"""
|
|
356
|
+
return [self._device.fd]
|
|
357
|
+
|
|
358
|
+
def get_input_nonblocking(self) -> tuple[None, list[str], list[int]]:
|
|
359
|
+
"""
|
|
360
|
+
Return a (next_input_timeout, keys_pressed, raw_keycodes)
|
|
361
|
+
tuple.
|
|
362
|
+
|
|
363
|
+
The protocol for our device requires waiting for acks between
|
|
364
|
+
each command, so this method responds to those as well as key
|
|
365
|
+
press and release events.
|
|
366
|
+
|
|
367
|
+
Key repeat events are simulated here as the device doesn't send
|
|
368
|
+
any for us.
|
|
369
|
+
|
|
370
|
+
raw_keycodes are the bytes of messages we received, which might
|
|
371
|
+
not seem to have any correspondence to keys_pressed.
|
|
372
|
+
"""
|
|
373
|
+
data_input: list[str] = []
|
|
374
|
+
raw_data_input: list[int] = []
|
|
375
|
+
timeout = None
|
|
376
|
+
|
|
377
|
+
packet = self._read_packet()
|
|
378
|
+
while packet:
|
|
379
|
+
command, data = packet
|
|
380
|
+
|
|
381
|
+
if command == self.CMD_KEY_ACTIVITY and data:
|
|
382
|
+
d0 = data[0]
|
|
383
|
+
if 1 <= d0 <= 12:
|
|
384
|
+
release = d0 > 6
|
|
385
|
+
keycode = d0 - (release * 6) - 1
|
|
386
|
+
key = self.key_map[keycode]
|
|
387
|
+
if release:
|
|
388
|
+
self.key_repeat.release(key)
|
|
389
|
+
else:
|
|
390
|
+
data_input.append(key)
|
|
391
|
+
self.key_repeat.press(key)
|
|
392
|
+
raw_data_input.append(d0)
|
|
393
|
+
|
|
394
|
+
elif command & 0xC0 == 0x40 and command & 0x3F == self._last_command: # "ACK"
|
|
395
|
+
self._send_next_command()
|
|
396
|
+
|
|
397
|
+
packet = self._read_packet()
|
|
398
|
+
|
|
399
|
+
next_repeat = self.key_repeat.next_event()
|
|
400
|
+
if next_repeat:
|
|
401
|
+
timeout, key = next_repeat
|
|
402
|
+
if not timeout:
|
|
403
|
+
data_input.append(key)
|
|
404
|
+
self.key_repeat.sent_event()
|
|
405
|
+
timeout = None
|
|
406
|
+
|
|
407
|
+
return timeout, data_input, raw_data_input
|
|
408
|
+
|
|
409
|
+
def _send_next_command(self) -> None:
|
|
410
|
+
"""
|
|
411
|
+
send out the next command in the queue
|
|
412
|
+
"""
|
|
413
|
+
if not self._command_queue:
|
|
414
|
+
self._last_command = None
|
|
415
|
+
return
|
|
416
|
+
command, data = self._command_queue.pop(0)
|
|
417
|
+
self._send_packet(command, data)
|
|
418
|
+
self._last_command = command # record command for ACK
|
|
419
|
+
self._last_command_time = time.time()
|
|
420
|
+
|
|
421
|
+
def queue_command(self, command: int, data: str) -> None:
|
|
422
|
+
self._command_queue.append((command, data))
|
|
423
|
+
# not waiting? send away!
|
|
424
|
+
if self._last_command is None:
|
|
425
|
+
self._send_next_command()
|
|
426
|
+
|
|
427
|
+
def draw_screen(self, size: tuple[int, int], canvas: Canvas):
|
|
428
|
+
if size != self.DISPLAY_SIZE:
|
|
429
|
+
raise ValueError(size)
|
|
430
|
+
|
|
431
|
+
if self._screen_buf:
|
|
432
|
+
osb = self._screen_buf
|
|
433
|
+
else:
|
|
434
|
+
osb = []
|
|
435
|
+
sb = []
|
|
436
|
+
|
|
437
|
+
for y, row in enumerate(canvas.content()):
|
|
438
|
+
text = [run for _a, _cs, run in row]
|
|
439
|
+
|
|
440
|
+
if not osb or osb[y] != text:
|
|
441
|
+
self.queue_command(self.CMD_LCD_DATA, chr(0) + chr(y) + "".join(text))
|
|
442
|
+
sb.append(text)
|
|
443
|
+
|
|
444
|
+
if (
|
|
445
|
+
self._previous_canvas
|
|
446
|
+
and self._previous_canvas.cursor == canvas.cursor
|
|
447
|
+
and (not self._update_cursor or not canvas.cursor)
|
|
448
|
+
):
|
|
449
|
+
pass
|
|
450
|
+
elif canvas.cursor is None:
|
|
451
|
+
self.queue_command(self.CMD_CURSOR_STYLE, chr(self.CURSOR_NONE))
|
|
452
|
+
else:
|
|
453
|
+
x, y = canvas.cursor
|
|
454
|
+
self.queue_command(self.CMD_CURSOR_POSITION, chr(x) + chr(y))
|
|
455
|
+
self.queue_command(self.CMD_CURSOR_STYLE, chr(self.cursor_style))
|
|
456
|
+
|
|
457
|
+
self._update_cursor = False
|
|
458
|
+
self._screen_buf = sb
|
|
459
|
+
self._previous_canvas = canvas
|
|
460
|
+
|
|
461
|
+
def program_cgram(self, index: int, data: Sequence[int]) -> None:
|
|
462
|
+
"""
|
|
463
|
+
Program character data. Characters available as chr(0) through
|
|
464
|
+
chr(7), and repeated as chr(8) through chr(15).
|
|
465
|
+
|
|
466
|
+
index -- 0 to 7 index of character to program
|
|
467
|
+
|
|
468
|
+
data -- list of 8, 6-bit integer values top to bottom with MSB
|
|
469
|
+
on the left side of the character.
|
|
470
|
+
"""
|
|
471
|
+
if not 0 <= index <= 7:
|
|
472
|
+
raise ValueError(index)
|
|
473
|
+
if len(data) != 8:
|
|
474
|
+
raise ValueError(data)
|
|
475
|
+
self.queue_command(self.CMD_CGRAM, chr(index) + "".join([chr(x) for x in data]))
|
|
476
|
+
|
|
477
|
+
def set_cursor_style(self, style: Literal[1, 2, 3, 4]) -> None:
|
|
478
|
+
"""
|
|
479
|
+
style -- CURSOR_BLINKING_BLOCK, CURSOR_UNDERSCORE,
|
|
480
|
+
CURSOR_BLINKING_BLOCK_UNDERSCORE or
|
|
481
|
+
CURSOR_INVERTING_BLINKING_BLOCK
|
|
482
|
+
"""
|
|
483
|
+
if not 1 <= style <= 4:
|
|
484
|
+
raise ValueError(style)
|
|
485
|
+
self.cursor_style = style
|
|
486
|
+
self._update_cursor = True
|
|
487
|
+
|
|
488
|
+
def set_backlight(self, value: int) -> None:
|
|
489
|
+
"""
|
|
490
|
+
Set backlight brightness
|
|
491
|
+
|
|
492
|
+
value -- 0 to 100
|
|
493
|
+
"""
|
|
494
|
+
if not 0 <= value <= 100:
|
|
495
|
+
raise ValueError(value)
|
|
496
|
+
self.queue_command(self.CMD_BACKLIGHT, chr(value))
|
|
497
|
+
|
|
498
|
+
def set_lcd_contrast(self, value: int) -> None:
|
|
499
|
+
"""
|
|
500
|
+
value -- 0 to 255
|
|
501
|
+
"""
|
|
502
|
+
if not 0 <= value <= 255:
|
|
503
|
+
raise ValueError(value)
|
|
504
|
+
self.queue_command(self.CMD_LCD_CONTRAST, chr(value))
|
|
505
|
+
|
|
506
|
+
def set_led_pin(self, led: Literal[0, 1, 2, 3], rg: Literal[0, 1], value: int):
|
|
507
|
+
"""
|
|
508
|
+
led -- 0 to 3
|
|
509
|
+
rg -- 0 for red, 1 for green
|
|
510
|
+
value -- 0 to 100
|
|
511
|
+
"""
|
|
512
|
+
if not 0 <= led <= 3:
|
|
513
|
+
raise ValueError(led)
|
|
514
|
+
if rg not in {0, 1}:
|
|
515
|
+
raise ValueError(rg)
|
|
516
|
+
if not 0 <= value <= 100:
|
|
517
|
+
raise ValueError(value)
|
|
518
|
+
self.queue_command(self.CMD_GPO, chr(12 - 2 * led - rg) + chr(value))
|
urwid/display/raw.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Urwid raw display module
|
|
2
|
+
# Copyright (C) 2004-2009 Ian Ward
|
|
3
|
+
#
|
|
4
|
+
# This library is free software; you can redistribute it and/or
|
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
|
6
|
+
# License as published by the Free Software Foundation; either
|
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+
# Lesser General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
|
15
|
+
# License along with this library; if not, write to the Free Software
|
|
16
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17
|
+
#
|
|
18
|
+
# Urwid web site: https://urwid.org/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
Direct terminal UI implementation
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import sys
|
|
28
|
+
|
|
29
|
+
__all__ = ("Screen",)
|
|
30
|
+
|
|
31
|
+
IS_WINDOWS = sys.platform == "win32"
|
|
32
|
+
|
|
33
|
+
if IS_WINDOWS:
|
|
34
|
+
from ._win32_raw_display import Screen
|
|
35
|
+
|
|
36
|
+
else:
|
|
37
|
+
from ._posix_raw_display import Screen
|