Repository navigation
Expand file tree
/
Copy pathlcd.py
More file actions
409 lines (324 loc) · 14.5 KB
/
Copy pathlcd.py
File metadata and controls
409 lines (324 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
from __future__ import annotations
import ctypes
from enum import Enum
import itertools
import math
from typing import Any, Callable, NamedTuple, Optional, TypeVar
try:
from typing import Final, Literal
except ImportError:
from typing_extensions import Final, Literal
from eye import lib
from eyepy.drawing import Image, ImageResolution, Colour, IntPoint, IntPointLike, Point, PointLike, colour_to_str, make_coord_map
def _LCD_OK(return_code: int) -> bool:
"""
Default LCD return code checking behaviour.
"""
return return_code == 0
from eye import LCDPrintf as _LCDPrintf
def LCDPrintf(format: str, *data: Any) -> bool:
return_code = _LCDPrintf(format, *data)
return _LCD_OK(return_code)
from eye import LCDSetPrintf as _LCDSetPrintf
def LCDSetPrintf(row: int, col: int, format: str, *data: Any) -> bool:
return_code = _LCDSetPrintf(row, col, format, *data)
return _LCD_OK(return_code)
from eye import LCDClear as _LCDClear
def LCDClear() -> bool:
return_code = _LCDClear()
return _LCD_OK(return_code)
from eye import LCDSetPos as _LCDSetPos
def LCDSetPos(row: int, col: int) -> bool:
return_code = _LCDSetPos(row, col)
return _LCD_OK(return_code)
class LCDPos(NamedTuple):
row: int
col: int
def LCDGetPos() -> LCDPos:
"""
Throws a `RuntimeError` if the internal call to `LCDGetPos` returns an error value.
"""
row = ctypes.c_int()
col = ctypes.c_int()
return_code = lib.LCDGetPos(ctypes.pointer(row), ctypes.pointer(col))
if not _LCD_OK(return_code): raise RuntimeError()
return LCDPos(row=row.value, col=col.value)
def _validate_colours(*cols: Colour):
"""
Throws a `ValueError` if a colour is invalid.
"""
for col in cols:
if col < 0x000000 or col > 0xFFFFFF:
raise ValueError(f"colour {colour_to_str(col)} is not a valid colour (out of bounds)")
from eye import LCDSetColor as _LCDSetColor
def LCDSetColor(*, foreground: Colour, background: Colour, validate_cols: bool = True) -> bool:
"""
:param:`foreground` and :param:`background` use RGB hex colour codes,
e.g. 0x000000 => black, 0xFFFFFF => white.
See the docs for the predefined colour constants.
"""
if validate_cols: _validate_colours(foreground, background)
return_code = _LCDSetColor(foreground, background)
return _LCD_OK(return_code)
# can't use unions of pre-defined literals, so we use enums to get type checking
from eye import HELVETICA as _HELVETICA, TIMES as _TIMES, COURIER as _COURIER
class Font(Enum):
HELVETICA = _HELVETICA
TIMES = _TIMES
COURIER = _COURIER
HELVETICA: Final[Font] = Font.HELVETICA
TIMES: Final[Font] = Font.TIMES
COURIER: Final[Font] = Font.COURIER
from eye import NORMAL as _NORMAL, BOLD as _BOLD, ITALICS as _ITALICS
class FontVariation(Enum):
NORMAL = _NORMAL
BOLD = _BOLD
ITALICS = _ITALICS
NORMAL: Final[FontVariation] = FontVariation.NORMAL
BOLD: Final[FontVariation] = FontVariation.BOLD
ITALICS: Final[FontVariation] = FontVariation.ITALICS
from eye import LCDSetFont as _LCDSetFont
def LCDSetFont(font: Font, variation: FontVariation) -> bool:
"""
Doesn't work with x11 on linux.
"""
return_code = _LCDSetFont(font, variation)
return _LCD_OK(return_code)
FontSize = Literal[8, 10, 12, 14, 18, 24]
# `lib.LCDSetFontSize`` (incorrectly) defined in eye.py, and also has no python function
lib.LCDSetFontSize.argtypes = [ctypes.c_int]
def LCDSetFontSize(size: FontSize) -> bool:
"""
Doesn't work with x11 on linux.
"""
return_code = lib.LCDSetFontSize(size)
return _LCD_OK(return_code)
LCDMode = int
from eye import LCDSetMode as _LCDSetMode
lib.LCDSetMode.argtypes = [ctypes.c_int] # incorrectly defined in eye.py
def LCDSetMode(mode: LCDMode) -> bool:
"""
Appears to do stuff in libeyesim, but hasn't seemed
to change the LCD behaviour during testing.
"""
return_code = _LCDSetMode(mode)
return _LCD_OK(return_code)
from eye import LCDMenu as _LCDMenu
def LCDMenu(str1: str, str2: str, str3: str, str4: str) -> bool:
return_code = _LCDMenu(str1, str2, str3, str4)
return _LCD_OK(return_code)
from eye import LCDMenuI as _LCDMenuI
def LCDMenuI(entry: int, string: str, *, foreground: Colour, background: Colour, validate_cols: bool = True) -> bool:
if validate_cols: _validate_colours(foreground, background)
return_code = _LCDMenuI(entry, string, foreground, background)
return _LCD_OK(return_code)
class LCDSize(NamedTuple):
width: int
"""px"""
height: int
"""px"""
# `eye.LCDGetSize`` wants pointers, so we might as well directly use `lib.LCDGetSize`
def LCDGetSize() -> LCDSize:
"""
Throws a `RuntimeError` if the internal call to `LCDGetSize` returns an error value.
Printable pixel values include 0 but exclude the values returned.
"""
width = ctypes.c_int()
height = ctypes.c_int()
return_code = lib.LCDGetSize(ctypes.pointer(width), ctypes.pointer(height))
if not _LCD_OK(return_code): raise RuntimeError()
return LCDSize(width=width.value, height=height.value)
def lcd_make_coord_map(p2: PointLike, p1: PointLike = Point(0, 0)) -> Callable[[Point], IntPoint]:
"""
Produces a function flipping the y axis, mapping :param:`p2` to the
top right of the screen, and :param:`p1` to the bottom left.
"""
display_max_x, display_max_y = LCDGetSize()
bottom_left = Point(0, display_max_y - 1)
top_right = Point(display_max_x - 1, 0)
f = make_coord_map((p1, p2), (bottom_left, top_right))
f_int: Callable[[Point], IntPoint] = lambda p: f(p).round()
return f_int
def lcd_default_point_map(p: Point) -> IntPoint:
return p.round()
lcd_default_world_coord_map = lcd_make_coord_map((2000, 2000))
_lcd_point_map: Callable[[Point], IntPoint] = lcd_default_point_map
def LCDSetPointMap(f: Callable[[Point], IntPoint]):
global _lcd_point_map
_lcd_point_map = f
from eye import LCDPixel as _LCDPixel
def LCDPixel(pixel: PointLike, col: Colour, *, validate_col: bool = True, point_map_override: Callable[[Point], IntPoint] | None = None) -> bool:
if validate_col: _validate_colours(col)
pixel = Point(*pixel)
map = _lcd_point_map
if point_map_override is not None:
map = point_map_override
mapped_point = map(pixel)
return_code = _LCDPixel(mapped_point.x, mapped_point.y, col)
return _LCD_OK(return_code)
from eye import LCDGetPixel as _LCDGetPixel
def LCDGetPixel(pixel: PointLike, *, validate: bool = True, point_map_override: Callable[[Point], IntPoint] | None = None) -> Colour:
"""
If :param:`validate` is `True`, a `RuntimeError` will be thrown if
an invalid colour is returned by `LCDGetPixel`.
"""
pixel = Point(*pixel)
map = _lcd_point_map
if point_map_override is not None:
map = point_map_override
mapped_point = map(pixel)
col = _LCDGetPixel(mapped_point.x, mapped_point.y)
if validate:
try:
_validate_colours(col)
except ValueError as err:
raise RuntimeError(*err.args)
return col
from eye import LCDLine as _LCDLine
def LCDLine(p1: PointLike, p2: PointLike, col: Colour, *, validate_col: bool = True, point_map_override: Callable[[Point], IntPoint] | None = None) -> bool:
if validate_col: _validate_colours(col)
p1 = Point(*p1)
p2 = Point(*p2)
map = _lcd_point_map
if point_map_override is not None:
map = point_map_override
mapped_p1 = map(p1)
mapped_p2 = map(p2)
return_code = _LCDLine(mapped_p1.x, mapped_p1.y, mapped_p2.x, mapped_p2.y, col)
return _LCD_OK(return_code)
from eye import LCDArea as _LCDArea
def LCDArea(p1: PointLike, p2: PointLike, col: Colour, *, fill: bool = True, validate_col: bool = True, point_map_override: Callable[[Point], IntPoint] | None = None) -> bool:
if validate_col: _validate_colours(col)
p1 = Point(*p1)
p2 = Point(*p2)
map = _lcd_point_map
if point_map_override is not None:
map = point_map_override
mapped_p1 = map(p1)
mapped_p2 = map(p2)
return_code = _LCDArea(mapped_p1.x, mapped_p1.y, mapped_p2.x, mapped_p2.y, col, int(fill))
return _LCD_OK(return_code)
def LCDPixelArea(pixel: PointLike, col: Colour, *, pixel_dx: float = 1, pixel_dy: float = 1, validate_col: bool = True, point_map_override: Callable[[Point], IntPoint] | None = None) -> bool:
"""
Assumes point map is locally linear and pixels are still aligned after being mapped (rotation by 90 is fine).
:param:`pixel_dx` and :param:`pixel_dy` are the dimensions of pixels in the coordinate space before mapping.
"""
pixel = Point(*pixel)
if validate_col: _validate_colours(col)
pixel = Point(*pixel)
map = _lcd_point_map
if point_map_override is not None:
map = point_map_override
mapped_point = map(pixel)
# calculate approx bounding box by finding the mapped locations of the adjacent pixels
mapped_adj_pixels = [map(pixel + (dx * pixel_dx, dy * pixel_dy)) for dx, dy in itertools.product(range(-1, 1 + 1), range(-1, 1 + 1)) if not (dx == 0 and dy == 0)]
bound_left = min(mapped_adj_pixels, key=lambda p: p.x).x
bound_right = max(mapped_adj_pixels, key=lambda p: p.x).x
bound_top = min(mapped_adj_pixels, key=lambda p: p.y).y
bound_bottom = max(mapped_adj_pixels, key=lambda p: p.y).y
# find new bounds - favour top-left for rounding
area_left = math.floor((mapped_point.x + bound_left) / 2)
area_right = math.floor((mapped_point.x + bound_right) / 2)
area_top = math.floor((mapped_point.y + bound_top) / 2)
area_bottom = math.floor((mapped_point.y + bound_bottom) / 2)
p1 = IntPoint(area_left, area_top)
p2 = IntPoint(area_right, area_bottom)
if p1 == p2: # area is just 1 pixel
return LCDPixel(p1, col, point_map_override=lcd_default_point_map)
return LCDArea(p1, p2, col, fill=True, point_map_override=lcd_default_point_map)
from eye import LCDCircle as _LCDCircle
def LCDCircle(centre: PointLike, size: int, col: Colour, *, fill: bool = True, validate_col: bool = True, point_map_override: Callable[[Point], IntPoint] | None = None) -> bool:
"""
TODO check what 'size' means (radius? diameter?)
Note: size is not scaled by the point map
"""
if validate_col: _validate_colours(col)
centre = Point(*centre)
map = _lcd_point_map
if point_map_override is not None:
map = point_map_override
mapped_centre = map(centre)
return_code = _LCDCircle(mapped_centre.x, mapped_centre.y, size, col, int(fill))
return _LCD_OK(return_code)
_image_position: IntPoint = IntPoint(0, 0)
from eye import LCDImageSize as _LCDImageSize
def LCDImageSize(resolution: ImageResolution) -> bool:
"""
Note: the image size is automatically set when printed by
:func:`LCDImage`, :func:`LCDImageGray`, or :func:`LCDImageBinary`;
this function does not need to be called manually.
"""
resolution_code = resolution._code
return_code = _LCDImageSize(resolution_code)
return _LCD_OK(return_code)
from eye import LCDImageStart as _LCDImageStart
def LCDImageStart(start: IntPointLike, *, width: int, height: int) -> bool:
"""
Sets default image position and size.
Note: image size will be overwritten automatically when calls to :func:`LCDImage`,
:func:`LCDImageGray`, or :func:`LCDImageBinary` are made.
"""
start = IntPoint(*start)
return_code = _LCDImageStart(start.x, start.y, width, height)
if not _LCD_OK(return_code):
return False
global _image_position
_image_position = start
return True
R = TypeVar("R")
def _lcd_image_print_base(image: Image, *, print_func: Callable[[ctypes.Array[ctypes.c_byte]], R], ok_predicate: Callable[[R], bool], start: Optional[IntPointLike]) -> bool:
default_position = _image_position
if not LCDImageStart(
start if start is not None else default_position,
width=image.resolution.WIDTH,
height=image.resolution.HEIGHT
): return False
image_bytes = image._c_bytes
return_code = print_func(image_bytes)
if start is not None:
if not LCDImageStart(default_position, width=image.resolution.WIDTH, height=image.resolution.HEIGHT):
return False
return ok_predicate(return_code)
# `eye.LCDImage` simply wraps it, so we directly use `lib.LCDImage`
def LCDImage(image: Image, *, start: Optional[IntPointLike] = None) -> bool:
"""
Returns `False` if the image is not the correct type (e.g. is a gray image),
or if the internal calls to `LCDImageStart` or `LCDImage` return an error value.
If :param:`start` is specified, it will be used for the image position rather
than the current default image position. Does not override the default image position.
"""
if image.is_gray:
return False
return _lcd_image_print_base(image, print_func=lib.LCDImage, ok_predicate=_LCD_OK, start=start)
# `eye.LCDImageGray` simply wraps it, so we directly use `lib.LCDImageGray`
def LCDImageGray(image: Image, *, start: Optional[IntPointLike] = None) -> bool:
"""
Returns `False` if the image is not the correct type (e.g. is a colour image),
or if the internal calls to `LCDImageStart` or `LCDImageGray` return an error value.
If :param:`start` is specified, it will be used for the image position rather
than the current default image position. Does not override the default image position.
"""
if not image.is_gray:
return False
return _lcd_image_print_base(image, print_func=lib.LCDImageGray, ok_predicate=_LCD_OK, start=start)
# `eye.LCDImageBinary` simply wraps it, so we directly use `lib.LCDImageBinary`
def LCDImageBinary(image: Image, *, start: Optional[IntPointLike] = None) -> bool:
"""
Expects a gray image using only 0 (white) and 1 (black).
If :param:`validate` is `True`, will return `False` if the image is not
the correct type (e.g. is not binary). If :param:`validate` is `False`,
no attempt to validate the image will be made.
If :param:`start` is specified, it will be used for the image position rather
than the current default image position. Does not override the default image position.
Note: appears to be :func:`LCDImageGray`, but subtracting 1 from each pixel
(with 0 - 1 => 255 (white), 1 - 1 => 0 (black), and every other value
remaining approximately the same).
"""
if not image.is_gray:
return False
return _lcd_image_print_base(image, print_func=lib.LCDImageBinary, ok_predicate=_LCD_OK, start=start)
from eye import LCDRefresh as _LCDRefresh
def LCDRefresh() -> bool:
return_code = _LCDRefresh()
return _LCD_OK(return_code)
# TODO LCD modes?