-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_to_display.py
executable file
·83 lines (65 loc) · 2.83 KB
/
write_to_display.py
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
#! /usr/bin/env -S python3 -u
from importlib import import_module
import sys
import argparse
import json
from pathlib import Path
from display_protocol import Display
def import_display_module(name: str) -> Display:
try:
display_module = import_module(f'{name}')
return display_module.init_display()
except ModuleNotFoundError:
print('Display module not found, will use "simulated" instead')
from simulated import Simulated
return Simulated()
def main() -> int:
parser = argparse.ArgumentParser(
description='Write an image to a display')
parser.add_argument('display', metavar='name', type=str, help='Name of the display',
choices={'simulated', 'pimoroni_inky', 'GDEP073E01'})
parser.add_argument('image_path', metavar='path', type=Path, help='Image to write')
parser.add_argument('--preview', metavar='path', type=Path, required=False, default=None,
help='Optional preview image output')
parser.add_argument('--status', action='store_true', help='Print status')
parser.add_argument('--palette', metavar='name', type=str, required=False,
default='waveshare_gallery_palette', help='Display palette name')
parser.add_argument('--options', metavar='json', type=str, required=False, default={},
help='Options as a JSON encoded string')
arguments = parser.parse_args()
display: Display = import_display_module(arguments.display)
if arguments.status:
print('Status: CONVERTING')
palette = py_encre.palette_by_name[arguments.palette]
if arguments.options:
options = py_encre.Options(**json.loads(arguments.options))
else:
options = py_encre.Options()
preview_path: str = None
if arguments.preview:
preview_path = str(arguments.preview)
rotation = py_encre.read_compatible_encre_file(str(arguments.image_path),
len(palette.points), display.buf)
if not rotation:
rotation = py_encre.convert(str(arguments.image_path), palette,
display.buf, options=options)
if not rotation:
print('Conversion failed', file=sys.stderr)
return 1
if preview_path and not py_encre.write_preview(display.buf, palette.points,
rotation, preview_path):
print('Preview creation failed', file=sys.stderr)
return 1
if arguments.status:
print('Status: DISPLAYING')
display.show()
return 0
if __name__ == '__main__':
sys.path.append(str(Path(__file__).absolute().parent.parent / 'build/release/py'))
import py_encre
py_encre.initialize(sys.argv[0])
try:
result = main()
finally:
py_encre.uninitalize()
sys.exit(result)