forked from RetiredWizard/PyDOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayimage.py
More file actions
213 lines (183 loc) · 7.22 KB
/
Copy pathplayimage.py
File metadata and controls
213 lines (183 loc) · 7.22 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
import board
try:
import gifio
except:
pass
import adafruit_imageload
import bitmaptools
import displayio
import time
from os import getenv
try:
from pydos_ui import Pydos_ui
from pydos_ui import input
Pydos_display = ('display' in dir(Pydos_ui))
except:
Pydos_display = False
try:
type(envVars)
except:
envVars = {}
if '_display' in envVars.keys():
display = envVars['_display']
elif Pydos_display:
display = Pydos_ui.display
elif 'DISPLAY' in dir(board):
display = board.DISPLAY
else:
try:
import matrix
display = matrix.envVars['_display']
except:
try:
import framebufferio
import dotclockframebuffer
except:
try:
import adafruit_ili9341
except:
import framebufferio
import picodvi
displayio.release_displays()
if 'TFT_PINS' in dir(board):
sWdth = getenv('CIRCUITPY_DISPLAY_WIDTH')
if sWdth == None:
if board.board_id == "makerfabs_tft7":
sWdth = input("What is the resolution Width of the touch screen? (1024/800/...): ")
else:
sWdth = board.TFT_TIMINGS['width']
if 'updateTOML' in dir(Pydos_ui):
Pydos_ui.updateTOML("CIRCUITPY_DISPLAY_WIDTH",str(sWdth))
if sWdth == 1024 and "TFT_TIMINGS1024" in dir(board):
disp_bus=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS1024)
else:
disp_bus=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS)
display=framebufferio.FramebufferDisplay(disp_bus)
else:
try:
type(adafruit_ili9341)
if 'SPI' in dir(board):
spi = board.SPI()
else:
spi = busio.SPI(clock=board.SCK,MOSI=board.MOSI,MISO=board.MISO)
disp_bus=displayio.FourWire(spi,command=board.D10,chip_select=board.D9, \
reset=board.D6)
display=adafruit_ili9341.ILI9341(disp_bus,width=320,height=240)
except:
# DVI Sock
fb = picodvi.Framebuffer(320,240,clk_dp=board.GP14, clk_dn=board.GP15, \
red_dp=board.GP12, red_dn=board.GP13,green_dp=board.GP18, \
green_dn=board.GP19,blue_dp=board.GP16, blue_dn=board.GP17,color_depth=8)
display=framebufferio.FramebufferDisplay(fb)
def playimage(passedIn=""):
if passedIn != "":
fname = passedIn
else:
fname = ""
if fname == "":
fname = input("Enter filename: ")
try:
while Pydos_ui.virt_touched():
pass
except:
pass
if fname==passedIn:
print('Press "q" to quit')
else:
input('Press "Enter" to continue, press "q" to quit')
if fname[-4:].upper() in [".BMP",".PNG",".JPG",".RLE"]:
bitmap, palette = adafruit_imageload.load( \
fname, bitmap=displayio.Bitmap, palette=displayio.Palette)
scalefactor = display.width / bitmap.width
if display.height/bitmap.height < scalefactor:
scalefactor = display.height/bitmap.height
if scalefactor < 1:
print(f'scalefactor: {scalefactor}')
bitframe = displayio.Bitmap(display.width,display.height,2**bitmap.bits_per_value)
bitmaptools.rotozoom(bitframe,bitmap,scale=scalefactor)
facecc = displayio.TileGrid(bitframe,pixel_shader=palette)
# pixel_shader=displayio.ColorConverter(input_colorspace=colorspace))
pwidth = bitframe.width
pheight = bitframe.height
else:
facecc = displayio.TileGrid(bitmap,pixel_shader=palette)
# pixel_shader=displayio.ColorConverter(input_colorspace=colorspace))
pwidth = bitmap.width
pheight = bitmap.height
print("bitmap (w,h): ",bitmap.width,bitmap.height)
print("scaled bitmap (w,h): ",pwidth,pheight)
print("facecc (w,h): ",facecc.width,facecc.height)
if pwidth < display.width:
facecc.x = (display.width-pwidth)//2
if pheight < display.height:
facecc.y = (display.height-pheight)//2
splash = displayio.Group()
splash.append(facecc)
display.root_group = splash
input('Press Enter to close')
elif fname[-4:].upper() in [".GIF"]:
odgcc = gifio.OnDiskGif(fname)
with odgcc as odg:
if getenv('PYDOS_DISPLAYIO_COLORSPACE',"").upper() == 'BGR565_SWAPPED':
colorspace = displayio.Colorspace.BGR565_SWAPPED
else:
colorspace = displayio.Colorspace.RGB565_SWAPPED
scalefactor = display.width / odg.width
if display.height/odg.height < scalefactor:
scalefactor = display.height/odg.height
if scalefactor < 1:
print(f'scalefactor: {scalefactor}')
bitframe = displayio.Bitmap(display.width,display.height,2**odg.bitmap.bits_per_value)
bitmaptools.rotozoom(bitframe,odg.bitmap,scale=scalefactor)
facecc = displayio.TileGrid(bitframe, \
pixel_shader=displayio.ColorConverter(input_colorspace=colorspace))
pwidth = bitframe.width
pheight = bitframe.height
else:
facecc = displayio.TileGrid(odg.bitmap, \
pixel_shader=displayio.ColorConverter(input_colorspace=colorspace))
pwidth = odg.bitmap.width
pheight = odg.bitmap.height
if pwidth < display.width:
facecc.x = (display.width-pwidth)//2
if pheight < display.height:
facecc.y = (display.height-pheight)//2
splash = displayio.Group()
splash.append(facecc)
display.root_group = splash
start = 0
next_delay = -1
cmnd = ""
# Display repeatedly.
while cmnd.upper() != "Q":
if Pydos_ui.serial_bytes_available():
cmnd = Pydos_ui.read_keyboard(1)
print(cmnd, end="", sep="")
if cmnd in "qQ":
break
while time.monotonic() > start and next_delay > time.monotonic()-start:
pass
next_delay = odg.next_frame()
start = time.monotonic()
if next_delay > 0:
if scalefactor < 1:
bitmaptools.rotozoom(bitframe,odg.bitmap,scale=scalefactor)
else:
print('Unknown filetype')
try:
splash.pop()
odgcc = None
facecc.bitmap.deinit()
facecc = None
if scalefactor < 1:
bitframe.deinit()
bitframe = None
except:
pass
display.root_group = displayio.CIRCUITPYTHON_TERMINAL
if '_display' not in envVars.keys():
envVars['_display'] = display
if __name__ == "PyDOS":
playimage(passedIn)
else:
print("Enter 'playimage.playimage('filename')' in the REPL or PEXEC command to run.")